I'm doing an angular(4) application, but i got a problem integrating google analytics. I'm currently to add google analytics to my single page web application. But when I try to retrieve the ga function to send the new url it seems that it don't find the function.
This is the code I got:
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>
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');
}
});
}
}
windowRef.ts
export class WindowRef{
public static get(): any{
console.log(window);
return window;
}
}
I got this error: ERROR TypeError: windowRef_1.WindowRef.get(...).ga is not a function
When I do console.log(WindowRef.get()); I can see in the window the ga function but it still display the previous error when I try to use it.
here and here
I don't really understand I used this method to retrieve the stripe function and it worked pretty well.
Have a good day :)