In the following code I am attempting to:-
- Define google analytics on the page
- Add a jquery click event if the page has a certain query string and characters in the domain
- The click event invokes a google analytics tracking event
- I also have code checking for query strings and values within a domain
What is not working:-
- The third item, which is invoking google analytics tracking event, I seem to be hitting it but nothing is being returned into my GA account.
What I have tried:-
- I have triple checked that information for
_gaq_.pushis correctly linking to correct account. - Used firebug and IE9 developer tools to follow javascript and analyze what is going on. This is how I know I am hitting the event tracking when my scenarios are true.
- Tried in a isolated environment like jsfiddle, will not give link to its because I can’t actually give company information out. It doesn’t work in js fiddle. ( mainly because jsfiddle can’t authenticate with my ga account (domains are different).
-
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']); } } }); } });
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: