Reputation: 2667
In the following code I am attempting to:-
What is not working:-
What I have tried:-
_gaq_.push
is correctly linking to correct account.I have tried in an isolated file within my relevant domain still no luck.
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'mydomain.co.uk']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setSiteSpeedSampleRate', 100]); // this is a new line, allowing us to see how fast all pages load
_gaq.push(['_trackPageview']); // we’ve moved this line down, as ‘setdomain’ (etc) should appear before it
(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';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
$(document).ready(function () {
var querystring = (function (a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i) {
var p = a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'));
if (querystring["utm_expid"] != null) {
$('a').click(function () {
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']);
} 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']);
}
}
});
}
});
Upvotes: 1
Views: 504
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