cetver
cetver

Reputation: 11829

why google analytics need to be load dynamically?

Why i need to use:

<script type="text/javascript" charset="utf-8">
        //<![CDATA[
        var _gaq = _gaq || [];
        _gaq.push( ['_setAccount', 'xxxxxxxxxx'] );
        _gaq.push( ['_trackPageview'] );
        _gaq.push( ['_trackPageLoadTime'] );

        (function() {
            var ga = document.createElement( 'script' );
            ga.type = 'text/javascript';
            ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName( 'script' )[0];
            s.parentNode.insertBefore( ga, s );
        })();

        //]]>
</script>

instead of

<script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>
<script type="text/javascript" charset="utf-8">
        //<![CDATA[
        var _gaq = _gaq || [];
        _gaq.push( ['_setAccount', 'xxxxxxxxxx'] );
        _gaq.push( ['_trackPageview'] );
        _gaq.push( ['_trackPageLoadTime'] );        
        //]]>
</script>

Upvotes: 1

Views: 188

Answers (1)

John Conde
John Conde

Reputation: 219874

So it can load asynchronous with the rest of the page. Previously, the GA code would block other content from loading as when JavaScript is downloaded and parsed it prevents the rest of the page from loading. When it is asynchronous, it doesn't do this. That results is faster page loading speeds and more accurate tracking.

Upvotes: 2

Related Questions