最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google analytics, why have two script blocks? - Stack Overflow

programmeradmin0浏览0评论

Why does the Google Analytics script I add to my webpage need to come in two script blocks?

<script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
    try {
        var pageTracker = _gat._getTracker("UA-xxxxxxx-xx");
        pageTracker._trackPageview();
    } catch (err) { } 
</script>

Why does the Google Analytics script I add to my webpage need to come in two script blocks?

<script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
    try {
        var pageTracker = _gat._getTracker("UA-xxxxxxx-xx");
        pageTracker._trackPageview();
    } catch (err) { } 
</script>
Share Improve this question edited Oct 7, 2010 at 18:42 Quintin Robinson 82.3k14 gold badges126 silver badges132 bronze badges asked Mar 26, 2009 at 16:51 Dead accountDead account 20k13 gold badges54 silver badges82 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 21

The first script in your quote above writes a new <script> loading tag into the document. The second block has dependencies that are loaded by that dynamically written script tag, so the browser has to have a chance to load and parse the script before the second block runs. If everything was in one block, the script loading wouldn't take place until the first script section ended.

You may now wish to use the latest update to Google Analytics. This offers performances improvements, enhances data collection and prevents tracking code errors.

发布评论

评论列表(0)

  1. 暂无评论