Thursday, August 4, 2011

Now Track Google Analytics With AJAX Code

A few months ago Google came out with a nice function for Google Analytics that allows website publishers to do the analytics tracking via AJAX (it more or less speeds up to your site because end user's don't need to wait for the Google Analytics tracking code to download/run in order for your site to finish being rendered).

I wasn't able to find any documentation on how to track AdSense clicks when using the new code, but was able to figure it all out with some trial and error, so here's how to do it...

Replace UA-XXXXX-X with your web property ID and your domain.com with your domain and you should be good to go (there are 2 places to replace each for a total of 4 replacements)...

Code:

<script type="text/javascript">
google_analytics_uacct = "UA-XXXXX-X";google_analytics_domain_name = ".yourdomain.com";var _gaq = _gaq || [];_gaq.push(['_setAccount', 'UA-XXXXX-X'],['_setDomainName', '.yourdomain.com'],['_trackPageview']);(function() {var ga = document.createElement('script');ga.type = 'text/javascript';ga.async = true;ga.src = 'http://www.google-analytics.com/ga.js';(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);})();
</script>

I know the links of JavaScript are run together on a single line... it's intentional since that's how I do it on this site. There really is no point in having unnecessary characters in the HTML source... the whole point of going to AJAX is to make things faster, remember?

This will track your AdSense clicks within your Google Analytics accounts (assuming of course you linked your Analytics and AdSense account). Make sure the above code is somewhere in your page BEFORE the first AdSense unit if you want to track AdSense clicks (putting it into the <head> works nicely).

No comments:

Post a Comment