I have the Google Analytics tracking code in the index.html
file as well as a page view tracker in the app.component.ts
file. It tookHere is the code I have that is working.
app.component.ts
export class AppComponent {
lastRoute = location.href;
constructor(ga:any){
setInterval(() => {
if (location.href != this.lastRoute) {
this.lastRoute = location.href;
ga('send', 'pageview', this.lastRoute);
}
}, 1000);
}
}
The problem I was first having is "ga is not a function." The suggested fix was putting declare var ga:Function;
right above export class in app.component.ts
. That did not work, but putting go:any
in the constructor's input did. However, now I want to track whenever someone opens a modal. I cannot access the Google Analytic's function from any other components. I tried using ga:any
again and I am getting error's such as "cannot resolve all parameters." I just need for the component to allow me to call the ga function just like app.component.ts
does.