This post is strictly technical and it covers the common problems faced when one implements custom variable tracking and event tracking in Google Analytics and their solutions. I did few experiments and I have shared my findings.

I was struggling with custom variable and custom events and the bounce rate issue in Google Analytics and I spent days investigating the problem which it seems thousands of webmasters are facing, and I think I have cracked it.


Let me introduce Google Analytics first( GA for Google Analytics from here on). If you are already familiar with it, you can skip this paragraph. Google Analytics(GA) is a web traffic monitoring tool provided by Google for webmasters to track various metrics for websites like number of hits, number of unique visitors, average visit duration and so on. All of these data can be found in the GA dashboard. Usually these metrics are more than enough for small websites but more complex and bigger websites need more. These web giants have a whole bunch of analysts sitting there just to meticulously measure each and every metric of user interaction and fine tune the website for a better user experience and of'course for greater profits. One can make an account on google analytics for free and then put a simple tracking code provided by GA on each page of their website to start tracking website traffic data.

GA provides two very powerful functions to track these fine metrics, custom Events and custom Variables. As simple as it seems, these two are often mis-implemented by most people(including me) in the first go and they end up facing problems like:
1. Sudden drop in bounce rate
2. No custom variable data in GA dashboard
3. Incomplete custom variable data
4. Delayed custom variable data

Ya, it seems most of the problem is with the custom variable only but most of the problem is caused by the custom events tracking which itself keep working fine... :P

So I did these experiment with using my GA account to find out how these variables respond.

Initially I implemented simple custom variable like below(on page load), inserting the following lines at the end of the document.
_gaq.push(['_customVar', 1, 'visitor trail', '<?php echo $_uri ?>', 2]);
_gaq.push(['_customVar', 1, 'visitor Info', '<?php echo $_user_name ?>', 1]);
_gaq.push(['_customVar', 1, 'visitor Pages', '<?php echo $_user_id.'_'.$_user_name ?>', 3]);

But there was no data in the dashboard. I googled and studied on Stackoverflow that custom variables data won't be tracked on its own, a _trackPageview or a _trackEvent has to be fired after the custom variable data is set.

That is why it is recommended to set the custom variable data before the "_trackPageview" statement. But if you want to set the data later after the page has loaded, you need to trigger a new _trackPageview or a _customEvent so that GA pixel is fired and the custom variable is tracked.

Firing another _trackPageview will cause the same page to be tracked twice and hence should be never used. So I started tracking my custom variables by adding a dummy _trackEvent after the above mentioned lines. There was some data in the dashboad now. I waited for more data to come as I read many articles on how there can be some delay in the tracking data to appear.

While I was waiting for more data to appear, I observed that the bounce rate of my website fell down to zero percent from the day I started tracking the custom variables. This meant there is something wrong with the new changes in GA. I also read hundreds of questions on Stackoverflow regarding the same but none gave any useful answer except one answer which pointed to the GA official page for how to implement _trackEvent. I was looking after custom Variable implementation issue but anyway I read the whole article, and the reason was there.

The most common example of _trackEvent that we find all over the internet don't include the fifth optional parameter. This parameter tells GA weather or not to "exclude" this event in bounce rate calculation. This parameter is false by default, which means if an event is tracked using _trackEvent call, it will added to page interactions. Any page with more than one interaction have a bounce rate of zero as per GA, which kind of make sense. And since I had added/fired a trackEvent on page load, hence the two interaction by default and hence the bounce rate dropped down to zero.

So if you add a trackEvent on page load or if you don't want any event to not be included in bounce rate calculations, set the fifth non-interaction parameter to true. This true is a boolean true, don't pass a string "true" in here.

In the meanwhile I was fixing bounce rate, I was still waiting for rest of the custom variable data to appear in the dashboard. But it never appeared totally. Only the last one of the three variables that I had set, the "Visitor Pages" data was showing up. I concluded that the data once set is being overwritten by the last _setCustomVar statement( Which I now realise is already written in the documentation). During further investigation I learned that there can be only one variable in one slot( the first parameter in _setCustomVar function is the slot) So I tried setting different slot for each variable. All data is now visible.

In a single tracking event one slot may contain only one variable else only the last variable in that slot will be tracked.

To further confirm this I tested it by firing three pairs of variables and events with all custom variables in same slot. That means I set a variable in one statement followed by a _trackEvent and henceforth three times. It worked. All three variables appeared in the same slot after few minutes. I think the idea is that one tracking call can handle only 5 slots in total with each slot holding no more than one variable.

Will update on further investigation/revelation.

I wrote this post for my future reference and for anyone for whom it may turn out to be little useful. Not expecting any polls for obvious reasons.



Helpful links:

http://stackoverflow.com/questions/9843240/sudden-drastic-changes-in-bounce-rate-what-could-have-caused-this
https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide?hl=sv&csw=1
https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables

Sign In to know Author