Why does Google Analytics dynamically inject javascript into the page

后端 未结 1 1224
小蘑菇
小蘑菇 2020-12-19 17:47

Why do I need to use:

(function() {
var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
ga.src = (\'https:\' == docu         


        
相关标签:
1条回答
  • 2020-12-19 18:49
    1. Being able to serve out http or https depending on the protocol of the parent page is one reason.

    2. As the script is injected via a script tag inserted via running JS code, it does not block page loading. This is another reason. But this can very well be achieved via the new HTML5 async attribute too (but this is not compatible with older browsers - and GA absolutely needs to be compatible with pretty much all of them)

    0 讨论(0)
提交回复
热议问题