如何在谷歌分析和谷歌标签管理器中更改 cookie 域

Soh*_*ail 1 cookies google-analytics google-tag-manager

下面的两组代码创建了一些 cookie,一组是 google 标签管理器,另一组是 google 分析代码。我想将 cookie 域更改为 www.example.com,而不是 .example.com 或 .www.example.com。

<!-- GTM dataLayer -->
    <script>
        dataLayer = [];
        dataLayer.push({
        });
    </script>
<!-- End of GTM dataLayer -->

<!-- Google Tag Manager -->
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
            new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
            j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
            'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
        })(window,document,'script','dataLayer','GTM-XXXXXX');
    </script>
 <!-- End Google Tag Manager -->
Run Code Online (Sandbox Code Playgroud)

    <script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-xxxxxx', 'auto');
        ga('send', 'pageview');
</script>
Run Code Online (Sandbox Code Playgroud)

Eik*_*rff 5

GTM 不会自行创建 cookie。如果您在 GTM 中有创建 cookie 的自定义标签,您需要联系相应的供应商(但并非所有营销标签都允许这样做)。

Google Analytics 中的 cookie 域可以设置为参数或通过配置对象。即对于analytics.js,你要么用你的cookie域替换“auto”

  ga('create', 'UA-xxxxxx', 'www.domain.com');
Run Code Online (Sandbox Code Playgroud)

或者您提供具有相关键/值对的配置对象:

ga('create', 'UA-xxxxxx',{
'cookieDomain':'www.domain.com',
});
Run Code Online (Sandbox Code Playgroud)

虽然您没有要求它,但在通过 gtag.js 库的“新”(实际上现在是当前)跟踪代码中,cookie 域是在配置调用中设置的(它还发送页面浏览量):

gtag('config', 'GA_TRACKING_ID', {
  'cookie_domain': 'www.domain.com',
});
Run Code Online (Sandbox Code Playgroud)

如果您通过 GTM 标签配置了 GA,您可以转到“更多设置”中的“要设置的字段”选项,单击“添加新字段”,输入“cookieDomain”作为字段名称和您的域作为值。