Anicho
Anicho

Reputation: 2667

Google analytics event tracking failing to feedback?

In the following code I am attempting to:-

What is not working:-

What I have tried:-

Upvotes: 1

Views: 504

Answers (1)

mike
mike

Reputation: 7177

Google Analytics _trackEvent (and _trackPageview, etc) work by making a tracking pixel request from the analytics server. If the click results in loading a new page to the same window before the tracking request has completed, you can end up with missing data, or only tracking some of the data.

The following code adds a slight delay before following the link:

var delayLink = function(e, url) {
  e.preventDefault();
  setTimeout(function(){location.href = url}, 150);
};

if (querystring["utm_expid"] != null) {
    $('a').click(function (e) {
        if ($(this).attr("href") != 'undefined' && $(this).attr("href") != null) {
            if ($(this).attr("href").toLowerCase().indexOf("keyword") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword2") >= 0) {
                _gaq.push(['_trackEvent', 'eventCategories', 'eventAction', 'eventLabel']);
                if (this.target != '_blank') delayLink(e, this.href);
            } else if (($(this).attr("href").toLowerCase().indexOf("keyword") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword2") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword3") >= 0) || ($(this).attr("href").toLowerCase().indexOf("keyword4") >= 0)) {
                _gaq.push(['_trackEvent', 'eventCategories', 'eventAction', 'eventLabel']);
            if (this.target != '_blank') delayLink(e, this.href);
            }
    }
    });
}

Upvotes: 3

Related Questions