Copy link to clipboard
Copied
There seems to be conflicting information as to where abouts the <script> code should be inserted into the pages code. I've always inserted it directly AFTER the opening <body> tag but now I see on the Google information page it should be inserted directly AFTER the opening <head> tag or as close as possible.
Any views?
Os
Copy link to clipboard
Copied
The reason Google states that it should be as close to the top of your document is so that a user can be counted even if they close the page before it is fully rendered.
The other view is that it should be as close to the bottom of the document otherwise it will take longer to render the page.
My view is that, because Google (mis)uses the info for its own purpose, you should not use the Google analytics at all. Most hosts have their own analysis on the server that you can use. This sits in the background and has no effect on loading the page and uses no bandwidth.
Copy link to clipboard
Copied
Unfortunately a digital marketing company has ask for it to be included......l might just stick it after the opening body tag as l have always done when used in the past.
They want it to see what activity they get from some online ads.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Ok, l'll toss a coin.
Thanks for the information guys.
Copy link to clipboard
Copied
https://forums.adobe.com/people/Teodor+K wrote
I always place it in the end of the page, before the closing </body> tag - it works just perfectly.
I think this script doesn't have the highest priority on pages so it should be loaded last
Me too.
I put GA tracking code inside a server-side include file. If needed, I can easily move it to the <head>, <body> or wherever. But I think it's best to defer loading until the rest of the page has loaded.
Copy link to clipboard
Copied
The problem of where to place scripts is that the 'best practices' that are often quoted and used, are out of date.
The old place none essential scripts before the closing body tag, was from around 2006, (from Yahoo) which was a time when that avarage computer user was still using a computer with a processor speed in M/hz, and an internet connection measured in kb/sec.
Maybe it is time to question not just script best practice, but many other best practices in general. After all many now ignore the amount of code and assets the end user must download, but still insist on saying and using outdated practices that are mainly irrelevant now.
Copy link to clipboard
Copied
I still have a few sites with Google Analytics and decided to put them to the test using PageSpeed Insights. This is what Google says:
Eliminate render-blocking JavaScript and CSS in above-the-fold content
The strange part is that, when I place the JS near the top of my document, mention is made of the links that need to be moved, yet 'ga.js' is not mentioned.
Copy link to clipboard
Copied
That's just it Ben, I don't think anyone really knows what should and should not be done anymore.
Googles page speed, does not tell you how they are calculating the results. The last time I remember Google saying anything about how it works was about 6 years ago, and then they based the results on how IE6 would work, with no mention of how fast the connection speed was, or even how the page load time was calculated.
Even elements such as srcset will probably slow a page download time, as the browser has to calculate which image to use during rendering before it can be downloaded. Then of course there are downloadable fonts, which are not even mentioned in page speed.
Copy link to clipboard
Copied
BenPleysier wrote
I still have a few sites with Google Analytics and decided to put them to the test using PageSpeed Insights. This is what Google says:
Eliminate render-blocking JavaScript and CSS in above-the-fold content
The strange part is that, when I place the JS near the top of my document, mention is made of the links that need to be moved, yet 'ga.js' is not mentioned.
Personally I think Google talks bullcrap and always has done. It always tells YOU to do exactly the opposite to doing what its pages do. I have tested a number of Google landing pages over time and the majority do not adhere to their own bullshite.........go figure. I have zero respct for them or any large company really because most are run by zealots who think they are god, preach and evangelse, indoctrinate their staff into their way of thinking by bullying tactics and really have no more idea than the average Joe on the street.
Copy link to clipboard
Copied
osgood_ wrote
I have zero respct for them or any large company really because most are run by zealots who think they are god, preach and evangelse, indoctrinate their staff into their way of thinking by bullying tactics and really have no more idea than the average Joe on the street.
I agree.
The other item that annoys me about so called 'experts', is that very few of them have ever built more than simple sites in isolated environments.
I used to think that what I did in web development was not representitive of development in general, but over the last 2-4 years I have come to realise it is more representitive than most.
Copy link to clipboard
Copied
https://forums.adobe.com/people/Nancy+OShea wrote
https://forums.adobe.com/people/Teodor+K wrote
I always place it in the end of the page, before the closing </body> tag - it works just perfectly.
I think this script doesn't have the highest priority on pages so it should be loaded last
Me too.
I put GA tracking code inside a server-side include file. If needed, I can easily move it to the <head>, <body> or wherever. But I think it's best to defer loading until the rest of the page has loaded.
This is an old website in need of major redesign BUT fortunately it did have an include file for the header and navigation information so in this instance rather than open up dozens of pages or take a chance on find and replace it was best to include the script there which means it falls just after the opening <body> tag where Google suggests it goes as one of their 2 preferred places.
Copy link to clipboard
Copied
I completly agree with Teodor... it hasn't to be in first ressources used... before the ending body tag is just perfect... but I react on this in the way that I will be curious to know how many of you around this thread use Google for such a purpose... and how many refer to Piwik (now Mamoto)
Copy link to clipboard
Copied
I dont really understand the google analytics anyway but sometimes, not always, get asked by the client if its included on their site or if l can include it. Other than sharing the analytics link with the client l dont get involved as the data is largely confusing to me.
Copy link to clipboard
Copied
woap I understand it... just know that you can rely on the piwik alternative... it is really easy to install... but the major great point is that your datas (at least the ones from your client) doesn't run through google server, nor anyone else server than the one of your clients...
that gives all the needed datas and information that your client will need... and it's an open source project
Copy link to clipboard
Copied
If you find yourself in a situation where you must use Google Analytics, I highly recommend using the Google Tag Manager to track your GA, AdWords, and a myriad of 3rd party vendor tracking codes. It centralizes and externalizes the code placement to a few lines before your head tag, and once placed, you don't need edit your HTML; the admin interface for Tag Manager makes it easy to control everything (including code for chat, social sharing, and other apps).
If you need a quick reference, check out: Google Analytics Academy
Copy link to clipboard
Copied
Why don't you just use the async and defer attributes, (as required), that way you can place all your script tags in the head content?
That way you can also ensure that the browser finishes rendering before the script is downloaded, or that the script downloads whilst rendering completes, (without blocking) using async.
Or use defer if you are absolutly certain that the script is not required for layout.
As for the claim, (probably by Google) that the analytics will provide feedback if the page is not fully rendered/loaded, that will only happen if you are using javascript to change the layout or provide functionality, and place the script after the html code.
Copy link to clipboard
Copied
be careful because as regards the administrations, and some institutions that are slow to update their systems, async is supported only from IE10, so if you choose to place scripts in the HEAD, think of doubling with defer which exists since ie6 and has been renewed with html5
Copy link to clipboard
Copied
On the good side, in browsers that support async and providing one uses the html5 doctype, the supporting browsers do continue with rendering even if the script has not downloaded. So there is no delay in rendering unlike if async is not used, when the browser treats the js download according to the old rules, and stops rendering until the script is downloaded.
Copy link to clipboard
Copied
I think I'll just stick it right before the closing </head> tag. Once again if you Google for answers everyone has their own opinion about where it should go. Its ok at the top, its ok at the bottom.........hummm.