9

I'd like to go to the asynchronous Google Analytics script, but I'd rather not have it in the head (even though I know this is best practice).

So, is there any benefit of using the async code if it isn't in the head, and alternatively, would it be safe to place elsewhere high in the page, like just inside the opening body tag, or in a .NET user control that loads at the top of the page?

EDIT:

In the instructions Google states:

If that isn't an option, you can still put the asynchronous snippet at the bottom of the page. You can also split your snippet to retain some of the benefits of asynchronous tracking.

If I place the code there (without splitting) do I still benefit from asynchronous loading or is it negligible since it is at the bottom anyway?

0

3 Answers 3

6

Compared to having the old tracking code at the bottom of the page, having the asynchronous code at the top of the <body> should still mean that the tracking is more likely to register users who leave the page (or interact with elements on the page that are being tracked) before it has fully loaded.

2

You can put it anywhere you just get the most benefit from putting it in the header. The full instruction to add it to your web page are here - http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html.

You do want to put it inside of the head or body tags though. Just to be clear. But beyond that you can do whatever you want.

3
  • 1
    The instructions also say you can put the async code at the bottom of the page. Would that have any benefit over the old code?
    – user42
    Commented Jul 12, 2010 at 15:28
  • 1
    Google imply that you can put the code pretty much anywhere but that for most pages the optimal place is at the bottom of the <head> (code.google.com/apis/analytics/docs/tracking/asyncTracking.html)
    – pelms
    Commented Jul 12, 2010 at 15:37
  • 1
    @pelms, that is my worry, it is implied, but no explicit alternate placing (besides the bottom where the old code is) is stated.
    – user42
    Commented Jul 12, 2010 at 15:50
1

You can add your analytics code anywhere on your page. Google is smart enough to identify / register all events on your page. When you are talking about async there is no point thinking about its in-line/sync placement.

I have used it at bottom for most of my sites and have found no issues.

The main load events that you get in your google analytics account:

1) Dom load time
2) Page load time

This takes into consideration everything and are independent of where you add your code.

It might be possible(with sync code) that these time are calculated on basis of Google script position on page as in :
DOM load time - > time of firing dom load event of browser - GA script loading/event
Page load time - > time of firing window load event of browser - GA script loading/event

If this is the case then script position would play a small role in changing above time to more accurate once else i don't think it make much difference.(as placing it on top brings the page with GA prior to placing it at bottom because page loads sequentially)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.