Resource icon

Track Usergroups in Google Analytics

DBA

Well-known member
DBA submitted a new resource:

Track Usergroups in Google Analytics (version 1.0) - Track your Usergroups in Google Analytics

Track your usergroups via the Google Analytics Custom Variables and Advanced Segments.

Replace the code in your google_analytics template with the following.

Code:
<xen:if is="{$xenOptions.googleAnalyticsWebPropertyId}"><script>
<script type="text/javascript">
 
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '{$xenOptions.googleAnalyticsWebPropertyId}']);
  _gaq.push(['_setCustomVar',
      1,
      'Usergroup',
<xen:if is="{xen:helper ismemberof, $visitor, 1}">...

Read more about this resource...
 
Thanks for this...I've been wanting to track this for a while now just didn't know how. Hopefully this gives me the info I need. Are you doing any other advanced tracking on your site?
 
Thanks for this...I've been wanting to track this for a while now just didn't know how. Hopefully this gives me the info I need. Are you doing any other advanced tracking on your site?
The only other thing I'm did with the Analytics code was add this:
Code:
  _gaq.push(['_setSiteSpeedSampleRate', 100]);

Plus we have a couple funnels/goals setup, however that doesn't take extra code.
 
The only other thing I'm did with the Analytics code was add this:
Code:
  _gaq.push(['_setSiteSpeedSampleRate', 100]);

Plus we have a couple funnels/goals setup, however that doesn't take extra code.
What's the default for the sample rate? Any reason why you added that?

What funnels did you set up? For registering? Or for merchandise? If for registering, can you post a screenshot of the set up please?
 
Well, I installed this the other day and it stopped tracking everything :/ Maybe I edited something wrong. But this is what I have. Maybe I added too many options? Or did them wrong?

Code:
<xen:if is="{$xenOptions.googleAnalyticsWebPropertyId}"><script>
<script type="text/javascript">
 
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '{$xenOptions.googleAnalyticsWebPropertyId}']);
  _gaq.push(['_setCustomVar',
      1,
      'Usergroup',
<xen:if is="{xen:helper ismemberof, $visitor, 1}">
      'Unregistered-1',
<xen:elseif is="{xen:helper ismemberof, $visitor, 2}" />
      'Member-2',
<xen:elseif is="{xen:helper ismemberof, $visitor, 3}" />
      'Administrator-3',
<xen:elseif is="{xen:helper ismemberof, $visitor, 4}" />
      'SuperModerator-4',
<xen:elseif is="{xen:helper ismemberof, $visitor, 7}" />
      'DietModerator-7',
<xen:elseif is="{xen:helper ismemberof, $visitor, 8}" />
      'Premium-8',
<xen:elseif is="{xen:helper ismemberof, $visitor, 34}" />
      'Retired-34',
<xen:elseif is="{xen:helper ismemberof, $visitor, 9}" />
      'VIP-9',
<xen:elseif is="{xen:helper ismemberof, $visitor, 27}" />
      'Writer-27',
<xen:elseif is="{xen:helper ismemberof, $visitor, 25}" />
      'CommunityChamp-25',
<xen:elseif is="{xen:helper ismemberof, $visitor, 47}" />
      'MOTM-47',
</xen:if>
      1
]);
  _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>
</xen:if>
 
Well, I installed this the other day and it stopped tracking everything :/ Maybe I edited something wrong. But this is what I have. Maybe I added too many options? Or did them wrong?

Code:
<xen:if is="{$xenOptions.googleAnalyticsWebPropertyId}"><script>
<script type="text/javascript">
 
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '{$xenOptions.googleAnalyticsWebPropertyId}']);
  _gaq.push(['_setCustomVar',
      1,
      'Usergroup',
<xen:if is="{xen:helper ismemberof, $visitor, 1}">
      'Unregistered-1',
<xen:elseif is="{xen:helper ismemberof, $visitor, 2}" />
      'Member-2',
<xen:elseif is="{xen:helper ismemberof, $visitor, 3}" />
      'Administrator-3',
<xen:elseif is="{xen:helper ismemberof, $visitor, 4}" />
      'SuperModerator-4',
<xen:elseif is="{xen:helper ismemberof, $visitor, 7}" />
      'DietModerator-7',
<xen:elseif is="{xen:helper ismemberof, $visitor, 8}" />
      'Premium-8',
<xen:elseif is="{xen:helper ismemberof, $visitor, 34}" />
      'Retired-34',
<xen:elseif is="{xen:helper ismemberof, $visitor, 9}" />
      'VIP-9',
<xen:elseif is="{xen:helper ismemberof, $visitor, 27}" />
      'Writer-27',
<xen:elseif is="{xen:helper ismemberof, $visitor, 25}" />
      'CommunityChamp-25',
<xen:elseif is="{xen:helper ismemberof, $visitor, 47}" />
      'MOTM-47',
</xen:if>
      1
]);
  _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>
</xen:if>
Did you already remove it from your site?
 
I'm not sure if this is how it should look in the code...I only clipped part of the code for you.
Code:
  _gaq.push(['_setCustomVar',
      1,
      'Usergroup',
 
      'Administrator-3',
 
      1
]);

It looks like you did some editing to the original code:
Code:
<xen:if is="{$xenOptions.googleAnalyticsWebPropertyId}"><script>
 
    var _gaq = [['_setAccount', '{$xenOptions.googleAnalyticsWebPropertyId}'], ['_trackPageview']];
    !function(d, t)
    {
        var g = d.createElement(t),
            s = d.getElementsByTagName(t)[0];
        g.async = true;
        g.src = ('https:' == d.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        s.parentNode.insertBefore(g, s);
    }
    (document, 'script');
 
    </script><xen:comment><!--Adapted from http://mathiasbynens.be/notes/async-analytics-snippet--></xen:comment></xen:if>

So can't we do something like this while keeping most of the original code in tact since it uses async (which I don't think your code does):
Code:
<xen:if is="{$xenOptions.googleAnalyticsWebPropertyId}"><script>
 
    var _gaq = [['_setAccount', '{$xenOptions.googleAnalyticsWebPropertyId}'], ['_setCustomVar',
                1,
      'Usergroup',
<xen:if is="{xen:helper ismemberof, $visitor, 1}">
      'Unregistered-1',
<xen:elseif is="{xen:helper ismemberof, $visitor, 2}" />
      'Member-2',
<xen:elseif is="{xen:helper ismemberof, $visitor, 3}" />
      'Administrator-3',
<xen:elseif is="{xen:helper ismemberof, $visitor, 4}" />
      'SuperModerator-4',
<xen:elseif is="{xen:helper ismemberof, $visitor, 7}" />
      'DietModerator-7',
<xen:elseif is="{xen:helper ismemberof, $visitor, 8}" />
      'Premium-8',
<xen:elseif is="{xen:helper ismemberof, $visitor, 34}" />
      'Retired-34',
<xen:elseif is="{xen:helper ismemberof, $visitor, 9}" />
      'VIP-9',
<xen:elseif is="{xen:helper ismemberof, $visitor, 27}" />
      'Writer-27',
<xen:elseif is="{xen:helper ismemberof, $visitor, 25}" />
      'CommunityChamp-25',
<xen:elseif is="{xen:helper ismemberof, $visitor, 47}" />
      'MOTM-47',
</xen:if>
      1], ['_trackPageview']];
    !function(d, t)
    {
        var g = d.createElement(t),
            s = d.getElementsByTagName(t)[0]; 
        g.async = true;
        g.src = ('https:' == d.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        s.parentNode.insertBefore(g, s);
    }
    (document, 'script');
 
    </script><xen:comment><!--Adapted from http://mathiasbynens.be/notes/async-analytics-snippet--></xen:comment></xen:if>

I'm not that good at coding...mainly get it by trial and error so I probably did something wrong.
 
Not sure why yours isn't working as my code looks identical.
Mine:
Code:
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXXX-X']);
  _gaq.push(['_setCustomVar',
      1,
      'Usergroup',
 
      'Administrator-3',
 
      1
]);
  _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);
  })();

Copied from your site:
Code:
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA- XXXXXXXXX-X ']);
  _gaq.push(['_setCustomVar',
      1,
      'Usergroup',
 
      'Unregistered-1',
 
      1
]);
  _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);
  })();
 
Not sure if you saw my last bit of code I asked you about, asking if this could work:
Code:
<xen:if is="{$xenOptions.googleAnalyticsWebPropertyId}"><script>
 
    var _gaq = [['_setAccount', '{$xenOptions.googleAnalyticsWebPropertyId}'], ['_setCustomVar',
                1,
      'Usergroup',
<xen:if is="{xen:helper ismemberof, $visitor, 1}">
      'Unregistered-1',
<xen:elseif is="{xen:helper ismemberof, $visitor, 2}" />
      'Member-2',
<xen:elseif is="{xen:helper ismemberof, $visitor, 3}" />
      'Administrator-3',
<xen:elseif is="{xen:helper ismemberof, $visitor, 4}" />
      'SuperModerator-4',
<xen:elseif is="{xen:helper ismemberof, $visitor, 7}" />
      'DietModerator-7',
<xen:elseif is="{xen:helper ismemberof, $visitor, 8}" />
      'Premium-8',
<xen:elseif is="{xen:helper ismemberof, $visitor, 34}" />
      'Retired-34',
<xen:elseif is="{xen:helper ismemberof, $visitor, 9}" />
      'VIP-9',
<xen:elseif is="{xen:helper ismemberof, $visitor, 27}" />
      'Writer-27',
<xen:elseif is="{xen:helper ismemberof, $visitor, 25}" />
      'CommunityChamp-25',
<xen:elseif is="{xen:helper ismemberof, $visitor, 47}" />
      'MOTM-47',
</xen:if>
      1], ['_trackPageview']];
    !function(d, t)
    {
        var g = d.createElement(t),
            s = d.getElementsByTagName(t)[0];
        g.async = true;
        g.src = ('https:' == d.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        s.parentNode.insertBefore(g, s);
    }
    (document, 'script');
 
    </script><xen:comment><!--Adapted from http://mathiasbynens.be/notes/async-analytics-snippet--></xen:comment></xen:if>

It appears to be working properly. Some of the stats are a bit down from when I used the original code. And it seems some of the usergroups aren't tracking but that could also be the small number of users in those usergroups have tracking disabled. But now I'm thinking it could also be the double digits. As per your instructions, I set it up like this:
906d59.png


I'm wondering if I should actually put the whole name in there to differentiate the usergroups. I'm not sure if 47 will fall within 4 and 7 usergroup in the tracking code, and then not actually show up in "47". So should I be doing "MOTM-47" instead of just 47? Sorry for the questions...the documentation on this stuff always confused me which is why it has taken me this long to do it.

Also, if you don't mind me asking, I set this up in the admin panel under advanced segments:
78c3d9.png

I don't see where I can just view stats for "guests vs registered". Is there a way to do this? Or what was the point of doing it this way?
 
Like I said above, that code appears to be correct.

Instead of "Containing" try "Exactly matching".

Are some of those Secondary User Groups? Not sure which one will show (Primary or Secondary) if a user is part of two usergroups.
 
Like I said above, that code appears to be correct.

Instead of "Containing" try "Exactly matching".

Are some of those Secondary User Groups? Not sure which one will show (Primary or Secondary) if a user is part of two usergroups.
2 may be secondary. I'll have to see how that works. Thanks for the help. :)

Also, is there a report that I can pull to look at the stats of Registered vs Guest?
 
Also, is there a report that I can pull to look at the stats of Registered vs Guest?
You can use it with all reports; go to the report, click "Advance Segments", and select your custom segments.

If you look at the two images at the bottom of this page you'll see how I setup my logged in and guest segments.
http://xenforo.com/community/resources/track-usergroups-in-google-analytics.726/

Since you have so many usergroups I'd suggest using "Exactly matching" instead of "Containing", when you set the segments up.


If you can't figure it out by Monday (I'll be out of town until then), we can try adding me as a temporary admin and see if I can set them up.
 
Ok, so one of my admins mentioned to put the "members-2" at the bottom of the code so that way those that do have secondary usergroups will properly show up in those usergroups in the stats. I'll see how it works. If it works, I'll put up my final code for everyone to check out and if you want to edit your original one you can (since I just modified the original async code xf comes with, which I think you are using the old gA code in yours - the non async).
 
Top Bottom