google-analytics-plugin: Tracker not started - only iOS - Ionic2

Hello - everything works fine on Android but the Tracker is not starting on iOS.

import {GoogleAnalytics} from 'ionic-native';

    platform.ready().then(() => {
      StatusBar.styleDefault();
      // --- Google Analytics ---
      //GoogleAnalytics.debugMode();
      GoogleAnalytics.startTrackerWithId("UA-XXXXXXXX-X");   //I replaced it with my ID

      GoogleAnalytics.enableUncaughtExceptionReporting(true)
        .then((_success) => {})
        .catch((_error) => {
        console.log("ERROR Google Analytics:", _error)
      })
    });

Error: Tracker not started

Thanks for help 😃

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 39

Most upvoted comments

@martingra Don’t downgrade the plugin and simply set your ionic-native to 2.2.16 in your package.json ("ionic-native": "2.2.16"), then run npm i in your terminal.

Instead of downgrading the plugin from 1.7.x to 1.6.0, updating ionic-native to 2.2.16 did the trick.

Ok, looks like the latest version is buggy. 1.6.0 does the trick! call trackEvent after startTrackerWithId is resolved and it works 👍

app_component_ts_ _de_tmob_android app_component_ts_ _de_tmob_android

Thanks @peterpeterparker & @fttx for your help!

1.6.0 😉

here you go @victorsosa

I created a service like this. But note, funny thing, since I always use that service to track my event, the plugin itself is always defined, so I don’t face the above error anymore and I could say that most of the following code (like saving stuffs in array to send these events later) isn’t use…but who knows what will happens on each devices

import {Injectable} from '@angular/core';
import {GoogleAnalytics} from 'ionic-native';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';

@Injectable()
export class GoogleAnalyticsService {

eventsNotSent:any[] = new Array();
trackerInitialized:boolean = false;

trackView(viewName:string) {
    if (typeof window.analytics === 'undefined') {
        this.eventsNotSent.push({viewName: viewName});
    } else {
        this.initGoogleAnalytics().then(() => {
            this.trackEventsNotSent().then(() => {
                GoogleAnalytics.trackView(viewName).then(() => {
                    // Do nothing
                });
            });
        }, (error: any) => {
            this.eventsNotSent.push({viewName: viewName});
        });
    }
}

 private trackEventsNotSent():Promise<{}> {
    return new Promise((resolve) => {
        if (this.eventsNotSent == null || Object.keys(this.eventsNotSent).length === 0) {
            resolve();
        } else {
            let promises = new Array();

            for (let i: number = 0; i < this.eventsNotSent.length; i++) {
                    promises.push(GoogleAnalytics.trackView(this.eventsNotSent[i].viewName));
            }

            Observable.forkJoin(promises).subscribe(
                (data:any) => {
                    this.eventsNotSent = new Array();
                    resolve();
                });
        }
    });
}

private initGoogleAnalytics():Promise<{}> {
    return new Promise((resolve, reject) => {
        if (this.trackerInitialized) {
            resolve();
        } else {
                GoogleAnalytics.startTrackerWithId('MY_TRACKER_ID').then(() => {
                      this.trackerInitialized = true;
                        resolve();
            }).catch((error: any) => {
                // Do nothing
                this.trackerInitialized = false;
                reject(error);
            });
        }
    });
   }
 }

Ok, Thanks you guys for the inside; 1.7.0 signature changed to window.ga.startTrackerWithId(‘UA-XXXX-YY’, 30) where 30 is the interval period. Maybe this is the root cause of the problem; Have anyone check the android logcat for errors? (It seems like this is not working only on Android)

If i downgrade to 1.6.0 it works! What’s your plugin version?

Thanks, this will help other to create their own using Ionic 2. I myself use Ionic 1 yet.