简体   繁体   中英

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!!

You can.

The only difference is that if you put in the "footer" (ie just before the </body> tag), the browser will first load the DOM (ie 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.

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.

I'd say you can - but easier than that. Just place it in the footer and check if the Live-tracking finds visitors if it does you have the answer.

It's possible and is in fact the way many plugins do it (especially most WordPress plugins I've seen). In fact, putting all your JS at the end of the html (just before < /body> ) is recommended if those are not required during the load of the page.

I have seen people put it pretty much anywhere on the page. You can put script tags anywhere on the page; many prefer to keep them in the <head> tag, while others put them at the bottom to avoid using document.ready . The point is that putting scripts there still works. Therefore, I see no problem in putting it at the bottom of the page.

Yes, I believe that it would work - it's not recommended though.

f you have a header and footer file, why not just append your Analytics code to it? Unless every page has unique footer/header page, of course. Then I'd recommend you to take a look at MVC .

Observations have already been made here regarding the scripts placement which can be taken on a site by site basis. We run Google Analytics using Google Tag Manager and use the header code placement in the footer and have no problems with that. Google is currently rolling out Core Web Vitals which places website performance as a must have, and so optimising code delivery is really important. Just wanted to add this here as it was pertinant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM