为什么应该将Tealium utag脚本动态添加到页面?

问题描述

我遇到过tealium utag.js文档,该文档建议在以下代码段中包含该标签。基本上,它将创建一个异步脚本标签,并将其插入到文档的第一个脚本标签之前。

<script type="text/javascript">
  // some setup needed for tealium utag.js to work properly
</script>
<!-- tealium snippet to include utag.js -->
<script type="text/javascript">
  (function(a,b,c,d) {
      a='//tags.tiqcdn.com/utag/ACCOUNT/PROFILE/ENVIRONMENT/utag.js';
      b=document;c='script';d=b.createElement(c);d.src=a;
      d.type='text/java'+c;d.async=true;
      a=b.getElementsByTagName(c)[0];a.parentNode.insertBefore(d,a)})();
</script>

我想知道使用此代码段而不是直接将脚本包含在DOM中是否有任何优势/差异。

<script type="text/javascript">
  // some setup needed for tealium utag.js to work properly
</script>
<!-- Include utag.js asynchronously -->
<script type="text/javascript" src="//tags.tiqcdn.com/utag/ACCOUNT/PROFILE/ENVIRONMENT/utag.js" async></script>

解决方法

原因是推荐的方法与100%的浏览器兼容。大多数现代的浏览器都支持async选项,因此您可能可以安全地使用它,但是您的访问者可能使用较旧的浏览器。您可能要谨慎一点,并确保您支持所有用户。

https://caniuse.com/script-async