我正在做一个角度(4)应用程序,但我在整合谷歌分析时遇到了问题.我目前正在将谷歌分析添加到我的单页Web应用程序中.但是当我尝试检索ga函数以发送新的url时,它似乎找不到该函数.
这是我得到的代码:
index.hbs
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'My-key', 'auto');
</script>
Run Code Online (Sandbox Code Playgroud)
app.component.ts
import { Component, OnInit } from '@angular/core';
import {NavigationEnd, Router} from "@angular/router";
import {WindowRef} from "./social/windowRef";
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
user: User;
private currentRoute: string;
constructor(private misc: MiscService, public router: Router) {
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
console.log(event.urlAfterRedirects);
WindowRef.get().ga('set', 'page', event.urlAfterRedirects);
WindowRef.get().ga('send', 'pageview');
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
windowRef.ts
export class WindowRef{
public static get(): any{ …Run Code Online (Sandbox Code Playgroud)