Two separate script tags for Google Analytics?

前端 未结 5 1039
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 13:04

Does anyone know why Google Analytics requires two separate script tags?

Specifically, their instructions advise users to embed the following snippet of code into a web

5条回答
  •  死守一世寂寞
    2021-02-05 13:29

    The first block is actually using JavaScript to write a whole new script tag inline to replace that script tag. It is doing a check to see if you are using "https" on the page being requested and if so, to use their secure url to request the script, or your browser may display a "Portions of this page are unsecure--display secure items?" or refuse the make the call at all.

    If the second script tag were included in the first, it would be blown away and/or not be a well-formed script tag and your code would have to intermix with theirs.

    This way, you can just do all your calls to trackPageView and set properties, etc. in your own blocks cleanly and still work properly over http and https.

    So, when the page renders, the DOM will look like this after the first script executes (regular http):

    
    
    
    
    

    And this (https):

    
    
    
    
    

    More details: http://code.google.com/apis/analytics/docs/tracking/gaTrackingOverview.html

提交回复
热议问题