Where’s the best place to insert the Google Analytics code in WordPress, header or footer? I prefer footer, because I wanted my site to load faster by reducing the number of scripts in the header, but can it work even if the script is in the footer?

- 18,263
- 7
- 55
- 75

- 1,763
- 2
- 11
- 4
-
4This question reminds me of this: "Tell we where is fancy bred, in the heart () or in the " - Shakespeare (The Merchant of Venice) – eharo2 Aug 06 '15 at 23:30
4 Answers
Google used to recommend putting it just before the </body>
tag, because the original method they provided for loading ga.js
was blocking. The newer async syntax, though, can safely be put in the head with minimal blockage, so the current recommendation is just before the </head>
tag.
<head>
will add a little latency; in the footer will reduce the number of pageviews recorded at some small margin. It's a tradeoff. ga.js
is heavily cached and present on a large percentage of sites across the web, so its often served from the cache, reducing latency to almost nil.
As a matter of personal preference, I like to include it in the <head>
, but its really a matter of preference.

- 37,023
- 22
- 103
- 153
-
10To add to this, adding it in the also comes with the added benefit of being able to verify Google Search Console without any other manual verification methods. – williamvicary Aug 28 '15 at 23:08
-
4now the current recommendation is: `right after the opening tag`. The source is on the same page as someone else linked - https://support.google.com/analytics/answer/1008080?hl=en#GA – JackLeo Oct 14 '18 at 16:03
-
It's worth mentioning that the page linked by JackLeo now states two _other_ locations, depending on whether the site is static or dynamic on the backend. – isherwood Aug 28 '20 at 16:05
Paste it into your web page, just before the closing
</head>
tag.One of the main advantages of the asynchronous snippet is that you can position it at the top of the HTML document. This increases the likelihood that the tracking beacon will be sent before the user leaves the page. It is customary to place JavaScript code in the
<head>
section, and we recommend placing the snippet at the bottom of the<head>
section for best performance

- 4,809
- 6
- 46
- 60
-
Worth mentioning that they now recommend placing it "immediately after the opening `` tag" on new Google Analytics accounts. (Though their [help files](https://support.google.com/analytics/answer/1008080?hl=en) still recommend placing it before the closing ``. The lesson: I think it's pretty up to personal preference and ideal flow in your code. – Jacob Ford May 21 '16 at 15:17
-
2
-
It is mentioned in the Dutch version in Analytics where they 'explain' how to implement with PHP: `Voeg de volgende regel direct na de openingstag toe aan elke sjabloonpagina` – Brainfeeder Dec 28 '16 at 08:08
If you want your scripts to load after page has been rendered, you can use:
function getScript(a, b) {
var c = document.createElement("script");
c.src = a;
var d = document.getElementsByTagName("head")[0],
done = false;
c.onload = c.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
b();
c.onload = c.onreadystatechange = null;
d.removeChild(c)
}
};
d.appendChild(c)
}
//call the function
getScript("http://www.google-analytics.com/ga.js", function() {
// do stuff after the script has loaded
});
-
5This could significantly affect your page analytics though, negatively. – LocalPCGuy May 31 '14 at 03:56
Yes, it is recommended to put the GA code in the footer anyway, as the page shouldnt count as a page visit until its read all the markup.

- 15,300
- 3
- 58
- 80
-
5What about if analyst want to track the source which let the user arrive at the page? User could leave the page even before loading the complete could be because of many factors and atleast source tracking should be always done. So, tag is recommended place. – iMatoria Jul 16 '14 at 16:49