Category Archives: PhoneGap

Google Analytics for Phonegap with error tracking

Google Analytics for Phonegap

As you all know Google analytic’s mobile is used to measure and optimize user acquisition and engagement with your mobile apps. Integrating SDk with your app play an important role. Google analytic’s provides an SDk for IOS and Android, which are leading mobile OS. For windows phone app we can use the JavaScript version of Google analytic’s.

In order to use Google Analytics for Phonegap download the plugin available on github  . Currently it supports only android and IOS.

This plugin uses GoogleAnalyticsV2.jar for android and Google Analytics.a for IOS library provided by Google mobile analytic SDk . This plugin allows you to track page, set variable and track events. GAPlugin can capture only native level events like device model, country, your ISP, exception etc.. It cannot capture JavaScript error and html page activity. To track JavaScript error, we can use to track events or track page custom events.

Note: This plugin cannot track client side error. In order to use this plugin just call the plugin in page load event and its done.

 var gaPlugin;

function onDeviceReady() {

    gaPlugin = window.plugins.gaPlugin;

    gaPlugin.init(successHandler, errorHandler, "UA-43345678-1", 10);

}

Now replace UA-12345678-1 with your tracking ID. Which can be found at Google analytic website. Number “10” is update duration in terms of seconds. Don’t forget to write success and error handler method.

To track custom events you can use

gaPlugin.trackEvent( successHandler, errorHandler, "login Button", "Click", "button was clicked", 1);

gaPlugin.setVariable( (successHandler, errorHandler, 1, "Myvariable");

gaPlugin.trackPage( (successHandler, errorHandler, "login.html");

To stop tracking call gaPlugin.exit(successHandler, errorHandler); method. To test your code you can log-in to https://www.google.com/analytics/ and check your log. To track JavaScript Errors, we can use

window.onerror

Which is run-time script error event handler, works on all mobile device browsers.

window.onerror = function(message, file, line) {
var error = file + "(" + line + "): " + message;
gaPlugin.trackEvent(nativePluginResultHandler, nativePluginErrorHandler, "Global", "Exception", error, 1)
}

If you want to use Google’s Universal Analytic’s in your app then you can refer Watson’s code at gitHub. This library logs any unhandled errors on your web pages and will report to your Google analytic account. Remember this will log error in Google analytic events section.

There is an alternative to track both native and hybrid events, to do so we have to send the error to gaPlugin and in native file use to ExceptionReporter to send the error to Google analytic’s.