如何从 angular 向 Google Analytics 报告当前标题?

How to report current title to Google Analytics from angular?

我已成功将当前页面的 URL 报告给 Google Analytics,但它报告的是上一页的标题。

我看过几个教程,其中 none 讨论了页面标题。

这是我正在使用的 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() 
    }
 }