Error Implementing Google Analytics into Ionic 2

2019-03-31 05:35发布

问题:

Trying to integrate Google Analytics ( http://ionicframework.com/docs/v2/native/google-analytics/ ) into my app, followed this tutorial as the docs didn't really have any sort of implementation instructions: https://www.thepolyglotdeveloper.com/2016/03/use-google-analytics-in-an-ionic-2-android-and-ios-app/

However, when I run my app in Simulator I get the following error while compiling:

TypeScript error: app/app.ts(21,14): Error TS2339: Property 'analytics' does not exist on type 'Window'.

Any ideas?

回答1:

the sample you are using is not using typescript or the ionic-native module.

here is a project you can look at https://github.com/aaronksaunders/GAProject

in app.ts

import {StatusBar, GoogleAnalytics} from 'ionic-native';

also you should access the module this way

import {Component} from '@angular/core';
import {Platform, ionicBootstrap} from 'ionic-angular';
import {StatusBar, GoogleAnalytics} from 'ionic-native';
import {HomePage} from './pages/home/home';


@Component({
  template: '<ion-nav [root]="rootPage"></ion-nav>'
})
export class MyApp {
  rootPage: any = HomePage;

  constructor(platform: Platform) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();

      // google
      GoogleAnalytics.debugMode()
      GoogleAnalytics.startTrackerWithId("YOUR_GOOGLE_ID");

      GoogleAnalytics.enableUncaughtExceptionReporting(true)
        .then((_success) => {
          console.log(_success)
        }).catch((_error) => {
          console.log(_error)
        })

    });
  }
}