TTCG
TTCG

Reputation: 9113

Google Analytics Custom Variables not working

I am trying to log the username of users who are using my system. Unfortunately, I couldn't track by using custom variables. I could track the page visit, but custom variable tracking is not working. Could you please help me? Thanks.

<script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-xxxxxxx-1']);
      _gaq.push(['_setDomainName', 'none']);
      _gaq.push(['_setCustomVar',1,'UserName','JohnPaul',2]);
      _gaq.push(['_trackPageview']);      

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();


    </script>

Upvotes: 0

Views: 1038

Answers (2)

kenold
kenold

Reputation: 318

For more than one user, you'll have to use PHP to output user names.

_gaq.push(['_setCustomVar', 1, 'UserName', '<?php get_usernames(); ?>',2]);

***Substitute "get_usernames" with the function for your site that retrieves the usernames.

Upvotes: 0

TTCG
TTCG

Reputation: 9113

The code is correct and it does track the users now. I just used the similar codes as above and it works now. But I needed to wait 2 days to track the custom variables and it appears in my reports now.

Upvotes: 1

Related Questions