I spent days trying to get that plugin to work. I waited 24 hours to see if the reports would work, changed the type from a mobile app to a website, tried using different plugins (no others were supported by PhoneGap build), tried Google’s universal analytics, etc. I never got it to work. I have the same set up as you: Ionic app using PhoneGap build, and I ran into the same problems.
My solution was to set analytics to a website and use the ga.js file with GALocalStorage.js. It isn’t the ideal solution, but it works for real time and reports.
You are replacing the ‘UA-xxxxxx-8’ with your tracking code from google analytics, correct? Also, make sure it is set as a website in google analytics and not a mobile app.
In my app.js file after platform ready I set the account and track the page it starts on:
// add analytics for mobile tracking
$ionicPlatform.ready(function () {
if (window.cordova && window.plugins && window.plugins.gaPlugin) {
window.plugins.gaPlugin.init(function () {
$rootScope.gaConnected = true;
}, function () {
$rootScope.gaConnected = false;
}, 'YOUR_GA_ID', 10);
}
});
In my base controller:
$rootScope.$on('$stateChangeSuccess', function () {
if ($rootScope.gaConnected) {
window.plugins.gaPlugin.trackPage(angular.noop, angular.noop, $location.path());
}
});
The listener comes from the ui-router (ionics state/rounting service) - >i check if analytics is initialized correctly -> i set a flag on the rootscope (see in my run-block).
i track the current path if a state-change succeeded.
Meanwhile I look for another solution (with ga.js and GALocalStorage.js) and works for me.
But I would to integrate GAPlugin.
In your solution, I don’t understand the second part (“In my base controller”).
Do you have a simple code to only check if the application is used ? (one code in .run)
my different views are loaded in the ion-nav-view.
But all around there is one controller which handles common stuff or shared stuff for all views, like tracking the views dymanically.
So you can write 1 code to track all views in that base controller instead of putting the tracking stuff in each view.
Incase this helps, I encountered non-connecting issues as well.
I am following bengtler’s posted recommendation and using $rootScope.
It was not until I moved the gaPlugin.init above all other code that it seemed to work fine. I’m now wondering if somehow there may be a code conflict with another plugin.
I am using “Mobile” for my GA account tracking.
The plugin.xml uses the js-module so you do not need to include any .js files within your index.html for this plugin to work.
I am using phonegap build successfully with this plugin.
Hi, im also having a problem just like ingalb, but my code is like this:
var gaPlugin; // Google analytics stuff
if(( /(ipad|iphone|ipod|android|windows phone)/i.test(navigator.userAgent) )) {
alert('hello world! this is a device');
document.addEventListener('deviceready', initApp, false);
} else {
alert(‘hello world! this is NOT a device’);
initApp();
}
/**
Init game, init google analytics
*/
function initApp() {
game.state.add(‘StartMenu’, BubbleWave.StartMenu);
// Init google analytics
gaPlugin = window.plugins.gaPlugin;
gaPlugin.init(onGASuccess, onGAError, “UA-x-x”, 10); // replace UA-x-x with your Tracking ID
}
I am trying to add Google Analytics in my ionic project and i included the GAPlugin, but i get this error
Uncaught TypeError: Cannot read property 'exec' of undefined