Best place to insert the Google Analytics code

JavascriptHtmlWordpressGoogle AnalyticsAnalytics

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

As google says:

> 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

Solution 3 - Javascript

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
});

Solution 4 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarky34View Question on Stackoverflow
Solution 1 - JavascriptYahelView Answer on Stackoverflow
Solution 2 - JavascriptAmrView Answer on Stackoverflow
Solution 3 - JavascriptSparkupView Answer on Stackoverflow
Solution 4 - Javascriptcitizen connView Answer on Stackoverflow