Friday, June 5, 2015

Add google analytics to Ionic

Main purpose is to add exception handling to ionic mobile app.Once exceptions are cached at global level, we need some service, where we can send exceptions. While exploring some of the options, I found some implementations of google analytics with AngularJS.

Following google analytics plugin is to be used.

https://github.com/danwilson/google-analytics-plugin/

Need to add following code to IONIC ready function,
analytics.startTrackerWithId('UA-XXXX-YY') where UA-XXXX-YY is your Google Analytics Mobile App property.
Note:- You can get Mobile app property by registering for Google analytic services at - https://www.google.co.in/analytics/

$ionicPlatform.ready(function() {
            if(typeof analytics !== undefined) {
                analytics.startTrackerWithId("UA-XXXXXXXX-XX");
            } else {
                console.log("Google Analytics Unavailable");
            }
        });


Once done, as per documentation on github as on 5-Jun-2015, following methods are supported.

To track a Screen (PageView):

window.analytics.trackView('Screen Title')
To track an Event:

window.analytics.trackEvent('Category', 'Action', 'Label', Value) Label and Value are optional, Value is numeric
To track an Exception:

window.analytics.trackException('Description', Fatal) where Fatal is boolean
To track User Timing (App Speed):

window.analytics.trackTiming('Category', IntervalInMilliseconds, 'Variable', 'Label') where IntervalInMilliseconds is numeric
To add a Transaction (Ecommerce)

window.analytics.addTransaction('ID', 'Affiliation', Revenue, Tax, Shipping, 'Currency Code') where Revenue, Tax, and Shipping are numeric
To add a Transaction Item (Ecommerce)

window.analytics.addTransactionItem('ID', 'Name', 'SKU', 'Category', Price, Quantity, 'Currency Code') where Price and Quantity are numeric
To add a Custom Dimension

window.analytics.addCustomDimension('Key', 'Value', success, error)
To set a UserId:

window.analytics.setUserId('my-user-id')
To enable verbose logging:

window.analytics.debugMode()

No comments:

Post a Comment