Monaca Docs
Search…
Cordova Google Analytics Plugin
This Cordova plugin is used to connect to Google's native Universal Analytics SDK.
To check the third party Cordova plugins, you need to create a custom build debugger (Android version or iOS version).

Demo

Enable the plugin in the Monaca IDE

    1.
    From the IDE menu, go to Config → Manage Cordova Plugins .
    2.
    Click the Import Cordova Plugin button. Then, you can choose to import the plugin using a ZIP file or a URL/package name.

Usage

After importing the plugin to your project, you can start by initializing your tracking ID. Make sure to call the plugin API after the Cordova is loaded.
1
//Replace your app tracking id here
2
var trackingID="YOUR_APP_TRACKING_ID";
3
4
document.addEventListener("deviceready", onDeviceReady, false);
5
6
function onDeviceReady(){
7
console.log('Google analytics is ready now');
8
window.ga.startTrackerWithId(trackingID);
9
}
Copied!

API references

This section describes some of the main functions used in our demo. For complete API references, please refer to the repository.

startTrackerWithId()

Sets up the analytics tracker.
1
window.ga.startTrackerWithId(trackingId, [interval]);
Copied!
Parameter
Name
Type
Description
trackingId
String
Your Google Analytics mobile app property
interval
Number
[optional] The dispatch period in seconds (default: 30)
Return Value
    Promise
Example
1
window.ga.startTrackerWithId('UA-XXXX-YY', 30);
Copied!

trackView()

Tracks the screen.
1
window.ga.trackView(title, campaignUrl, [newSession]);
Copied!
Parameter
Name
Type
Description
title
String
Screen title
campaignUrl
String
Campaign url for measuring referrals
newSession
Boolean
[optional] Set to true to create a new session
Return Value
    Promise
Example
1
//To track a Screen (PageView):
2
window.ga.trackView('Screen Title')
3
4
//To track a Screen (PageView) w/ campaign details:
5
window.ga.trackView('Screen Title', 'my-scheme://content/1111?utm_source=google&utm_campaign=my-campaign')
6
7
//To track a Screen (PageView) and create a new session:
8
window.ga.trackView('Screen Title', '', true)
Copied!

trackEvent()

Tracks an event.
1
window.ga.trackEvent(category, action, [label], [value], [newSession])
Copied!
Parameter
Name
Type
Description
category
String
Event category (e.g. 'Video')
action
String
Action type (e.g. 'play')
label
String
[optional] Event label (e.g. 'Fall Campaign')
value
Number
[optional] A numeric value associated with the event (e.g. 42)
newSession
Boolean
[optional] Set to true to create a new session
Return Value
    Promise
Example
1
//To track an Event
2
window.ga.trackEvent('Videos', 'play', 'Fall Campaign', 42)
3
4
//To track an Event and create a new session:
5
window.ga.trackEvent('Videos', 'play', 'Fall Campaign', 42, true)
Copied!

setUserId()

Sets a user id.
1
window.ga.setUserId(id);
Copied!
Parameter
Name
Type
Description
id
String
A unique identifier, associated with a particular user, must be sent with every hit
Return Value
    Promise
Example
1
//user ID for testing purpose
2
var myUserId="35009a79-1a05-49d7-b876-2b884d0f825b";
3
window.ga.setUserId(myUserId);
Copied!

setAppVersion()

Sets a specific app version.
1
window.ga.setAppVersion(appVersion)
Copied!
Parameter
Name
Type
Description
appVersion
String
App version
Return Value
    Promise
Example
1
window.ga.setAppVersion('1.33.7');
Copied!

debugMode()

Enables verbose logging.
1
window.ga.debugMode()
Copied!
Return Value
    Promise
Example
1
window.ga.debugMode();
Copied!
Last modified 2mo ago