开发者

why google analytics need to be load dynamically?

开发者 https://www.devze.com 2023-04-09 13:33 出处:网络
Why i need to use: <script type=\"text/javascript\" charset=\"utf-8\"> //<![CDATA[ var _gaq = _gaq || [];

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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号