Google标签经理在Localhost上

发布于 2025-02-01 02:54:03 字数 246 浏览 2 评论 0原文

我被要求将Google标签管理器脚本实现到React应用程序中。我已经将脚本添加到了我的HTML文件的头部和正文中。我已经以GTM的预览模式测试了该站点,并且一切似乎都可以正常工作。点击和路由更改正确跟踪。

我有疑问... Localhost的发展呢?它将在分析(我无法访问分析)上生成不必要的日志吗?也许这足以粘贴片段,仅此而已?

我找不到答案,所以这就是为什么我决定在这里问的原因。

如果有人在这个主题上有一些经验 - 请告诉我:)

I was asked to implement the Google Tag Manager scripts into the React application. I've added the scripts to head and body of my html file. I've tested the site in the Preview mode of GTM and everything seems to working fine. Clicks and route changes are tracked correctly.

I have one doubt... What about the localhost development ? Is it going to generate unnecessary logs on the analytics (which I have no access to)? Or maybe it's just enough to paste the snippets and that's it?

I can't find an answer so that's why I decided to ask here.

If someone has some experience on that topic - please let me know :)

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

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

发布评论

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

评论(1

骑趴 2025-02-08 02:54:03

原始答案

我建议您通过Datalayer变量,URL或自定义JavaScript映射Google Analytics属性ID,以根据用户是否从您的Localhost开发环境(您的UAT环境)访问您的网站(如果有的话)返回单独的属性ID )然后您的生产环境(或其他适用的环境)。

从本质上讲,您正在寻找一些说“如果URL包含“ Localhost”,返回我的开发属性ID”的内容,然后在Google Analytics(S)中使用此变量名称,而不是静态值。

是的,不幸的是,您的所有现有测试都在您的个人资料中,因为如果您配置了生产属性ID并发射了一堆事件和浏览量,它将绝对收集并作为调试体验的一部分发送到此处。尽管您占整体流量的一小部分,因此通常,这是生产应用程序的关注点。您在较大的数据集中只是几个斑点。

示例

这是您如何设置此操作以解决@shawnmclean

在Google标签管理器中的 评论的示例顶部。如果没有单击“配置”按钮并在页面组下检查并关闭右侧面板。

单击“用户定义变量”部分中的新按钮,然后从实用程序组中选择查找表。如果您现在使用GA4,则为“ Google Analytics属性ID”,例如GA4 Measurement ID。对于输入变量,请从下拉列表中选择“ {{page hostName}}”。在查找表中,我们仅指定单行 - 输入“ localhost”,以用于输入并删除您打算用于开发环境的ID中的ID。因为我们希望每当主机名不是“ localhost”时都具有值集,请选中“设置默认值”的框,然后将您的生产ID放入出现的默认值框中。单击表单后,应该看起来像以下内容:

”配置的GTM查找表变量“

从这里,您需要实际将其设置在GA配置中,以便使用它。您现在可能只有一个静态值,因此我们将用对此变量的引用来代替它。如果您使用旧的Google Analytics(Universal Analytics),它将其配置作为另一个变量。在变量中,使用Curly Brace note go GTM gtm gtm知道您要指定一个变量,而不是另一个静态值。由于您的Google Analytics(S)都应指向属性ID的此设置变量,因此无需进一步更改。

但是,如果您已将迁移到GA4迁移,则测量ID现在存储在“ Google Analytics(Google Analytics):GA4配置”标签中,因此请单击“标签”并打开此配置标签。在这里,我们将再次替换在顶部到点下的测量ID下的值,以替换我们创建的查找表,如下面的示例。

保存方法并在预览中进行测试。如果主机名是“ localhost”,则将为查找表变量而不是“ localhost”解决开发ID,它将使用生产ID作为默认值。

Original Answer

I would advise you to map separate Google Analytics property IDs by either a datalayer variable, URL or custom JavaScript to return a separate property ID based on whether users are accessing your website from either your localhost development environment, your UAT environment (if any) and then your production environment (or others as applicable).

Essentially you're looking at having something that says "if the URL contains "localhost", return my development property ID", then use this variable name in your Google Analytics tag(s) instead of a static value.

Yes, unfortunately all your existing testing is all in your profile because if you had the production property ID configured and fired off a bunch of events and pageviews, it absolutely collected and sent there as part of the debugging experience. Generally though, that's a low concern on a production app because you make up such a small portion of overall traffic; you're just a couple of blips in the larger dataset.

Example

Here's an example of how you might set this up to address the comment by @ShawnMclean

Within Google Tag Manager, open your Variables (from the left navigational column) and ensure that "Page Hostname" is listed under the Built-In Variables at the top. If it isn't click the Configure button and check it under the Pages group and close the right-hand panel.

Click the New button in the "User-Defined Variables" section and select the Lookup Table from the Utilities group. Given this a useful name like "Google Analytics Property ID" or if you're using GA4 now, "GA4 Measurement ID". For the input variable, select "{{Page Hostname}}" from the dropdown. In the lookup table, we'll only specify a single row - type in "localhost" for the input and drop in the ID you intend to use for your development environment into the value. Because we want to have a value set whenever the hostname isn't "localhost", check the box for "Set Default Value" and put your production ID into the Default Value box that appears. It should look like the following once you click off the form:

Configured GTM Lookup Table Variable

From here, you'll need to actually set it in your GA configuration so it's used accordingly. It's likely you just have a static value there now, so we're going to replace that with a reference to this variable. If you're using the older Google Analytics version, Universal Analytics, it maintained its configuration as another variable. In Variables, find your Google Analytics settings tag and replace the Tracking ID value with the name of your Lookup Table variable using the curly brace notation go GTM knows you're specifying a variable and not another static value. Because your Google Analytics tag(s) should all be pointing to this settings variable for the property ID, no further changes should be necessary.

Google Analytics - UA Variable Configuration

If, however, you've made the migration to GA4, the measurement ID is now stored in the "Google Analytics: GA4 Configuration" tag, so click over to Tags and open this configuration tag. Here, we'll again just replace the value under Measurement ID at the top to point to the Lookup Table we created as in the following example.

Google Analytics 4 - Configuration Tag

Save either approach and test out in preview. Where the hostname is "localhost", the development ID will be resolved for the lookup table variable and where it's not "localhost", it will use the production ID as the default value.

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