开发者

Where is the best place to put Google Analytics with MVC3

I just started using Google Analytics. Reading the instructions it says that 开发者_开发技巧the best place for them to be placed is just before the end of the closing head tag:

    <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'xxx']);
        _gaq.push(['_trackPageview']);

        (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>
</head>

I thought I had read advice before that said I should place them right at the end of the document. Also I am using jQuery and javascript so would it be better to place the code so that it fires only after document ready?

Any help and advice would be much appreciated.

Thanks,


Follow Google's instructions:

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.

Google's new tracking code marks the injected <script> tag with the attribute async=true. This means that in any modern browser, the GA script will NOT block execution of other scripts while executing. You will not notice any slowdown.


I've always placed it just before the end of the body tag. That way it won't load the javascript file until the end (which means the important parts of your page load faster)

UPDATE

I'm sure that Google Analytics said to put it at the end of the body (at least way back). However, they have changed their code snippet so that it loads asynchronously on newer browsers so it shouldn't matter so much.

Most I've heard on web page optimisation has always said to put the javascript towards the end of the page because of the way most browsers deal with loading and parsing javascript (i.e. not in parallel with other external resource like the loading and rendering of, say, images)


just before the "body" tag and "just before the end of the head tag" is one and the same thing. Some people place it at the end of the body tag (if that is what you mean) to not have the script delay the loading of the page. But if I'm not mistaken the script is already asynchronous so following googles advice would be best.


In the head tag is to have it be within the standards.

Have it at the end of the body tag is so that it loads last, as it's not an essential part of your site. In head, the browser have to download it first, making the loading of the page slower.


It will work fine in both places but it should be put at the end of the page content so it won't effect page load speed see this.


Web pages loading from top to bottom on web browsers. So if you put your GA codes bottom of your page, visitors see main content of page, firstly. And then GA will loaded. So, visitors will reach main content more quickly (i'm talking about really short moments, actually ) . Because of this generally webmasters putting all JS files and GA codes, bottom of the file.

But if you want to track your links etc, you have to put your GA codes to top of your page.


Some people say to put it at the end of your code so it does not effect page loads but it loads in async anyways so it doesn't really matter. Google tells you in the head tag because it may not even be loaded if your page doesn't finish loading when your user clicks away.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜