使用react-gtm-module设置Google跟踪代码管理器同意

发布于 2025-01-15 19:49:45 字数 486 浏览 0 评论 0原文

在我的 React SPA 中,我使用 npm 模块 react-gtm-module 进行连接到GTM。我可以使用以下语法发送事件:

  window.dataLayer.push({
    event: 'calc_price_btn'
  })

但我不确定是否以及如何发送 同意更新。这个模块是否可行,或者我必须使用标准 gtag HTML 代码段? react-gtm-module 可以执行标准 gtag() 调用可以执行的所有操作吗?

In my React SPA, I use npm module react-gtm-module to connect it to GTM. I can send events, using this syntax:

  window.dataLayer.push({
    event: 'calc_price_btn'
  })

but I am not sure if and how I can send consent updates. Is it possible with this module, or will I have to use the standard gtag HTML snippet? Can react-gtm-module do everything that standard gtag() calls can?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝影如岚 2025-01-22 19:49:45

我的建议是,如果您想为您的 SPA React 应用默认启用 Google 标签同意,请不要使用 react-gtm-module。我认为目前不可能。

我在过去的 3 个小时里一直在尝试,包括使用标签助手进行调试(转到谷歌标签管理器并单击“预览”按钮,您可以在本地主机上测试您的设置)。但问题是,react-gtm-module 没有通过 API 添加默认同意,也无法更新同意值。

手动设置 google tag 并不难,它允许您使用它们的最新功能,而不依赖于文档很少的软件包。

在第 5 步中,您会发现甚至在加载 google tag 脚本之前也需要运行一个额外的脚本。它将告诉 google 标签将同意值默认设置为拒绝,稍后在您的自定义 cookie 横幅中,如果用户单击“接受”,您可以将同意设置为 true。

步骤

  1. 转到 Google 标签管理器,选择您的容器,
  2. 在概览操作中点击您的标签 ID(例如 GTM-XXXXXXX),
  3. 按照说明操作,将代码段添加到您的 index.html 页面

这 3 个步骤是在 react-gtm-module 中初始化()函数的作用

然后,

  1. 在加载 google 标签的脚本上方添加另一个代码段(在 )。此脚本将在执行任何其他 google 标签操作之前将默认同意值发送到 google 标签管理器(这就是您想要的!!)
    <!-- Google Tag Manager consent -->
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag() {
        dataLayer.push(arguments);
      }

      if (localStorage.getItem('consentMode') === null) {
        gtag('consent', 'default', {
          ad_storage: 'denied',
          analytics_storage: 'denied',
          personalization_storage: 'denied',
          functionality_storage: 'denied',
          security_storage: 'denied',
        });
      } else {
        gtag('consent', 'default', JSON.parse(localStorage.getItem('consentMode')));
      }
    </script>
    <!-- Google Tag Manager -->
    
    <!-- End Google Tag Manager -->

  1. 如果您使用 typescript 则添加 global.d.ts
declare global {
  interface Window {
    gtag: (...args: any[]) => void;
  }
}

export {};
  1. 现在您可以在代码中使用 gtag 函数。
  interface IConsentMode {
    necessary: boolean;
    marketing: boolean;
    analytics: boolean;
    preferences: boolean;
  }
  const setConsentMode = (consent: IConsentMode) => {
    const consentMode = {
      functionality_storage: consent.necessary ? 'granted' : 'denied',
      security_storage: consent.necessary ? 'granted' : 'denied',
      ad_storage: consent.marketing ? 'granted' : 'denied',
      analytics_storage: consent.analytics ? 'granted' : 'denied',
      personalization: consent.preferences ? 'granted' : 'denied',
    };
    window.gtag('consent', 'update', consentMode);
    localStorage.setItem('consentMode', JSON.stringify(consentMode));
  };

参考资料:

My advice, don't use react-gtm-module if you want to enable the google tag consent by default for your SPA react app. I don't think its possible at the moment.

I've been trying for the last 3 hours, including debugging with the tag assisant (go to google tag manager and click the Preview button, you can test your setup on localhost). But the problem is that the react-gtm-module does not add the default consent via the API and also has no way of updating the consent values.

Its not that hard to setup google tag manually and it will allow you to use their latest funcionalities without depending on a package with very little documentation

In step 5 you see that you need an extra script to run even before the google tag script is loaded. It will tell the google tag to set the consent values by default to denied, and later on in your custom cookie banner you can set the consent to true if the user clicked accept.

Steps:

  1. Go to google tag manager, select your container
  2. Click your tag id in the overview op (eg GTM-XXXXXXX)
  3. follow the instructions, add the snippets to your index.html page

These 3 steps is what the initialize() function does in react-gtm-module

Then,

  1. add another snippet above the script that loads the google tag (above <!-- Google Tag Manager -->). This script will send the default consent values to google tag manager, before any other google tag actions are taken (this is what you want!!)
    <!-- Google Tag Manager consent -->
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag() {
        dataLayer.push(arguments);
      }

      if (localStorage.getItem('consentMode') === null) {
        gtag('consent', 'default', {
          ad_storage: 'denied',
          analytics_storage: 'denied',
          personalization_storage: 'denied',
          functionality_storage: 'denied',
          security_storage: 'denied',
        });
      } else {
        gtag('consent', 'default', JSON.parse(localStorage.getItem('consentMode')));
      }
    </script>
    <!-- Google Tag Manager -->
    
    <!-- End Google Tag Manager -->

  1. add a global.d.ts if you are using typescript with
declare global {
  interface Window {
    gtag: (...args: any[]) => void;
  }
}

export {};
  1. now you can use gtag functions in your code.
  interface IConsentMode {
    necessary: boolean;
    marketing: boolean;
    analytics: boolean;
    preferences: boolean;
  }
  const setConsentMode = (consent: IConsentMode) => {
    const consentMode = {
      functionality_storage: consent.necessary ? 'granted' : 'denied',
      security_storage: consent.necessary ? 'granted' : 'denied',
      ad_storage: consent.marketing ? 'granted' : 'denied',
      analytics_storage: consent.analytics ? 'granted' : 'denied',
      personalization: consent.preferences ? 'granted' : 'denied',
    };
    window.gtag('consent', 'update', consentMode);
    localStorage.setItem('consentMode', JSON.stringify(consentMode));
  };

references:

扶醉桌前 2025-01-22 19:49:45

我缺少的是定义 gtag() 函数:

window.gtag = function(){
  window.dataLayer.push(arguments)
}

令我困惑的是 Google 文档不包含 dataLayer.push() 调用,仅包含 gtag() 调用。定义函数后,可以立即使用文档中的代码片段。

What I was missing was defining the gtag() function:

window.gtag = function(){
  window.dataLayer.push(arguments)
}

It confused me that the Google documentation did not include the dataLayer.push() calls—only the gtag() calls. After defining the function, the code snippets from the docs can be used right away.

如梦 2025-01-22 19:49:45

您实际上不必做任何特殊的编程工作。

它将更多地是 GTM 中的配置。

有许多社区模板可以管理此操作,但我会使用 Simo Ahava 的 https:/ /www.simoahava.com/custom-templates/consent-mode/

在GTM中添加新标签时搜索即可。

然后,您将能够通过更新将某些事件推送到数据层,如下图所示。

输入图片此处描述

You don't really have to do anything special programming-wise.

It'll be more of a configuration in GTM.

There are many community templates that manages this, but I would go with Simo Ahava's https://www.simoahava.com/custom-templates/consent-mode/

Just search for it when adding a new tag in GTM.

Then you'll be able to push to the datalayer with certain events with the update, shown in the image below.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文