要解决Angular angulartics2(谷歌分析)在路由变化时不更新页面的问题,您可以尝试使用Angular的路由事件来手动触发谷歌分析的页面更新。
首先,确保您已经正确安装了angulartics2库和谷歌分析。
然后,在您的根组件(通常是app.component.ts)中添加以下代码:
import { Angulartics2GoogleAnalytics } from 'angulartics2/ga';
import { Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';
@Component({
// ...
})
export class AppComponent implements OnInit {
constructor(
private angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics,
private router: Router
) {}
ngOnInit(): void {
this.router.events
.pipe(filter((event) => event instanceof NavigationEnd))
.subscribe((event: NavigationEnd) => {
this.angulartics2GoogleAnalytics.pageTrack(event.urlAfterRedirects);
});
}
}
上述代码中,我们使用了router.events
来订阅路由事件,并使用filter
操作符仅选择NavigationEnd
事件。然后,我们调用angulartics2GoogleAnalytics.pageTrack
方法,手动触发谷歌分析的页面更新。
请确保您已在app.module.ts
中正确地配置了Angulartics2GoogleAnalytics提供者和GoogleAnalyticsSettings。具体配置方法可以参考angulartics2的文档。
这样,每当路由发生变化时,谷歌分析将会更新页面信息。