Wordpress Logo

Tracking users by nickname on WordPress using Google Analytics

Some time ago I write article about tracking nicknames of users (from comments) on a WordPress blog with Piwik. This time I’m doing same but for Google Analytics.

I’m using Google Analytics plugin for WordPress so I’ve edited googleanalytics.php file to add some additional code for user tracking:

<script type="text/javascript">
var i,x,y,ARRcookies=document.cookie.split(";");
var comment_author = "";
for (i=0;i<ARRcookies.length;i++) {
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x.indexOf("comment_author") != -1 && x.indexOf("comment_author_email") == -1 && x.indexOf("comment_author_url") == -1) {
    comment_author = unescape(y);
  }
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-YOUR-UNIQ-NUMBER']);
_gaq.push(['_setCustomVar', 1, 'Nickname', comment_author, 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>

Source: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables?hl=pl

Leave a Reply

Your email address will not be published. Required fields are marked *