使用react-gtm-module设置Google跟踪代码管理器同意
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的建议是,如果您想为您的 SPA React 应用默认启用 Google 标签同意,请不要使用
react-gtm-module
。我认为目前不可能。我在过去的 3 个小时里一直在尝试,包括使用标签助手进行调试(转到谷歌标签管理器并单击“预览”按钮,您可以在本地主机上测试您的设置)。但问题是,react-gtm-module 没有通过 API 添加默认同意,也无法更新同意值。
手动设置 google tag 并不难,它允许您使用它们的最新功能,而不依赖于文档很少的软件包。
在第 5 步中,您会发现甚至在加载 google tag 脚本之前也需要运行一个额外的脚本。它将告诉 google 标签将同意值默认设置为拒绝,稍后在您的自定义 cookie 横幅中,如果用户单击“接受”,您可以将同意设置为 true。
步骤:
这 3 个步骤是在
react-gtm-module
中初始化()函数的作用然后,
)。此脚本将在执行任何其他 google 标签操作之前将默认同意值发送到 google 标签管理器(这就是您想要的!!)
global.d.ts
参考资料:
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:
These 3 steps is what the initialize() function does in
react-gtm-module
Then,
<!-- 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!!)global.d.ts
if you are using typescript withreferences:
我缺少的是定义
gtag()
函数:令我困惑的是 Google 文档不包含
dataLayer.push()
调用,仅包含gtag()
调用。定义函数后,可以立即使用文档中的代码片段。What I was missing was defining the
gtag()
function:It confused me that the Google documentation did not include the
dataLayer.push()
calls—only thegtag()
calls. After defining the function, the code snippets from the docs can be used right away.您实际上不必做任何特殊的编程工作。
它将更多地是 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.