如何在单页应用程序中的Google Analytics(分析)中触发页面查看事件? [英] How to trigger a page view event in Google Analytics on a Single Page Application?

查看:82
本文介绍了如何在单页应用程序中的Google Analytics(分析)中触发页面查看事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据单页应用程序上的您是在用户首次加载应用时加载Google标记脚本的,然后在后续页面浏览中以编程方式更新页面路径,如下所示:

According to this on Single Page Applications you load the google tag script when the user first loads the app, then on subsequent page views you programmatically update the page path like so:

gtag('config', 'UA-1234567-89', {'page_path': '/about-us'});

我的问题是,当您调用此代码时,它也会自动发送新的页面视图吗?

My question is, when you call this code does it automatically send a new page view too?

例如,假设我的SPA的根URL为:

So for example let's say my SPA has a root URL of:

https://helloworld.com

用户单击主页上的/about-us 链接.这是否意味着将有2次网页浏览事件?

And the user clicks on the /about-us link from the homepage. Does this mean there will be 2 page view events?

  • 第一个 https://helloworld.com/
  • 然后 https://helloworld.com/about-us

还是在更新页面路径后将关于我们"页面的页面查看事件发送到GA,我还需要做其他事情吗?

Or do I need to do something else after updating the page path to send the page view event to GA for the about us page?

gtag('config', 'UA-1234567-89', {'page_path': '/about-us'});

// Call something else here to trigger new page view using updated path here?

推荐答案

如果您使用的是gtag.js,则用于实现的代码段将如下所示:

If you're using gtag.js then your code snippet for implementation will look something like this:

<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>

要防止其在加载时发送综合浏览量,您只需进行如下调整:

To prevent it from sending a pageview on load you just make the adjustment like so:

gtag('config', 'GA_MEASUREMENT_ID', {'send_page_view': false}); //replace the id with your own id

对于SPA,我建议改为使用Google跟踪代码管理器,并使用数据层事件或包含的历史记录更改触发器进行跟踪,可能会略有学习上的弯路,但我认为它将简化您的代码并使其更加实用便携式"您以后是否应该切换分析平台.

For SPAs, I'd recommend Google Tag Manager instead and use datalayer events or the included history change trigger for tracking, there might be a slight learning curve to start, but I think it'll simplify your code and make it more "portable" should you switch analytics platforms later on.

有关GTM的更多信息: https://developers.google.com/tag-manager/开发者指南

More info on GTM: https://developers.google.com/tag-manager/devguide

这篇关于如何在单页应用程序中的Google Analytics(分析)中触发页面查看事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆