Google Analytics


#1

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.


Google Analytics Views
My google analytics in my ionic app doesn't report any active app user
#2

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.


#3

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


#4

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?


#5

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


#6

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.")
      }

#7

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


#8

As a plugin.

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


#9

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


#10

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.”


#11

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é


#12

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');
    }
  });
})

#13

Thank you @tyler for this precious information.


#14

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


#15

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.


#16

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


#17

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


#18

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);

#19

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.


#20

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