최근 검색


최근 검색 없음

User Segment API not working for end user

답변함


2023년 1월 06일에 게시됨

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.


0

7

댓글 7개

      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.

       

      0


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

      0


      Hi Gaby Bitar

      Thank you very much, it helps I will try after some rest 😛

      Best regards,

      Raphaël

      0


      Hi Raphaël Péguet,

      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!

      0


      Hi Gaby Bitar

      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

      0


      Ahmed Zaid Thank you, it worked!

      0


      Ahmed Zaid is there no way to determine what User Segments an end-user belongs to?

      0


      댓글을 남기려면 로그인하세요.

      원하는 정보를 못 찾으셨나요?

      새 게시물