Google Analytics: Tracker not started

OK, I thought setting up Google Analytics would be a breeze :slight_smile:

Running on Android device, I get:
EXCEPTION: Uncaught (in promise): Tracker not started

my app.component.ts file:

platform.ready().then(() => {
        GoogleAnalytics.debugMode();
        GoogleAnalytics.startTrackerWithId('UA-XXXXXXX-X);
        GoogleAnalytics.enableUncaughtExceptionReporting(true)
          .then((_success) => {
            console.log(_success)
          }).catch((_error) => {
            console.log(_error)
          });
} 

What do I have to change?

Thanks!
Fred

Same problem there using ionic 1!

I use to have such a problem. I ended up writting a service which I use to handle all my GA tracking events. I added an array to push in all events which would be triggered before the plugin was ready. Funny thing, as soon as I added my service, I didn’t faced anymore the error “tracker not started”, I guess it was a problem in my initialization flow.

Anyway there I posted the all code of my service (see answers from @peterpeterparker):

Hope could help.

Thanks! Having all this difficulty, one wonders to go back towards coding a custom tracker with Http module. Just need a back-end for it :°)

1 Like

I tried creating a service as well, so now all the initialization code is in the service, but I’m still getting the “Tracker not started” error :-/

This is my service’s code (its simpler than yours):

import { Injectable } from '@angular/core';
import { GoogleAnalytics} from 'ionic-native';
import { Platform } from 'ionic-angular';

declare var ga: any;

@Injectable()
export class AnalyticsService {
  platform : any;

  constructor(private _platform: Platform) {
    this.platform = _platform;

    this.platform.ready().then(() => {

      console.log("GoogleAnalytics service platform ready");

      if(this.platform.is("ios") || this.platform.is("android")){

        GoogleAnalytics.debugMode();

        GoogleAnalytics.enableUncaughtExceptionReporting(true)
          .then((_success) => {
            console.log("GoogleAnalytics success: " + _success);
          }).catch((_error) => {
            console.log("GoogleAnalytics error: " + _error);
          });

        GoogleAnalytics.startTrackerWithId("UA-xxx-13");
        console.log("GoogleAnalytics initialized for mobile: UA-xxx-13");
      }

    });

  }

  track_page_view(page_name, page_path) {
    this.platform.ready().then(() => {
      console.log("Tracking page view: " + page_name);

      if(this.platform.is("ios") || this.platform.is("android")){
        //mobile
        GoogleAnalytics.trackView(page_name);
        console.log("GoogleAnalytics: Tracked pageview for mobile: " + page_name);
      }
      else{
        // web
        ga('set', 'page', page_path);
        ga('send', 'pageview');
        console.log("GoogleAnalytics: Tracked pageview for web: " + page_path);
      }

    });
  }
}

(ignore the else in track_page_view).

This is the output I get from logcat:

11-25 16:01:35.392 7066 7066 I chromium: [INFO:CONSOLE(796)] “GoogleAnalytics service platform ready”, source: file:///android_asset/www/build/js/app.bundle.js (796)
11-25 16:01:35.410 7066 7066 I chromium: [INFO:CONSOLE(806)] “GoogleAnalytics initialized for mobile: UA-xxx-13”, source: file:///android_asset/www/build/js/app.bundle.js (806)
11-25 16:01:35.432 7066 7066 I chromium: [INFO:CONSOLE(817)] “GoogleAnalytics: Tracked pageview for mobile: Home”, source: file:///android_asset/www/build/js/app.bundle.js (817)
11-25 16:01:35.483 7066 7066 I chromium: [INFO:CONSOLE(803)] “GoogleAnalytics error: Tracker not started”, source: file:///android_asset/www/build/js/app.bundle.js (803)
11-25 16:01:36.463 7066 7066 I chromium: [INFO:CONSOLE(796)] “GoogleAnalytics service platform ready”, source: file:///android_asset/www/build/js/app.bundle.js (796)
11-25 16:01:36.463 7066 7066 I chromium: [INFO:CONSOLE(806)] “GoogleAnalytics initialized for mobile: UA-51071822-13”, source: file:///android_asset/www/build/js/app.bundle.js (806)
11-25 16:01:36.463 7066 7066 I chromium: [INFO:CONSOLE(817)] “GoogleAnalytics: Tracked pageview for mobile: Search”, source: file:///android_asset/www/build/js/app.bundle.js (817)

Do you see something horribly wrong with my service?

Thanks!

I think the first thing first to test is: what version of the plugin are you using?

Just yesterday, users of the plugin found out that current version of the plugin doesn’t really work in Android but with previous version, 1.6.0 everything was working. Maybe there is your solution too?

See

About your code, I think it is fine. I just see a possible improvement, if you want to use enableUncaughtExceptionReporting, is to move the start of the tracker in the promise. Otherwise, if I understand, both operations gonna be done in parallel and honestly I don’t know if that could have a side effect…but maybe none

GoogleAnalytics.enableUncaughtExceptionReporting(true)
      .then((_success) => {
        console.log("GoogleAnalytics success: " + _success);

       GoogleAnalytics.startTrackerWithId("UA-xxx-13");
       console.log("GoogleAnalytics initialized for mobile: UA-xxx-13");

      }).catch((_error) => {
        console.log("GoogleAnalytics error: " + _error);
      });

If you still face problems, let me knows, may have another idea if needed :wink:

Hi @reedrichards, thanks for the reply.

Yes, I’m using version 1.6.0 of the plugin:

$ ionic plugins
  cordova-plugin-google-analytics 1.6.0 "Google Universal Analytics Plugin"

I made the change you suggested of calling the startTrackerWithId inside the promise for enableUncaughtExceptionReporting, and yes, that works better because now on every call to the service I get the “Tracker not started” error. Here is some additional output from the adb logcat. This is the only cordova plugin I’m using in the app, so I assume the “CordovaPlugin” are for GoogleAnalytics.

11-28 11:53:42.018 1230 1632 D hwcomposer: hw_composer sent 276 syncs in 65s
11-28 11:53:45.846 3859 3859 I chromium: [INFO:CONSOLE(796)] “GoogleAnalytics service platform ready”, source: file:///android_asset/www/build/js/app.bundle.js (796)
11-28 11:53:45.846 3859 3970 W CordovaPlugin: Attempted to send a second callback for ID: UniversalAnalytics809557534
11-28 11:53:45.846 3859 3970 W CordovaPlugin: Result was: “Invalid action”
11-28 11:53:45.847 3859 3970 W CordovaPlugin: Attempted to send a second callback for ID: UniversalAnalytics809557535
11-28 11:53:45.847 3859 3970 W CordovaPlugin: Result was: “Invalid action”
11-28 11:53:45.849 3859 3859 I chromium: [INFO:CONSOLE(813)] “Tracking page view: Players”, source: file:///android_asset/www/build/js/app.bundle.js (813)
11-28 11:53:45.849 3859 3859 I chromium: [INFO:CONSOLE(824)] “GoogleAnalytics: Tracked pageview for mobile: Players_android”, source: file:///android_asset/www/build/js/app.bundle.js (824)
11-28 11:53:45.851 3859 3859 I chromium: [INFO:CONSOLE(805)] “GoogleAnalytics error: Tracker not started”, source: file:///android_asset/www/build/js/app.bundle.js (805)

Maybe also worth mentioning, I’m using a simulator, not a real device. Could that be related?

No luck :frowning: Heard people fixing that error using version 1.6.0 of the plugin but you already do…

When I look at your code I may see a possible improvement. The thing is, you’ve got your initialization (startTrackerWithId) in your constructor. That will start an own promise but the service object, if I understand correctly, gonna be constructed without waiting for the end of that promise.

In parallel, you’ve got page view triggering (track_page_view) which is made in a separate method of your class which gonna be available as soon as the object exists.

So, again, if I understand correctly, there is still a chance that the startTrackerPromise won’t be finished before the first time you will call your tracker, right?

What you could maybe do then, move your startTrackerWithId in your method track_page_view. Then with a class variable (like initialized:boolean = false;) detect if the initialization was already performed or still have to be done, like

track_page_view(page_name, page_path) {
    this.platform.ready().then(() => {
       if (!this. initialized) {
           GoogleAnalytics.startTrackerWithId.then(() => {
                this. initialized = true;
                GoogleAnalytics.trackView .... => {
                 ....
             });
            });
        } else {
               GoogleAnalytics.trackView .... => {

Ok I’m getting closer :smiley:

I put the call for startTracker outside the constructor and into the track_view method, and then after the promise comes back, I execute the call to track view:

  track_page_view(page_name, page_path) {
    this.platform.ready().then(() => {
      console.log("Tracking page view: " + page_name);

      if(this.platform.is("ios") || this.platform.is("android")){
        //mobile

        var platform = "";

        if(this.platform.is("ios")){
          platform = "ios";
        }
        else{
          platform = "android";
        }

        GoogleAnalytics.debugMode();

        GoogleAnalytics.startTrackerWithId("UA-xxx-13").then((_result) => {
          console.log("GoogleAnalytics initialized for mobile: UA-xxx-13");
          console.log("GoogleAnalytics result: " + _result);

          console.log("GoogleAnalytics: Attempt to trackView: " + page_name + "_" + platform);

          GoogleAnalytics.trackView(page_name + "_" + platform).then((_success) => {
            console.log("GoogleAnalytics: Tracked pageview for mobile: " + page_name + "_" + platform);
          }).catch((_error) => {
            console.log("GoogleAnalytics error tracking view: " + _error);
          });

          GoogleAnalytics.enableUncaughtExceptionReporting(true)
            .then((_success) => {
              console.log("GoogleAnalytics success: " + _success);
            }).catch((_error) => {
              console.log("GoogleAnalytics error: " + _error);
            });
        });

      } 

Now I get a “tracker started” message in the success of the enableUncaughtExceptionReporting, which is great, but still, for some reason, the call to trackView doesn’t seem to be doing anything (no activity in the real time view in the Google Analytics dashboard).

Could this be a missing permission in the manifest file? some .aar or .jar I need to put somewhere? No errors this time, just not working.

Do I need to copy the google-services.json as stated in the guide for Android?

Also, about having a class variable to avoid calling startTrackerWithId every time, that’s not working for me. It seems a new instance its being created everytime my service is called. Is there a side effect on calling startTrackerWithId multiple times?

Thanks for the help!

Ok, I realized I had this as a provider and not as a service and that’s why the class variable was not being persisted. So I fixed that (not it is a service) and I don’t run the startTrackerWithId everytime.

Still the call to GoogleAnalytics.trackView is not doing anything. Not an error, nothing.

GoogleAnalytics.trackView(page_name + "_" + platform).then((_success) => {
              console.log("GoogleAnalytics: Tracked view for mobile: " + page_name + "_" + platform);
            }).catch((_error) => {
              console.log("GoogleAnalytics error tracking view: " + _error);
            });

Just doesn’t seem to be executing anything and its not registering activity in the GA dashboard. :-/

Maybe it takes a little couple of minutes till the event is displayed in the analytics console.

Well, again not sure, but when I compare your code with the pseudo I suggested you is that it seems you don’t trigger the track event when the start tracker was done, but maybe I don’t see it clearly. Won’t you mind posting the all current code of your injectable service?

Sure @reedrichards , here is the full code of the service in its current version:

import { Injectable } from '@angular/core';
import { GoogleAnalytics} from 'ionic-native';
import { Platform } from 'ionic-angular';

declare var ga: any;

@Injectable()
export class AnalyticsService {
  platform : any;
  initialized: boolean;

  constructor(private _platform: Platform) {
    this.platform = _platform;
    this.initialized = false;
    this.platform.ready().then(() => {

      console.log("GoogleAnalytics service platform ready");

    });

  }

  track_page_view(page_name, page_path) {
    this.platform.ready().then(() => {

      console.log("GoogleAnalytics is initialized? " + this.initialized);

      if(this.platform.is("ios") || this.platform.is("android")){
        //mobile

        var platform = "";

        if(this.platform.is("ios")){
          platform = "ios";
        }
        else{
          platform = "android";
        }

        //GoogleAnalytics.debugMode();

        if(!this.initialized){

          GoogleAnalytics.debugMode();

          GoogleAnalytics.startTrackerWithId("UA-xxxx-13").then((_result) => {

            this.initialized = true;

            console.log("GoogleAnalytics initialized for mobile: UA-xxx-13");
            console.log("GoogleAnalytics result: " + _result);

            GoogleAnalytics.enableUncaughtExceptionReporting(true)
              .then((_success) => {
                console.log("GoogleAnalytics success: " + _success);
              }).catch((_error) => {
                console.log("GoogleAnalytics error: " + _error);
              });
            GoogleAnalytics.trackView(page_name).then((_success) => {
              console.log("GoogleAnalytics: Tracked view for mobile: " + page_name + "_" + platform);
            }).catch((_error) => {
              console.log("GoogleAnalytics error tracking view: " + _error);
            });

          });

        }
        else{

          console.log("GoogleAnalytics: Starting Tracking page view: " + page_name + "_" + platform);

          GoogleAnalytics.trackView(page_name).then((_success) => {
            console.log("GoogleAnalytics: Tracked view for mobile: " + page_name + "_" + platform);
          }).catch((_error) => {
            console.log("GoogleAnalytics error tracking view: " + _error);
          });
        }
      }
      else{
        // web
        ga('set', 'page', page_path);
        ga('send', 'pageview');
        console.log("GoogleAnalytics: Tracked pageview for web: " + page_path);
      }

    });
  }
}

I checked this morning, and no messages show up :frowning:

I don’t see any error in logcat. I tried both in the simulator and in a real device, no luck. Today I’ll try on an iphone.

Thx @jeudyx maybe someone else may have another thought but your code looks good to me and you are using the plugin version 1.6.0 and still nothing triggered :frowning:

In your log you do see “GoogleAnalytics: Tracked view for mobile…” and no error prior to that right?

Another idea is, did you had a more recent version of the plugin than 1.6.0 before installing that version? If yes, did you remove the plugin before adding it again? Maybe the installation of 1.6.0 didn’t overrided the previous installation ?

Otherwise I’m out of ideas, sorry :frowning:

Thanks for al lthe help @reedrichards

Yes, no errors displayed. And yes, I had the latest version of the plugin before trying version 1.6.0.

I’m going to try to build from a different machine and see how it goes.

That’s crazy … hope someone got an idea

About the plugin, maybe just remove / add again or also remove the platform and add again…but it proves I’ve got no more ideas :wink:

Trust me, I’ve done that many times :smiley:

1 Like

Did you also double check that your ID is the correct one (“UA-xxxx-13”), no characters mistakes, the ID for the account you used to check your results … you know sometimes sh*t happens

Yeah. I even created a new property in case the original one had some issue. Its very suspicious that I don’t see any error. Hopefully building from a different machine will help.

1 Like

Hi @reedrichards I got some updates: I got it to work on iOS, I was able to see the realtime data in teh GA dashboard, but no luck on Android yet! But this means the code is fine, the problem is related to the platform.

So, some questions:

  • Did you add a google-services.json file somewhere?
  • Did you add any external library, either a .aar or .jar into your project?
  • Are you using any additional permissions in your android manifest?
  • Are you using crosswalk in your project?

I’m close!

Sweet that’s a good news, at least work in iOS :wink:

  • No
  • No
  • No
  • Yes

In my case it was the contrary, at first it was working in android and not in iOS. Finally I extended my service by adding my own initialization stack of events (see code in the GitHub ticket) and that fixed both terminal…if that could help too