angular - 如何从角度向 Google Analytics 报告当前标题?
问题描述
我已成功将当前页面的 URL 报告给 Google Analytics,但它正在报告上一页的标题。
我看过几个教程,但没有一个讨论页面标题。
这是app.component.ts
我正在使用的文件中的代码:
constructor(public router: Router) {
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
// FIXME need to make this track the correct current title.
gtag('config', 'UA-XXXXXXXXX-X', {
page_path: event.urlAfterRedirects
});
}
});
}
我希望它报告我在页面的构造函数运行后设置的标题,但它似乎正在发送前一个标题。如果订阅提供任何有用的上下文,我的标题是根据订阅数据生成的。
解决方案
尝试在 ngOninit 中设置相同的逻辑,因为构造函数不是设置或获取属性的地方。
export class App implements OnInit{
constructor(){
//called first time before the ngOnInit()
}
ngOnInit(){
//called after the constructor and called after the first ngOnChanges()
}
}
推荐阅读
- c# - C# Linq 选择/加入自定义返回类型
- java - 解释 Java 中的 NodeJS 缓冲区 - 字节大小不匹配(NodeJS 中的加密和 Java 中的解密)
- php - Eloquent - 如果 firstOrNew 在多个条件下失败,则匹配记录中的字段
- sprite - Mapbox GL JS:如何为多个数据源使用多个精灵?
- java - 无法在 netbeans 中清理和构建 Java Web 应用程序
- java - 通过解析字符串键在地图内创建地图
- reactjs - D3JS/React - 从每个级别任意数量的子级的链表构造树?
- python-3.x - Python 中的大型 XML 文件解析
- android - 如何修复“重复类 com.google.firebase.components”?
- ios - 如何使用 iOS 13 在 iOS 设备中获取 SSID