Static website, what's the right place for Google Analytics script?
born2run
9:58 am on Apr 23, 2021 (gmt 0)
Hi I have a static LAMP based website, which has no CMS just plain html files.
I wanted to ask what's the best place to put the Google Analytics js script in the html? In the <head> section or before </body> ? I'm using a static website. Thanks!
RhinoFish
4:18 pm on Apr 23, 2021 (gmt 0)
Fire up a GTM (Google Tag Manager) account, use GTM to optimize tag deployment and FIRING.
Pay very close attention to the part here about delaying some tags until after the page has loaded: [analyticsmania.com...]
It depends on how you define "best", if you want a faster page load, then place the script before </body> tag. But you risk missing some clicks, that is some users will partially load the page and the click a link to another page, since the tag has not loaded the initial page view will not be recorded.
On the other hand if you want more accurate stats placing the script in the head is better.
But note that the script is loaded asynchronously so waiting for the request from Google's server to return the code will not block the thread, thus the difference in speed between <head> and </body> is likely going to be marginal at best.
Personally I would (and do) included the code in the head.
born2run
11:11 am on Apr 24, 2021 (gmt 0)
Thing is I’ve found google Analytics script also slows my static website.
born2run
7:51 am on May 19, 2021 (gmt 0)
I was wondering if anyone places the code before </body> ?
robzilla
8:15 am on May 19, 2021 (gmt 0)
Yes, I always place it before </body>. Analytics isn't important enough for placement in <head>.