角度和谷歌分析整合=> ga不是一个功能

lan*_*tro 8 javascript analytics google-analytics angular

我正在做一个角度(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{
        console.log(window);
        return window;
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误: ERROR TypeError: windowRef_1.WindowRef.get(...).ga is not a function

当我这样做时,console.log(WindowRef.get());我可以在窗口中看到ga函数,但是当我尝试使用它时它仍然显示上一个错误. 在这里这里

我真的不明白我使用这种方法来检索条带功能,它工作得很好.

祝你有美好的一天 :)

小智 10

我尝试将Google Analytics集成到我的Angular 4 App中时遇到了类似的问题.

我的诀窍是将谷歌分析代码从AppComponent的构造函数移动到ngAfterViewInit()生命周期钩子,以确保视图首先完全初始化.

这是我得到的代码:

index.html(和你一样):

<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', 'some code', 'auto');
</script>
Run Code Online (Sandbox Code Playgroud)

app.component.ts:

import {AfterViewInit, Component, Inject, PLATFORM_ID} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';
import {NavigationEnd, Router} from '@angular/router';

// declare google analytics
declare const ga: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {

  constructor(@Inject(PLATFORM_ID) private platformId: Object,
              private router: Router) {}


  ngAfterViewInit(): void {
    this.router.events.subscribe(event => {
      // I check for isPlatformBrowser here because I'm using Angular Universal, you may not need it
      if (event instanceof NavigationEnd && isPlatformBrowser(this.platformId)) {
        console.log(ga); // Just to make sure it's actually the ga function
        ga('set', 'page', event.urlAfterRedirects);
        ga('send', 'pageview');
      }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

如果这对您有用,请告诉我.祝你今天愉快!:)