google-tag-managerms-clarity

How to set custom ID (user id) in microsoft clarity?


I want to send a custom user id to Microsoft clarity from GTM and write this in Tag manager.

<script type="text/javascript">
    (function(c,l,a,r,i,t,y){
        c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
        t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
        y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
    })(window, document, "clarity", "script", "99999999");
  
    var userId = {{Is Logged In}} ? {{userid}} : 0 ;
    window.clarity("identify", userId)
</script>

but when I filter my user id or even 0 in custom user ID field in the filter section, it doesn't find anything.

what should I do now?


Solution

  • after some research and make discussion with clarity account finally I found this solution worked:

    use clarity("set") inside the function.

    the point is you should use your custom user id as string not number! the final code will be like this:

    var currentUserId = {{Is Logged In}} ? {{userid}} : '0' ;
    (function(c,l,a,r,i,t,y){
        c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
        t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
        y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
        clarity("set", "userId", currentUserId.toString());
    })(window, document, "clarity", "script", "xxxxx");