Debugging Google Analitycs tracking codes
All the sites that I work on, have google analitycs tracking code, but how can I debug them to see if it's working?
I used to put a "console.log" line below the tracking code:
__ga.push(["trackEvent","category","event","label"]);
console.log("tracking is working");
But I figured out a better way to do this. If I am working on development or test environment, I override the "__ga.push" method. Something like this:
var _gaq = _gaq || [];
var _url = window.location.href.toString();
var _regex = /(development|test)/g;
(function()
{
//Environment is Development or Test
if(_regex.test(_url))
{
window._gaq.push = function(array)
{
console.log(array.join(" : "));
}
}
else
{
_gaq.push(['_setAccount', 'UA-36379912-1']);
_gaq.push(['_trackPageview']);
(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);
})();
}
})();Written by Giulian Drimba
Related protips
1 Response
Very good, but if you want more detailed information about the "tracks", this Chrome's plugin helps too:
https://chrome.google.com/webstore/detail/google-analytics-debugger/jnkmfdileelhofjcijamephohjechhna?hl=en
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#