I am trying to use the 'https://github.com/danwilson/google-analytics-plugin.git' plugin with ngCordova. I have added the plugin and added ngCordova as a dependency in my controller.
when I try to set:
$cordovaGoogleAnalytics.startTrackerWithId('UA-XXXXXXXX-X');
I get this error: 'TypeError: Cannot read property 'startTrackerWithId' of undefined'.
I have set my analytics up as a mobile app in the Google dashboard.
Can anybody help with this?
That happens because you are trying to use analytics plugin before it's initialized by cordova.
Just wrap the initialization recursevely with a setTimetout:
function _waitForAnalytics(){
if(typeof analytics !== 'undefined'){
$cordovaGoogleAnalytics.debugMode();
$cordovaGoogleAnalytics.startTrackerWithId('UA-XXXXXXXX-X');
$cordovaGoogleAnalytics.trackView('APP first screen');
}
else{
setTimeout(function(){
_waitForAnalytics();
},250);
}
};
_waitForAnalytics();
I think that this is the best way of tracking with google analytics
$ionicPlatform.ready(function () {
$rootScope.$on('$stateChangeSuccess', function () {
if(typeof analytics !== undefined) {
analytics.debugMode();
analytics.startTrackerWithId("UA-xxxxxxxx-x");
analytics.trackView($state.current.name);
} else {
console.log("Google Analytics Unavailable");
}
});
});
This will track on each state change on the application and will give you the state that user is on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With