GoogleAnalytics: Cannot read property 'trackView' of undefined


#1
    Trying to include GA into my project, am getting uncaught in promise when running. I added the import statement in app.module.ts like this:

import { GoogleAnalytics } from ‘@ionic-native/google-analytics’;

@NgModule({
declarations: [
MyApp,
QuotesListPage,
QuotesDetailPage

],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
QuotesListPage,
QuotesDetailPage
],
providers: [
GoogleAnalytics,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})

The pages wanted to track are quotes-list:

export class QuotesListPage {

quotesList = [];
filteredQuotes = [];
isfiltered: boolean ;
googleanalytics: GoogleAnalytics;

constructor(private http:Http, public navCtrl: NavController, public navParams: NavParams, platform: Platform) {
this.isfiltered = false;
this.http.get(‘quotes.json’)
.map(res => res.json())
.subscribe(
data => {
this.quotesList = data.quotes;
},
err => console.log("error is "+err), // error
() => console.log('read quotes Complete '+ this.quotesList) // complete
);
platform.ready().then(() => {
this.googleanalytics.trackView(“Quotes List”);
});
}

And quotes-detail:

import { GoogleAnalytics } from ‘@ionic-native/google-analytics’;

@IonicPage()
@Component({
selector: ‘page-quotes-detail’,
templateUrl: ‘quotes-detail.html’,
})
export class QuotesDetailPage {

quoteDetail: {quote:’’, author:’’};
googleanalytics: GoogleAnalytics;

constructor(public navCtrl: NavController, public navParams: NavParams) {
this.quoteDetail = navParams.get(‘quote’);
this.googleanalytics.trackView(“Quotes Detail”);
}

ionViewDidLoad() {
console.log(‘ionViewDidLoad QuotesDetailPage’);
}

Finally app.component:

export class MyApp {
rootPage:any = QuotesListPage;
public googleanalytics: GoogleAnalytics;

constructor(platform: Platform) {
platform.ready().then(() => {
this.googleanalytics.debugMode();
this.googleanalytics.startTrackerWithId(“XX-XXXXXXXXX-X”);
this.googleanalytics.enableUncaughtExceptionReporting(true).then((_success) => {
console.log("Successful enabling of uncaught exception reporting "+_success)}).catch((_error) => {
console.log("error occured "+_error)
});
});
}

Some help, please! Thank you very much!


#2

You need to wait for completion of the start tracker

  this.ga.startTrackerWithId('UA-XXXXXXXXX-X').then(() => {
    console.log('Google analytics is ready now');
    this.ga.setAllowIDFACollection(true);
    this.ga.setAppVersion(this.settings.version);
    // Tracker is ready
    // You can now track pages or set additional information such as AppVersion or UserId
  })

I have seen that it is recommended to remove the call for debug mode