脚本标签内的异步脚本 src,GDPR 合规性谷歌广告

我正在执行 GDPR cookie 合规性检查,并仅在用户接受 cookie 时才尝试加载 Google 广告。


我只想在 cookie 不存在时加载谷歌广告脚本。所以我想这样做:


    <script>

    if(cookieNo!="1"){

       <script async src="https://www.googletagmanager.com/gtag/js?id=AW-mycode"></script>

       window.dataLayer = window.dataLayer || [];

       function gtag(){dataLayer.push(arguments);}

       gtag('js', new Date());


       gtag('config', 'AW-mycode');

       }

     <script>

知道如何将脚本标签放在脚本标签中吗?


小唯快跑啊
浏览 132回答 2
2回答

森林海

下面是一个片段,您可以使用它向网页添加异步脚本。将其包裹在您的病情中,您就可以开始了。const gScript = document.createElement('script');gScript.src = "https://www.googletagmanager.com/gtag/js?id=AW-mycode";gScript.type = 'text/javascript';gScript.onload = () => {&nbsp; &nbsp;console.log('Script has been fully loaded');&nbsp; // initlaize anything you want that needs to be done after script has loaded&nbsp; // Below code in your case&nbsp; &nbsp;window.dataLayer = window.dataLayer || [];&nbsp; &nbsp;function gtag(){dataLayer.push(arguments);}&nbsp; &nbsp;gtag('js', new Date());&nbsp; &nbsp;gtag('config', 'AW-mycode');};gScript.async = true;gScript.charset = 'utf-8';document.getElementsByTagName('head')[0].appendChild(gScript);希望这可以帮助 :)

慕莱坞森

尝试这个:<script>&nbsp; &nbsp; if(cookieNo!="1"){&nbsp; &nbsp; &nbsp; var googleAds = document.createElement('script');&nbsp; &nbsp; &nbsp; googleAds.src = "https://www.googletagmanager.com/gtag/js?id=AW-mycode";&nbsp; &nbsp; &nbsp; document.head.appendChild(googleAds);&nbsp; &nbsp; &nbsp; //Other code}</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript