Search code examples
javascripthtmlgoogle-analyticsanalytics

Should I put tracking/analytical JS codes after the opening <body> tag or before the closing <body> tag?


Sometimes I was told the analytical codes should be put immediately after the opening <body> tag, while sometimes I was told the code snippet should be put before the closing <body> tag.

Does anyone have ideas about what is the main difference between these two approaches? Which one is better?


Solution

  • In fact, Google says:

    Paste your snippet (unaltered, in its entirety) into every web page you want to track. Paste it immediately before the closing </head> tag.

    Before, it was good to put it just before closing the <body> tag, because the script blocked the page loading. But now, it has the async attribute, so it doesn't really matter, but putting in the head tag will guarantee that anyone who actually has seen your page will be counted.