Question

I have this code in my product page after an item has been added to cart:

<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(['_setCustomVar', 1, 'Page Type', 'Product', 3])
_gaq.push(['_setCustomVar', 2, 'Product Name', 'Gold Bracelet', 3]);
_gaq.push(['_trackEvent', 'Product Page', 'Added To Basket', 'Gold Bracelet', , 0]); // this is the line that isn't working.

        _gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
        _gaq.push(['_trackPageview']);


//]]>
</script>

It seems the pageview is tracked, the two custom variables, but not the _trackEvent. Like I said in the title, if I copy and paste this code into Chrome Dev Console, the events show up just fine. I added an alert() to the code that returns the _trackPageview and it worked, so the JS is definitely getting parsed.

Any idea what i'm doing wrong?

Thank you

Was it helpful?

Solution

If the opt_value parameter isn't an integer (or undefined), the event won't be recorded.

Do you want this event to affect bounce rate calculations? If so, the opt_noninteraction can be left off since the default value is false:

 _gaq.push(['_trackEvent', 'Product Page', 'Added To Basket', 'Bodysuit...']);

If you don't want the event to affect the bounce rate, use:

_gaq.push(['_trackEvent', 'Product Page', 'Added To Basket', 'Bodysuit...', undefined, true]);

I missed this in my first look at your code -- since _trackEvent is before _setAccount, the data it sends get's recorded to a default account (something like 'UA-XXXXX-X').

Move the _trackEvent call anywhere after _setAccount.

Also, since you've got both _trackEvent and _trackPageview in the same block of code, you'll want to be sure that the opt_noninteraction parameter is true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top