Angular 有没有一种方法可以通过谷歌分析追踪用户在页面上花费的时间(角度)

Angular 有没有一种方法可以通过谷歌分析追踪用户在页面上花费的时间(角度),angular,google-analytics,analytics,Angular,Google Analytics,Analytics,我有一个Angular应用程序,我想使用google analytics跟踪用户在每个页面上花费的时间。最好的方法是什么?(或者可能有其他替代Google Analytics的方法可以提供此功能?由于Angular是SPA,您需要在Google Analytics上手动设置页面。为此,您在路由更改上设置了侦听器 需要类似的东西: private subscribeToRouteChanges(): void { this.router.events .pipe(filter

我有一个Angular应用程序,我想使用google analytics跟踪用户在每个页面上花费的时间。最好的方法是什么?(或者可能有其他替代Google Analytics的方法可以提供此功能?

由于Angular是SPA,您需要在Google Analytics上手动设置页面。为此,您在路由更改上设置了侦听器

需要类似的东西:

 private subscribeToRouteChanges(): void {
    this.router.events
      .pipe(filter(event => event instanceof NavigationEnd))
      .subscribe((event: NavigationEnd) => this.sendPageDataToGoogleAnalytics(event.urlAfterRedirects));
  }

  public sendPageDataToGoogleAnalytics(url: string): void {
   //need a title for each page which will be shown at GA dashboards 
   //you can setup your own service or static function to return a title for each route registered
    const pageTitle = this.pageTitleService.getPageTitle(url);   
    if (pageTitle) {
      (window as any).ga('set', 'page', url);
      (window as any).ga('set', 'title', pageTitle);
      (window as any).ga('send', 'pageview');
    }
  }