User Segment API not working for end user | Community
Skip to main content

User Segment API not working for end user

  • January 6, 2023
  • 7 replies
  • 0 views

Gaby11

I am trying to pull which user segment the current logged in end user belongs to. I am currently using the "GET /api/v2/users/me" to retrieve the current users ID and using that ID to try to pull the user segments. (Code below, it's working for admins but not for end users, I also masked the email and tokenID for this forum purposes)

 

$.getJSON('/api/v2/users/me', function(data) {
    console.log("got data", data);
      getUserSegment(data.user.id);
});


function getUserSegment(userID){  
  // fetch UserSegment API.  
  $.ajax
  ({
  url: '/api/v2/help_center/users/'+userID+'/user_segments.json',
  headers: {
    "Authorization": "Basic email-address/token:tokenID"
  },
  beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', "Basic "+ btoa("email-address/token:tokenID")); 
    },
  secure: true,
  type: 'GET',
  contentType: "application/json",
  dateType: 'json',
  success: function(data){
    data.user_segments.forEach(function(element, index) {
          alert(element.name);
        });
    }
});
  }

 

Please help, not sure why it's not working.

7 replies

Gaby11
  • Author
  • January 7, 2023

@ahmed11 is there no way to determine what User Segments an end-user belongs to?


Gaby11
  • Author
  • January 9, 2023

@ahmed11 Thank you, it worked!


raphael12
  • February 13, 2023

Hi @gaby11

Did you find out how to do specific things depending on the user segment please?

I am looking for the same thing, :)

Best regards! 

Raphaƫl


Gaby11
  • Author
  • February 15, 2023

Hi @raphael12,

Yes I did, I used the Users/me api to grab the current users ID number and add that to a variable - https://developer.zendesk.com/api-reference/ticketing/users/users/#show-self

Then I used that userID to call the user segments api and list the current users User Segments.

/api/v2/help_center/users/{user_id}/user_segments.json

(https://developer.zendesk.com/api-reference/help_center/help-center-api/user_segments/#list-user-segments)

I hope this helps!


raphael12
  • February 17, 2023

Hi @gaby11

Thank you very much, it helps I will try after some rest šŸ˜›

Best regards,

Raphaƫl


  • July 17, 2025

@gaby11 @ahmed11, What's the solution for this? I don't see Ahmed Zaid's answer here. Thank you in advance.


Ahmed11
  • July 24, 2025

Hi Ravi Kumar this is only possible for agents. You need some elaborate workarounds for a user to identify their own user segment:

Option 1: Mirror the user segment conditions in your frontend code if possible. For example, User is in segment foo which applies to orgs with tag bar:

<script>
    document.addEventListener("DOMContentLoaded", () => {
        for (let org of HelpCenter.user.organizations) {
            if (org.tags.includes("bar")) {
                // Apply foo segment logic
                break;
            }
        }
    });
</script>

Option 2: If you have arbitrary conditions and you have categories/sections dedicated to this segment, iterate over categories using helpers and objects and test if a user can access the restricted sections. This is more complicated and may not apply to all templates.