Google Analytics
This plugin connects to Google's native Universal Analytics SDK Prerequisites:
- A Cordova 3.0+ project for iOS and/or Android
- A Mobile App property through the Google Analytics Admin Console
- (Android) Google Play Services SDK installed via Android SDK Manager
Repo: https://github.com/danwilson/google-analytics-plugin
Installation
- Install the Cordova and Ionic Native plugins:
$ ionic cordova plugin add cordova-plugin-google-analytics $ npm install --save @ionic-native/google-analytics@4
- Add this plugin to your app's module
Supported platforms
- Android
- Browser
- iOS
- Windows Phone 8
Usage
import { GoogleAnalytics } from '@ionic-native/google-analytics';
constructor(private ga: GoogleAnalytics) { }
...
this.ga.startTrackerWithId('YOUR_TRACKER_ID')
.then(() => {
console.log('Google analytics is ready now');
this.ga.trackView('test');
// Tracker is ready
// You can now track pages or set additional information such as AppVersion or UserId
})
.catch(e => console.log('Error starting GoogleAnalytics', e));
Instance Members
startTrackerWithId(id, interval)
In your ‘deviceready’ handler, set up your Analytics tracker. https://developers.google.com/analytics/devguides/collection/analyticsjs/
Param | Type | Details |
---|---|---|
id |
string
|
Your Google Analytics Mobile App property |
interval |
number
|
Optional dispatch period in seconds. Defaults to 30. |
Returns: Promise<any>
setAllowIDFACollection(allow)
Enabling Advertising Features in Google Analytics allows you to take advantage of Remarketing, Demographics & Interests reports, and more
Param | Type | Details |
---|---|---|
allow |
boolean
|
Returns: Promise<any>
setUserId(id)
Set a UserId https://developers.google.com/analytics/devguides/collection/analyticsjs/user-id
Param | Type | Details |
---|---|---|
id |
string
|
User ID |
Returns: Promise<any>
setAnonymizeIp(anonymize)
Set a anonymize Ip address
Param | Type | Details |
---|---|---|
anonymize |
boolean
|
Set to true to anonymize the IP Address |
Returns: Promise<any>
setAppVersion(appVersion)
Set the app version
Param | Type | Details |
---|---|---|
appVersion |
string
|
App version |
Returns: Promise<any>
getVar(key)
Get a variable
Param | Type | Details |
---|---|---|
key |
string
|
Variable |
Returns: Promise<any>
setVar(key, value)
Set a variable
Param | Type | Details |
---|---|---|
key |
string
|
Variable |
value |
string
|
Parameter |
Returns: Promise<any>
setOptOut(optout)
Set OptOut
Param | Type | Details |
---|---|---|
optout |
boolean
|
Returns: Promise<any>
debugMode()
Enable verbose logging
Returns: Promise<any>
trackMetric(key, value)
Track custom metric
Param | Type | Details |
---|---|---|
key |
number
|
|
value |
any
|
Returns: Promise<any>
trackView(title, campaignUrl, newSession)
Track a screen https://developers.google.com/analytics/devguides/collection/analyticsjs/screens
Param | Type | Details |
---|---|---|
title |
string
|
Screen title |
campaignUrl |
string
|
Campaign url for measuring referrals |
newSession |
boolean
|
Set to true to create a new session |
Returns: Promise<any>
addCustomDimension(key, value)
Add a Custom Dimension https://developers.google.com/analytics/devguides/platform/customdimsmets
Param | Type | Details |
---|---|---|
key |
number
|
|
value |
string
|
Returns: Promise<any>
trackEvent(category, action, label, value, newSession)
Track an event https://developers.google.com/analytics/devguides/collection/analyticsjs/events
Param | Type | Details |
---|---|---|
category |
string
|
|
action |
string
|
|
label |
string
|
|
value |
number
|
|
newSession |
boolean
|
Set to true to create a new session |
Returns: Promise<any>
trackException(description, fatal)
Track an exception
Param | Type | Details |
---|---|---|
description |
string
|
|
fatal |
boolean
|
Returns: Promise<any>
trackTiming(category, intervalInMilliseconds, variable, label)
Track User Timing (App Speed)
Param | Type | Details |
---|---|---|
category |
string
|
|
intervalInMilliseconds |
number
|
|
variable |
string
|
|
label |
string
|
Returns: Promise<any>
addTransaction(id, affiliation, revenue, tax, shipping, currencyCode)
Add a Transaction (Ecommerce) https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce#addTrans
Param | Type | Details |
---|---|---|
id |
string
|
|
affiliation |
string
|
|
revenue |
number
|
|
tax |
number
|
|
shipping |
number
|
|
currencyCode |
string
|
Returns: Promise<any>
addTransactionItem(id, name, sku, category, price, quantity, currencyCode)
Add a Transaction Item (Ecommerce) https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce#addItem
Param | Type | Details |
---|---|---|
id |
string
|
|
name |
string
|
|
sku |
string
|
|
category |
string
|
|
price |
number
|
|
quantity |
number
|
|
currencyCode |
string
|
Returns: Promise<any>
enableUncaughtExceptionReporting(shouldEnable)
Enable/disable automatic reporting of uncaught exceptions
Param | Type | Details |
---|---|---|
shouldEnable |
boolean
|
Returns: Promise<any>
dispatch()
Platforms:Android iOS Windows
Manually dispatch any data
Returns: Promise<any>