Moodle for mobile

Has anyone successfully used the cordova google analytics plugin? Advice needed.

 
Picture of Grayson Bartlet
Has anyone successfully used the cordova google analytics plugin? Advice needed.
 

I'm looking for a way to use google analytics with the mobile app. I'm under the impression that we can't use normal GA with the app, so I looked around and found this plugin. Moodle mobile actually has a reference to it in www/lib/ngCordova/dist/ng-cordova.js, which creates $cordovaGoogleAnalytics, a wrapper object for the plugin's real analytics object.

As per the instructions on the github and in ng-cordova.js, I ran cordova plugin add https://github.com/danwilson/google-analytics-plugin.git, which downloaded the plugin to plugins/cordova-plugin-google-analytics. Looking good.

I wasn't sure where to call GA for testing, so I plopped this in www/app.js:

angular.module('mm', ['ionic','ngCordova', bla bla bla])
.run(function($ionicPlatform, $cordovaGoogleAnalytics) {
$ionicPlatform.ready(function() {

$cordovaGoogleAnalytics.startTrackerWithId('UA-112316665-1')
.then(() => {
console.log('Google analytics is ready now');
//the component is ready and you can call any method here
$cordovaGoogleAnalytics.debugMode();
$cordovaGoogleAnalytics.setAllowIDFACollection(true);
})
.catch(e => console.log('Error starting GoogleAnalytics', e));
});
});

The $cordovaGoogleAnalytics.startTrackerWithId() call runs, but it's just a wrapper object, and it can't find the window.analytics object the plugin is supposed to provide.

Is there something else I must do to initialize the cordova plugin? Does it simply not work with a browser? Looking through its repository lead me to believe it DOES work with Ionic 1.x. The repo instructions do mention an extra Ionic 2/Ionic Native plugin thing, but I don't think that's necessary? And I assume the Ionic 2 stuff wouldn't work with Moodle Mobile anyway.

If anyone has any experience or advice, even with cordova plugins in general, please share. smile


 
Average of ratings: -
Picture of Grayson Bartlet
Re: Has anyone successfully used the cordova google analytics plugin? Advice needed.
 

OK, so I've resolved this. The cordova plugin does indeed work, just not with a browser (despite the presence of a "browser" implementation in the plugin). Guess that's what the Ionic 2 plugin is for. I also needed to disable debugMode.

I found the easiest way to track views was to hook into the state transition event. I just shoved a GA trackView() call into this function at the bottom of core/components/sidemenu/services/sidemenu.js:

.run(function($rootScope, $mmSideMenu, $cordovaGoogleAnalytics) {
// Hide right side menu everytime we change state.
$rootScope.$on('$stateChangeStart', function(event, toState) {
// Check we're not loading split view contents.
if (toState.name.split('.').length == 2) {
$mmSideMenu.hideRightSideMenu();
}

//wrap in try/catch because running in browser throws an exception due to plugin not being loaded
try {
console.log("********* Tracking state change to " + toState.name);
$cordovaGoogleAnalytics.trackView(toState.name);
} catch (exception) {
console.log(exception.message);
}
});
});


This will track all states as GA screens. Keep in mind this won't track external links opened in the browser, and it won't be able to distinguish different courses or different sections of the same course, so you may want to manually add tracking code for those.

 
Average of ratings: -
Picture of Juan Leyva
Re: Has anyone successfully used the cordova google analytics plugin? Advice needed.
Core developersMoodle HQParticularly helpful MoodlersPlugin developersTesters

Hi Grayson,

this is really interesting, maybe we could start supporting in the app (when you are logged to a site with Google Analytics enabled for the app).

Thanks for sharing, Juan

 
Average of ratings: -
Picture of Grayson Bartlet
Re: Has anyone successfully used the cordova google analytics plugin? Advice needed.
 

Hi Juan,

Yeah, that would be pretty cool. Our organization certainly put a lot of value on having analytics for the app, and I suspect many others feel the same. Filtering the logs for webservice calls isn't quite enough. If you get to it after you guys upgrade Ionic versions, you could use the extra Ionic 2 plugin, which would be helpful.

One thing that would be interesting to explore would be unifying analytics across Moodle Mobile and Moodle web, using the GA setID function to track users by their moodle ID (GA would, of course, anonymize this). So you could follow users as they moved between the app and the website. But I've heard that if you give a website and a mobile app the same GA property (tracking code), some stuff can get messed up. And if you want to use different properties, you need a paid GA account to roll them up. Needs more research, but it would definitely be neat.

 
Average of ratings: -