Thanu
Thanu

Reputation: 2501

Google Analytics Custom Variable Script not working

I have following script in my web site to push a custom variable to Google analytics. But even after 5 days this data is not appear in analytics. Can anyone notice something that I'm doing wrong here?

<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type="text/javascript">
//<![CDATA[
(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';
   (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
                })();

    var _gaq = _gaq || [];

    _gaq.push(['_setAccount', 'xxxxxxxxxx']);
    _gaq.push(['_trackPageview']);

    _gaq.push(['_setCustomVar', 1, 'Product SKU','Test Data', 3]);
        //]]>

</script>
<!-- END GOOGLE ANALYTICS CODE -->  

Additional info : This script is from Google Analytis module for Magento, I'm over writing the _toHTML method in GA.php

Upvotes: 0

Views: 362

Answers (1)

carlsoja
carlsoja

Reputation: 324

The line that actually sets the custom variable comes after your _trackPageview() call, so the custom variable is never actually sent to the Analytics servers. You need to add either a _trackPageview() or _trackEvent() call after you set your custom variable(s).

_gaq.push(['_setAccount', 'xxxxxxxxxx']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setCustomVar', 1, 'Product SKU','Test Data', 3]);
_gaq.push(['_trackEvent', 'Tow Truck', 'go', '-', 0, true]);

Here's some additional reference material on this: http://www.lunametrics.com/blog/2011/12/30/google-analytics-custom-variables-working/

Upvotes: 1

Related Questions