【问题标题】:Google Analytics/Google Ads and Content Security Policy谷歌分析/谷歌广告和内容安全政策
【发布时间】:2023-02-02 02:29:20
【问题描述】:

让 Google Analytics/Google Ads 与 CSP 一起工作并不容易。我查看了一些 SO 文章including this one,但想看看是否有另一种方法。

我找到了一个非常简单的方法in this article,并想把它贴在这里以防它对其他人有用。我会把它贴在这里作为答案。

【问题讨论】:

    标签: content-security-policy google-analytics-4 react-helmet google-ads-script helmet.js


    【解决方案1】:

    我找到了一个非常简单的方法来做到这一点in this article

    ...将 Google Analytics 代码 sn-p 移动到外部代码文件, 托管在已被您的脚本源列入许可名单的域上 定向的,例如您网站的主域。

    最初由谷歌提供的脚本是:

    <!-- Google tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-##########">
    </script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
    
        gtag('config', 'UA-##########');
    </script>
    

    我去了https://www.googletagmanager.com/gtag/js?id=UA-##########,下载了脚本,并将其添加到它的底部:

    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    
    gtag('config', 'UA-##########');
    

    我把它保存到我的网站上。我添加了这个:

    <script async src="/script-saved-to-my-website.js"></script>
    

    ...里面头部部分。 (出于某种原因,谷歌广告似乎希望它位于头部部分)。

    我按照上面文章中的描述添加了 csp 策略。

    我使用在 Google Analytics 中找到的“连接到 Google 广告”功能将 Google Analytics 4 连接到 Google Ads。注意:您必须让 Google Ads 处于专家模式,而不是智能模式,此功能才能发挥作用。

    我在 Google Ads 上设置了转化目标。

    而且...它正在工作。 :) 我可以在 Google Analytics 上看到我的统计数据,在 Google Ads 上看到我的转化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      相关资源
      最近更新 更多