简体   繁体   English

在 Google 跟踪代码管理器和 Google Analytics 4 (GA4) 中手动触发 page_view 的规范方法是什么?

[英]What is the canonical way to manually fire page_view in Google Tag Manager and Google Analytics 4 (GA4)?

My website is a single page app (SPA) that never changes browser history or the page title.我的网站是一个单页应用程序 (SPA),从不更改浏览器历史记录或页面标题。 Therefore, it's my understanding that I have two options if I want to capture page_view events:因此,据我了解,如果我想捕获 page_view 事件,我有两种选择:

  1. Add browser history and a unique title for every virtual page.为每个虚拟页面添加浏览器历史记录和唯一标题。
  2. Manually fire a page_view in SPA code.在 SPA 代码中手动触发 page_view。

I'm going with option 2.我选择选项 2。

I've read numerous articles on how to do this, and they tend to converge on similar advice, but they're always a little different from each other.我已经阅读了很多关于如何做到这一点的文章,它们倾向于集中在类似的建议上,但它们之间总是有一点不同。 This is probably because most were written soon after GA4's release.这可能是因为大多数是在 GA4 发布后不久编写的。 After trying most of the steps in these articles, I'm not convinced I've set up virtual pageviews correctly.在尝试了这些文章中的大部分步骤后,我不确定自己是否正确设置了虚拟综合浏览量。 Unfortunately, the official documentation provides little guidance :不幸的是,官方文档提供的指导很少

Alternatively, you can always explicitly set page parameters when sending a page_view event to ensure accuracy and clarity.或者,您始终可以在发送 page_view 事件时明确设置页面参数,以确保准确性和清晰度。

When it comes to the official documentation, I've only been able to find guidance on doing this with gtag.js , but I'm not using that.关于官方文档,我只能找到有关使用 gtag.js 执行此操作的指导,但我没有使用它。 I'm using Google Tag Manager (GTM).我正在使用 Google 跟踪代码管理器 (GTM)。 Here are the steps I've taken and my rationale for taking them (besides an article telling me to do so).以下是我采取的步骤以及我采取这些步骤的理由(除了一篇告诉我这样做的文章)。 After this list, I'll explain why I think it's not working correctly.在此列表之后,我将解释为什么我认为它无法正常工作。

  1. Login to GTM登录GTM
  2. Create a Google Analytics GA4 Configuration but uncheck "Send a page view event when this configuration loads".创建 Google Analytics GA4 配置,但取消选中“加载此配置时发送页面查看事件”。 I do this because, if I'm going to manually send page_view events, I don't want this configuration to send a duplicate.我这样做是因为,如果我要手动发送page_view事件,我不希望此配置发送重复项。
  3. Create a custom event trigger named "Page Loaded".创建一个名为“Page Loaded”的自定义事件触发器。 This allows me to control when I fire this trigger.这使我可以控制何时触发此触发器。 There's nothing special about it yet.目前还没有什么特别之处。 Here's how it looks:这是它的样子: 在此处输入图像描述
  4. On my SPA, I add this line above the GTM tag in the <header> :在我的 SPA 上,我在<header>中的 GTM 标记上方添加了这一行:
     <script> window.dataLayer = window.dataLayer || []; </script> <!-- Google Tag Manager -->
  5. Elsewhere in my code, I manually use that dataLayer to fire my trigger:在我的代码的其他地方,我手动使用该dataLayer来触发我的触发器:
     dataLayer.push({ 'event': 'Page Loaded', 'page_url': ..., 'page_title': ..., });
  6. Back in GTM, I create data layer variables for those two page_* fields:回到 GTM,我为这两个page_*字段创建了数据层变量: 在此处输入图像描述
  7. I create a page_view tag that reacts to this trigger and uses those variables.我创建了一个page_view标签来响应这个触发器并使用这些变量。 在此处输入图像描述
  8. I click the preview button to see how it's working.我单击预览按钮以查看它是如何工作的。 在此处输入图像描述

Now, as far as I can tell, this tag is firing at the right time and passing all the right information.现在,据我所知,这个标签在正确的时间触发并传递了所有正确的信息。 But here's why I think there's something wrong:但这就是我认为有问题的原因:

If I view my other tag, the google analytics hit information has Page Location and Page Title values of the actual page, not the virtual page (aka the custom event I created).如果我查看我的其他标签,谷歌分析点击信息具有实际页面的Page LocationPage Title值,而不是虚拟页面(也就是我创建的自定义事件)。 This other tag's trigger looks like this:这个其他标签的触发器看起来像这样:

在此处输入图像描述

The tag looks like this:标签如下所示:

在此处输入图像描述

And the Google Analytics Hit looks like this (on the Tag Assistant page): Google Analytics Hit 看起来像这样(在 Tag Assistant 页面上):

在此处输入图像描述

Those red arrows have the value of the actual page.那些红色箭头具有实际页面的值。

Is there some step/configuration I'm missing?我缺少某些步骤/配置吗? I would expect all the tags to use the Page Title of my page_view tag.我希望所有标签都使用我的page_view标签的Page Title

Well, your newly made Event tag isn't a Configuration tag , and you haven't set your Configuration tag 's page title/location to the values you wish.那么,您新制作的Event tag不是Configuration tag ,您还没有将Configuration tagpage title/location设置为您想要的值。 Since all your other events only refer to Configuration tags , they're bound to inherit its default settings , not what you set up in your Event tag .由于所有其他事件仅引用Configuration tags ,它们必然会继承其默认设置,而不是您在Event tag中设置的内容。

If you want to change the page_title and page_location for all your events, my thoughts are that you should either:如果您想更改所有活动的page_titlepage_location ,我认为您应该:

  1. Set page_title and page_location parameter values in your Configuration tag 's Fields to Set menu, not in an Event tagConfiguration tag的“要设置的字段”菜单中设置page_titlepage_location参数值,而不是Event tag

-> By doing so all your events' page title and locations will be set to whatever value you wish -> 通过这样做,您所有活动的page title and locations都将设置为您希望的任何值

-> Refer to https://support.google.com/analytics/answer/11262438 -> 参考https://support.google.com/analytics/answer/11262438

  1. Manually modify page_title and page_location via Configuration > Modify events menu通过Configuration > Modify events菜单手动修改page_titlepage_location

-> Refer to https://support.google.com/analytics/answer/10085872?hl=en&ref_topic=9756175#zippy=%2Cin-this-article and https://support.google.com/analytics/answer/11091026?hl=en#zippy=%2Cin-this-article -> 参考https://support.google.com/analytics/answer/10085872?hl=en&ref_topic=9756175#zippy=%2Cin-this-articlehttps://support.google.com/analytics/answer/11091026 ?hl=en#zippy=%2Cin-this-article

By the way, I recommend tagging only what you need with different tags for each purpose.顺便说一句,我建议只标记您需要的内容,并为每个目的使用不同的标签。 If you capture too much data, GA will sample it, thus making it useless.如果您捕获了太多数据,GA 将对其进行采样,从而使其无用。 Also, using different tags for each purpose makes it easier to modify the data you wish to collect according to the circumstances.此外,为每个目的使用不同的标签可以更容易地根据情况修改您希望收集的数据。

Right, that's cuz either the fields aren't being inherited from the settings variable (I have noticed that behavior in GA4 before) or the values of your DL variables are not set at that point (which is unlikely).是的,这是因为这些字段不是从设置变量继承的(我之前注意到 GA4 中的行为),或者你的 DL 变量的值没有在那时设置(这不太可能)。

An obvious fix for it would be just adding your fields to the click tag and be done with it.一个明显的解决方法是将您的字段添加到点击标签并完成它。

The way I do GA4 tags is by making one single tag.我做 GA4 标签的方法是制作一个标签。 For everything.对于一切。 All its content are variables, including the name of the event.它的所有内容都是变量,包括事件的名称。 And all the logic for it is either in regex lookup tables or in CJS.它的所有逻辑要么在正则表达式查找表中,要么在 CJS 中。 Or in both: CJS that uses rLUTs.或两者兼有:使用 rLUT 的 CJS。

Now it may seem complicated and overengineered, but now the size of your GA4 set up is small (remember: the size of the container is limited), it's easy to manage if you love your JS (all logic is in one place) and you don't need to iterate through all your dimensions every time you need a new event to fire.现在它可能看起来很复杂和过度设计,但是现在你的 GA4 设置的大小很小(记住:容器的大小是有限的),如果你喜欢你的 JS(所有逻辑都在一个地方)并且你很容易管理每次需要触发新事件时,都不需要遍历所有维度。

So I basically treat a GA4 event tag as a config variable.所以我基本上将 GA4 事件标签视为配置变量。 In your case, you can even merge the pageview and the click events into one.在您的情况下,您甚至可以将综合浏览量和点击事件合并为一个。

Also, not having history changes is a really poor practice and I would switch the site engine completely.此外,不更改历史记录是一种非常糟糕的做法,我会完全切换站点引擎。 It will cause dramatic issues in other places, like SEO.它会在其他地方引起严重的问题,比如 SEO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有 Google Tag Manager 的情况下以编程方式将事件发送到 GA4? - How to programatically send an event to GA4 without Google Tag Manager? ga() function 谷歌分析在谷歌标签管理器中作为标签实现 - ga() function Google Analytics implemented as a tag in Google Tag Manager 带有GA的Google跟踪代码管理器 - Google Tag Manager with GA 如何在 Google Analytics(分析)4 (GA4) 媒体资源中添加引荐排除项 - How to add referral exclusions in Google Analytics 4 (GA4) properties 是否仍然可以在 Google Analytics 4 (GA4) 中匿名化用户的 IP? - Is it still possible to anonymize the user's IP in Google Analytics 4 (GA4)? 使用新的 Google Analytics 4 (GA4) 服务器端进行跟踪 - Tracking with the new Google Analytics 4 (GA4) server-side Google Analytics Data API (GA4) 如何使用runRealtimeReport function? - Google Analytics Data API (GA4) How to use runRealtimeReport function? Google跟踪代码管理器包括旧版分析(GA)和通用分析(UA)脚本 - Google Tag Manager includes both legacy Analytics (GA) and Universal Analytics (UA) scripts Google Analytics(不使用标签管理器),发送带有散列 URL 和 AJAX 的页面视图,SPA 类型 web 应用程序 - Google Analytics (not with Tag manager), send page view with hashed URL and AJAX, SPA type web app Google跟踪代码管理器虚拟页面视图 - Google Tag Manager Virtual Page View
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM