Search code examples
jquerygoogle-analyticsevent-tracking

Google Analytics jquery event tracking not working


I set up some Event Tracking code on a website with JQuery, by using the example provide on this website.

here is the core part of the jquery code I am using :

var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
var filePath = href;
_gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
if (jQuery(this).attr('target') != undefined && 
jQuery(this).attr('target').toLowerCase() != '_blank') {
                        setTimeout(function() { location.href = baseHref + href; }, 200);
                        return false;
                    }

But what I don't want is to have "Click-"... displayed in Google Analytics. But after removing this, and the code becoming :

_gaq.push(['_trackEvent', 'Download', extension, filePath]);

no more events are being tracked in Google Analytics.


Solution

  • After spending some time on this problem, I figured out that the extension variable :

     var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
    

    isn't a string (thanks to console.log(extension); in firebug). This can explain that Google Anyltics isn't showing any Events.

    So the solution I came up with was to change my code to the following :

    _gaq.push(['_trackEvent', 'Download', '' + extension, filePath]);
    

    After this the tracking is working properly.