Google Analytics Plugin Not Reporting any User in the Dashboard


#1

Hi All,
I am trying to add Google Analytics to my app. I am using Dan Wilson’s Google Analytics plugin. I am not getting any errors but my Google Analytics Dashboard is not Updating at all. Where am i wrong,

var googleanalyticsApp = angular.module('starter', ['ionic','ngCordova'])

.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {    
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);     
  cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}

if(typeof analytics !== undefined) {                
            analytics.startTrackerWithId("UA-872489XX-1");
            console.log("Analytics Initialized");
        } else {
            console.log("Google Analytics Unavailable");
        }
  });
});

googleanalyticsApp.controller('ExampleController', function($scope,$cordovaGoogleAnalytics) {
function _waitForAnalytics(){

      if(typeof analytics !== 'undefined'){         
       analytics.startTrackerWithId("UA-87248923-1");
       analytics.trackView('APP first screen');          
       console.log("Analytics in Controller");
      }

      else{

      setTimeout(function(){

      _waitForAnalytics();

      },250);

      }
      };

   _waitForAnalytics();

I have initialized the Google Analytics in both Device Ready and Controller.
Console.log states as:

But my Dashboard is not updating at all

Kindly, help me out.


#2

A couple things come to mind. For one, if you check the documentation for that plugin (https://github.com/danwilson/google-analytics-plugin) it tells you specifically not to use analytics

v1.0.0 – api change from window.analytics to window.ga, ‘analytics’ is deprecated since 1.0.0 and you should use the new api ‘ga’, because in the next release we are removing the analytics.

Also all the examples given in the documentation don’t use analytics, example:

window.ga.startTrackerWithId(‘UA-XXXX-YY’)

So I would start by following the documentation.

If that fails, in Google Analytics you’re looking at Audience Overview, you’d be better of looking under Real Time, because that updates in real time. You might not see anything on the screen you’ve shown here for a while.

Finally, if all that somehow fails, use window.ga.debugMode() to turn on verbose logging, and use logcat to see what errors may be logged with the Google Analytics SDK. With debug mode on you will see lots of logs and should be able to determine the issue.

P.S. As a side note, a lot of your code is really bad. For one typeof analytics will never be undefined. It might be "undefined" or analytics itself might be undefined, but your if block will always run, whether analytics exists or not.

In your controller you actually have the if check there correctly, with 'undefined' instead of undefined, but that code is a complete nightmare…instead of doing that insanity, just check if the platform is ready:

$ionicPlatform.ready(function() {
    window.ga.trackView('APP first screen');   
});

#3

Hi @rlouie,
Thanks for the reply. I added your recommendations.

            window.ga.debugMode();
            window.ga.startTrackerWithId('UA-872489XX-1');
             window.ga.trackView('APP first screen');  
             console.log(window.ga); 
            console.log("Analytics Initialized");

But, still NO LUCK.
I called adb logcat after ionic run android, but got a ton of logcat data and i am unable to find out where is the error related to Google Analytics Plugin.
Would really appreciate your help in this.

Thanks
Sourav


#4

You need to filter down your logs to your app and look at the logs. There being a lot of logs is not really an excuse to not look through them. It’s the only thing that will point to the answer at this point I believe.

Here are some tips on filtering the logs down: http://stackoverflow.com/questions/6854127/filter-logcat-to-get-only-the-messages-from-my-application-in-android


#5

Thanks. It started working after a long time. Dont know what happened :smiley:


#6

how it’s working with you? what you did to make it work?


#7

Solved it.

$cordova platform add android
$cordova platform update android@6.1.0
$cordova platform add cordova-plugin-google-analytics

#8

The reason was the Cordova Version.
See the solution below