Where to put Analytics Code

37,866 views
Skip to first unread message

Sudipta Banerjee

unread,
Oct 12, 2013, 11:55:05 AM10/12/13
to digital-analyti...@googlegroups.com
Hello

In course video it says to put tracking code "just before the closing </head> tag" but in Google Analytics it says setup after body tag "Then, add the following line to each template page immediately after the opening <body> tag".

What is the best practice to follow.

Best Regards
Sudipta Banerjee

Shehan Silva

unread,
Oct 12, 2013, 12:16:30 PM10/12/13
to digital-analyti...@googlegroups.com

Hi, few years ago we used to paste the GA code before the </body> tag. Now we are inserting/pasting just before the </head> tag. That is the correct way.

As I know Google Tag Manager code should be paste just after the <body> tag.

Emilio Olivares

unread,
Oct 12, 2013, 10:43:31 PM10/12/13
to digital-analyti...@googlegroups.com
I've been using the "just before the </head> tag" advice and it's working well. That being said this doesn't correspond with general best practices of where to place javascript code in an html document as seen in this stackoverflow.com question: http://stackoverflow.com/questions/436411/where-is-the-best-place-to-put-script-tags-in-html-markup

Any additional information from the analytics team on this would be great.

Best!
Emilio

Analytics Academy Course Staff

unread,
Oct 15, 2013, 11:32:28 AM10/15/13
to digital-analyti...@googlegroups.com
Hello Sudipta,

In practice, the Google Analytics code will work regardless of where you put it on the page. We recommend inserting it in the <head> section or after the opening <body> tag if possible. This is because the higher up it is inserted in your page source code, the sooner it will get executed after the user lands on your page. If you put your Analytics tracking code at the bottom of your code, it will be the last piece of script to get executed. Sometimes, if the user navigates to another page before the page has finished loading, the tag may not have time to fire and the data you collect will be less accurate. That's why the best practice is to put the tag towards the top of your page code. 
Also - keep in mind that the tag is designed to work asynchronously, therefore, it will have no impact on your page load time.
Hope that clarifies the issue.

Lothaire from the Google Analytics Academy Staff
Reply all
Reply to author
Forward
0 new messages