Skip to main content
Legend
February 22, 2018
Question

OT: Google Analytics code placement?

  • February 22, 2018
  • 5 replies
  • 2743 views

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

    This topic has been closed for replies.

    5 replies

    pziecina
    Legend
    February 22, 2018

    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.

    B i r n o u
    Legend
    February 22, 2018

    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

    pziecina
    Legend
    February 22, 2018

    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.

    anissa_thompson
    Community Expert
    Community Expert
    February 22, 2018

    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

    Hope this helps! Make sure to press "✔ Correct Answer" on this post if this answers your question. Happy Creating!Anissa • @anissat
    B i r n o u
    Legend
    February 22, 2018

    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)

    osgood_Author
    Legend
    February 22, 2018

    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.

    B i r n o u
    Legend
    February 22, 2018

    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

    Teodor K
    Participating Frequently
    February 22, 2018

    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

    ---DMXzone | Wappler
    osgood_Author
    Legend
    February 22, 2018

    Ok, l'll toss a coin.

    Thanks for the information guys.

    BenPleysier
    Community Expert
    Community Expert
    February 22, 2018

    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.

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    osgood_Author
    Legend
    February 22, 2018

    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.