Google Analytics

I’m trying to use Dan Wilson’s Google Analytics plugin but can’t seem to get it to pull any data in. He says to place the UA code in the deviceready handler. Would that be something like this for Ionic?

$ionicPlatform.ready(function() {
  analytics.startTrackerWithId('UA-XXXXX-X');
});

Any help on this would be awesome.

1 Like

Hey @Tyler, try putting this into angular’s run function:

angular.run(['$ionicPlatform', function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    analytics.startTrackerWithId('UA-XXXXX-X');
  });
}])

That would be the correct place to put it, so if it’s not working I would suggest trying it with just deviceready outside of angular and narrowing it down.

we have the plugin working in out app and that where we placed it

Yeah, I actually have it running inside the .run directive… doesn’t seem to be working though. Is there a good way to test or debug Google Analytic?

wrap the call in a try catch block to see if it is throwing exceptions would be the first thing i would do

Few obvious things, you need to get a valid token id from analytics.google.com for a mobile app, not a web site. Second, we init it like this:

.run(function(previewData, localStorage, $ionicPlatform) {
  $ionicPlatform.ready(function() {
      if (typeof analytics !== 'undefined'){
        analytics.startTrackerWithId('UA-XXXXXXXX-X');
      }
      else
      {
        console.log("Google Analytics plugin could not be loaded.")
      }
1 Like

how is analytics injected, or brought into scope in this example?

As a plugin.

I also got it working in the end. Was missing the analytics.trackView('Screen Title') bit.

Check this module : http://luisfarzati.github.io/angulartics/

I am trying to add Google analytics to my app too. Could you share the code that you use to call the startTrackerWithId()? I tried a few options but I am still getting “Google Analytics plugin could not be loaded.”

I installed the google-analytics-plugin using:

cordova plugin add https://github.com/danwilson/google-analytics-plugin.git

Here is my code in app.js:

.run(function($ionicPlatform, $rootScope) {
$ionicPlatform.ready(function() {
if(window.StatusBar) {
StatusBar.styleDefault();
}
if (typeof analytics !== ‘undefined’){
analytics.startTrackerWithId(‘UA-52379059-1’);
analytics.trackView(‘Home’);
}
else
{
console.log(“Google Analytics plugin could not be loaded.”)
}
});

But analytics is still not defined. Any idea what am I doing wrong?

Pura vida.

Hervé

Here’s how I have mine. Also note that this function only works on deployed apps due to it being installed as a plugin so you’ll always get the else statement until you build and deploy the app.

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if (typeof analytics !== 'undefined'){
      analytics.startTrackerWithId('UA-XXXXXX-XX');
      analytics.trackView('name-of-template');
    }
  });
})
2 Likes

Thank you @tyler for this precious information.

Should I call the plugin somewhere? Do I have to include it in config.xml?

Nope. You should just install it via command line. Both iOS and Android have their own config.xml files where the correct tags will be included.

Hey guys,
thanks to this thread I managed to have the plugin to work.
There is still one thing I can’t figure out myself, where in the angular routing process should I indicate that user changed view ?
In other words, where should I put this : analytics.trackView(‘name-of-template’);
if I want to track each template ?

Thanks

Hi koko!, i am getting an error when i try to build to ios, you are using it right now?

Here’s how I’m currently using it, I place at the end of each controller the following :

if (typeof analytics !== 'undefined')
    analytics.trackView($state.current.name);

hey i am always getting an (analytics is not defined) ‘Google Analytics plugin could not be loaded’ please help me, how to make it work.
i used the following code.

if (typeof analytics !== ‘undefined’){
analytics.startTrackerWithId(‘UA-xxxxxxxx-x’);
analytics.trackView(‘newhome-2’);
console.log(“starting analytics”);
analytics.debugMode();
}
else
{
console.log(“Google Analytics plugin could not be loaded.”);
}

i have included this code in $ionicPlatform.ready function.
please help me out.

Did you try on the device? I’m not sure if it’s supposed to work in the browser.