Google Analytics and Google Adsense Code Placement Order



google analytics code placement order Google Analytics and Google Adsense Code Placement Order

There have been a lot of complaints that Google Analytics does not always track Google AdSense data accurately, and there is a reason for the errors.

In order for Google Analytics to track Google AdSense data properly the code for your Google Ad Manager, AdSense Analytics Code (ASAC), Google AdSense ads, and Google Analytics code, must be placed into your web site template files in the proper order.

If you use Google Ad Manager then that code will be pasted into the your template files of your website first in between the HTML <head></head> tags as shown in the example below.

<script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js">
</script>
<script type="text/javascript">
  GS_googleAddAdSenseService("ca-pub-xxxxxxxxxxxxxxxx");
  GS_googleEnableAllServices();
</script>
<script type="text/javascript">
  GA_googleAddSlot("ca-pub-xxxxxxxxxxxxxxxx", "YOUR-Homepage-160x600");
  GA_googleAddSlot("ca-pub-xxxxxxxxxxxxxxxx", "YOUR-Homepage-300x250");
  GA_googleAddSlot("ca-pub-xxxxxxxxxxxxxxxx", "YOUR-Homepage-336x280");
  GA_googleAddSlot("ca-pub-xxxxxxxxxxxxxxxx", "YOUR-Homepage-728x90");
  GA_googleAddSlot("ca-pub-xxxxxxxxxxxxxxxx", "YOUR-Homepage-bottom-300x250");
</script>
<script type="text/javascript">
  GA_googleFetchAds();
</script>
	
	<?php wp_head(); ?>
</head>

<body>

Next you will past the AdSense Analytics Code (ASAC) right after the <body> tag. This is not required for the primary domain name, or web site, it is only required for additional domain names or web sites being tracked by Google Analytics.

<?php wp_head(); ?>
</head>

<body>

<script type="text/javascript">
window.google_analytics_uacct = "UA-xxxxxx-x";
</script>

After the AdSense Analytics Code (ASAC) you will the paste your Google AdSense ad code. The example below uses the Google Ad Manager ad code. This code will be pasted after the AdSense Analytics Code (ASAC), which means it will also come after the <body> tag, but before the </body> tag. So the code below must be pasted between the <body></body> tags.

<script type="text/javascript">
  GA_googleFillSlot("YOUR-Homepage-336x280");
</script>

Last on the list is to paste the Google Analytics code. This code needs to be pasted just before the </body> tag as shown in the example below. Make certain you are using the new ga.js tracking code, and not the legacy urchin.js tracking code, otherwise your tracking statistics will be limited. Eventually the urchin.js tracking code will be retired.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>
</html>

If all of your code is placed into your web site templates in the correct order the statistical data collected should be accurate.


Short Link - http://ngurl.me/qm Posted on September 4, 2009 at 10:01 am (PST)
Tags: | | |


One comment on “Google Analytics and Google Adsense Code Placement Order

  1. Nikolas on said:

    Hello and thank you for your useful articel.
    The instructions you gave were clear but when I checked the source code of your page, I realized that the code is not placed in the same order.
    First it’s the Analytics tracking code, then the Ad manager code and then the Adsense Analytics code.

    So what is the right order?

    Thank you in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

70,029 Spam Comments Blocked so far by Spam Free Wordpress

HTML tags are not allowed.

© Copyright Nerd Grind 2009 - 2010. All rights reserved.