Speed up Including Google Analytics

Have you ever noticed that your homepage hangs when including the Google Analytics JavaScript file? I think a few people have noticed a delay. Well, let’s try something different. Let’s create the script node dynamically using DOM methods and put a…


This content originally appeared on Zach Leatherman and was authored by Zach Leatherman

Have you ever noticed that your homepage hangs when including the Google Analytics JavaScript file? I think a few people have noticed a delay. Well, let’s try something different. Let’s create the script node dynamically using DOM methods and put a timeout on this creation so that it inserts just enough delay so that your page won’t hang (we’ll move out of the current execution path with the timeout, thus allowing your page to finish loading). The obvious benefit here is that even when the Google servers lag a little bit serving you the JavaScript file, your page will appear as if it has already finished loading. Given optimally performing Google Servers, this method will perform slower most of the time, but it shines in those rare instances where there is a bit of a delay. Give it a try, and let me know if it works any better.

Of course, remember to put near the end of your <body>, and not directly in your <head>.

<script type="text/javascript">
(function()
{
    setTimeout(function()
    {
        var node = document.createElement("script");
        node.src = 'http://www.google-analytics.com/urchin.js';
        //for SSL
        //node.src = 'https://ssl.google-analytics.com/urchin.js';
        node.type = 'text/javascript';
        document.getElementsByTagName("head")[0].appendChild(node);
        var init = setInterval(function()
        {
            _uacct = 'UA-XXXXXX-X'; // INSERT YOUR CODE HERE
            if(typeof urchinTracker != 'undefined') {
                urchinTracker();
                clearInterval(init);
            }
        }, 100);
    }, 0);
})();
</script>

Try it on my benchmark page.


This content originally appeared on Zach Leatherman and was authored by Zach Leatherman


Print Share Comment Cite Upload Translate Updates
APA

Zach Leatherman | Sciencx (2007-11-01T05:00:00+00:00) Speed up Including Google Analytics. Retrieved from https://www.scien.cx/2007/11/01/speed-up-including-google-analytics/

MLA
" » Speed up Including Google Analytics." Zach Leatherman | Sciencx - Thursday November 1, 2007, https://www.scien.cx/2007/11/01/speed-up-including-google-analytics/
HARVARD
Zach Leatherman | Sciencx Thursday November 1, 2007 » Speed up Including Google Analytics., viewed ,<https://www.scien.cx/2007/11/01/speed-up-including-google-analytics/>
VANCOUVER
Zach Leatherman | Sciencx - » Speed up Including Google Analytics. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2007/11/01/speed-up-including-google-analytics/
CHICAGO
" » Speed up Including Google Analytics." Zach Leatherman | Sciencx - Accessed . https://www.scien.cx/2007/11/01/speed-up-including-google-analytics/
IEEE
" » Speed up Including Google Analytics." Zach Leatherman | Sciencx [Online]. Available: https://www.scien.cx/2007/11/01/speed-up-including-google-analytics/. [Accessed: ]
rf:citation
» Speed up Including Google Analytics | Zach Leatherman | Sciencx | https://www.scien.cx/2007/11/01/speed-up-including-google-analytics/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.