如何在单页应用程序的 Google 分析中触发页面查看事件?
How to trigger a page view event in Google Analytics on a Single Page Application?
根据 this 在单页应用程序上,当用户首次加载应用程序时加载 google 标记脚本,然后在后续页面视图中以编程方式更新页面路径,如下所示:
gtag('config', 'UA-1234567-89', {'page_path': '/about-us'});
我的问题是,当您调用此代码时,它是否也会自动发送新的页面视图?
例如,假设我的 SPA 有一个 URL 的根:
https://helloworld.com
然后用户点击主页上的 /about-us
link。这是否意味着将有 2 个页面浏览事件?
- 第一个
https://helloworld.com/
- 然后
https://helloworld.com/about-us
或者我是否需要在更新页面路径后执行其他操作以将页面查看事件发送到关于我们页面的 GA?
gtag('config', 'UA-1234567-89', {'page_path': '/about-us'});
// Call something else here to trigger new page view using updated path here?
如果您使用 gtag.js,那么您的实施代码片段将如下所示:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-00000000-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-00000000-1'); //modify this line
</script>
要防止它在加载时发送综合浏览量,您只需像这样进行调整:
gtag('config', 'GA_MEASUREMENT_ID', {'send_page_view': false}); //replace the id with your own id
对于 SPA,我建议改用 Google 跟踪代码管理器,并使用数据层事件或包含的历史更改触发器进行跟踪,开始时可能会有轻微的学习曲线,但我认为它会简化您的代码并使其更“便携”,以便您稍后切换分析平台。
关于 GTM 的更多信息:https://developers.google.com/tag-manager/devguide
根据 this 在单页应用程序上,当用户首次加载应用程序时加载 google 标记脚本,然后在后续页面视图中以编程方式更新页面路径,如下所示:
gtag('config', 'UA-1234567-89', {'page_path': '/about-us'});
我的问题是,当您调用此代码时,它是否也会自动发送新的页面视图?
例如,假设我的 SPA 有一个 URL 的根:
https://helloworld.com
然后用户点击主页上的 /about-us
link。这是否意味着将有 2 个页面浏览事件?
- 第一个
https://helloworld.com/
- 然后
https://helloworld.com/about-us
或者我是否需要在更新页面路径后执行其他操作以将页面查看事件发送到关于我们页面的 GA?
gtag('config', 'UA-1234567-89', {'page_path': '/about-us'});
// Call something else here to trigger new page view using updated path here?
如果您使用 gtag.js,那么您的实施代码片段将如下所示:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-00000000-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-00000000-1'); //modify this line
</script>
要防止它在加载时发送综合浏览量,您只需像这样进行调整:
gtag('config', 'GA_MEASUREMENT_ID', {'send_page_view': false}); //replace the id with your own id
对于 SPA,我建议改用 Google 跟踪代码管理器,并使用数据层事件或包含的历史更改触发器进行跟踪,开始时可能会有轻微的学习曲线,但我认为它会简化您的代码并使其更“便携”,以便您稍后切换分析平台。
关于 GTM 的更多信息:https://developers.google.com/tag-manager/devguide