Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics - Can I put script in footer?

I want to add google analytics to our website and have read some conflicting info about where to put the script tag.

Google says to put it before the closing </ head> tag: http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html

The way our site is designed, this would mean making an edit to every page. It's not that big of a deal if I need to do this, however, our site also uses header and footer include files.

These header & footer files have html code in them and fall just inside the <body> and </body> tags once the page is loaded.

It would be so much easier to just add the script to the header or the footer file. I'd just paste it in there once and it would be serving up the code on every page.

My question is: Can I do this? Can I move the script snippet inside footer include file even though it's not before the </head> tag as google suggests?

Am I asking for problems if i do it this way?

Thanks!!

like image 614
Kevin Avatar asked Jan 28 '12 17:01

Kevin


People also ask

Can you put Google Analytics code in footer?

Want to know where should you put your Google Analytics code? While it's safe to put your code in the header or footer, Google recommends you to add your code to your WordPress header.

Does Google Analytics code go in header or footer?

There are a few exceptions, but for the most part, you'll always want to put your GA tracking code immediately after the <head> tag in the header of each page of your site. If you put the code in the footer, especially if you run other scripts on your site, Analytics might not track your users accurately.

Where do I put the Google Analytics script?

You can add the Analytics tag directly to the HTML of each page on your site, or indirectly using a tag management system such as Google Tag Manager.

Can I put Google Analytics code in body?

You can put it anywhere you want on page, and ll run anywhere on the page whether it's in the head or the body. But according to Google support (Add the tracking code directly to your site) it is recommended to it in the head tag, paste it immediately before the closing </head> tag.


2 Answers

You can.

The only difference is that if you put in the "footer" (i.e. just before the </body> tag), the browser will first load the DOM (i.e. everything up to the script tag) and then start to load the script. If you put it in the header, it will try to load the script either before it loads the rest of the DOM or while it is loading it (depending on the browser).

You might feel a difference only if you have a huge page with a lot of elements, but the only difference really would be that the script would be loaded later, so if someone aborts a load of the page, the script might not get executed.

Thus, Google recommends to place it in the <head> and if you can, you should, however you also can put it somewhere else if that makes life easier.

like image 72
MMM Avatar answered Sep 21 '22 12:09

MMM


I would recommend saving the analytics code in a file and including it in your footer or header - Example: . I've recently run into some issues with Firefox and IE regarding the page load and analytics. It loads the page to the point where the analytics code is present and then the page hangs indefinitely. I can refresh the page and it loads fine, but for some reason it's happening and there's very little info out there regarding solutions. So, in my case I've included the code in the Footer or of the page at the bottom. This way, in regards to your site visitors, they'll likely not even notice because the rest of your page will load and once they click on a link it shouldn't hang again.

like image 33
Natalie K Avatar answered Sep 21 '22 12:09

Natalie K