【发布时间】:2018-04-09 17:13:58
【问题描述】:
我是 Google Analytics(分析)的新手,所以我继续将gtag.js 代码添加到我的 HTML 页面标题中,它非常适合初始页面加载。但是,我使用 React 和 react-router 来动态更改 URL,当我的 SPA 中发生 URL 更改时,Google Analytics 不会获取活动页面中的更改或新点击。
我找到的答案是ga.js,而不是gtag.js。我尝试在其他答案中使用 URL 侦听器调整 Google's documentation suggestion here 以满足我的需要。但尽管代码在动态页面加载时运行,但没有任何内容发送到 GA。
window.gtag && history.listen((location)=>{
console.log('>>>', location.pathname)
gtag('config', 'GA_TRACKING_ID', {'page_path': location.pathname});
// window.ga('set', 'page', location.pathname); //--> this would have worked with ga.js
// window.ga('send', 'pageview', location.pathname);
});
【问题讨论】:
标签: javascript google-analytics google-tag-manager