# Cordova Google Analytics Plugin

This Cordova plugin is used to connect to Google's native Universal Analytics SDK.

* Repository: <https://github.com/danwilson/google-analytics-plugin>
* Plugin ID/package name: `cordova-plugin-google-analytics`
* Tested version: 1.8.6

{% hint style="info" %}
To check the third party Cordova plugins, you need to create a custom build debugger ([Android version](https://en.docs.monaca.io/products_guide/debugger/installation/debugger_android#build-and-install-a-custom-monaca-debugger) or [iOS version](https://en.docs.monaca.io/products_guide/debugger/installation/debugger_ios#building-a-custom-monaca-debugger)).
{% endhint %}

## Demo

[<img src="https://docs.monaca.io/images/common/import_img.png" alt="" data-size="line">Import the Google Analytics plugin demo to your Monaca account](https://monaca.mobi/directimport?pid=5ac33902e7888548428b4567)

![](https://docs.monaca.io/images/samples/analytics.png)

## 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.

```javascript
//Replace your app tracking id here
var trackingID="YOUR_APP_TRACKING_ID";

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady(){
    console.log('Google analytics is ready now');
    window.ga.startTrackerWithId(trackingID);
}
```

## API references

This section describes some of the main functions used in our [demo](https://monaca.mobi/directimport?pid=5ac33902e7888548428b4567). For complete API references, please refer to the [repository](https://github.com/danwilson/google-analytics-plugin).

### startTrackerWithId()

Sets up the analytics tracker.

```javascript
window.ga.startTrackerWithId(trackingId, [interval]);
```

**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**

```javascript
window.ga.startTrackerWithId('UA-XXXX-YY', 30);
```

### trackView()

Tracks the screen.

```javascript
window.ga.trackView(title, campaignUrl, [newSession]);
```

**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**

```javascript
//To track a Screen (PageView):
window.ga.trackView('Screen Title')

//To track a Screen (PageView) w/ campaign details:
window.ga.trackView('Screen Title', 'my-scheme://content/1111?utm_source=google&utm_campaign=my-campaign')

//To track a Screen (PageView) and create a new session:
window.ga.trackView('Screen Title', '', true)
```

### trackEvent()

Tracks an event.

```javascript
window.ga.trackEvent(category, action, [label], [value], [newSession])
```

**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**

```javascript
//To track an Event
window.ga.trackEvent('Videos', 'play', 'Fall Campaign', 42)

//To track an Event and create a new session:
window.ga.trackEvent('Videos', 'play', 'Fall Campaign', 42, true)
```

### setUserId()

Sets a user id.

```javascript
window.ga.setUserId(id);
```

**Parameter**

| Name | Type   | Description                                                                         |
| ---- | ------ | ----------------------------------------------------------------------------------- |
| `id` | String | A unique identifier, associated with a particular user, must be sent with every hit |

**Return Value**

* `Promise`

**Example**

```javascript
//user ID for testing purpose
var myUserId="35009a79-1a05-49d7-b876-2b884d0f825b";
window.ga.setUserId(myUserId);
```

### setAppVersion()

Sets a specific app version.

```javascript
window.ga.setAppVersion(appVersion)
```

**Parameter**

| Name         | Type   | Description |
| ------------ | ------ | ----------- |
| `appVersion` | String | App version |

**Return Value**

* `Promise`

**Example**

```javascript
window.ga.setAppVersion('1.33.7');
```

### debugMode()

Enables verbose logging.

```javascript
window.ga.debugMode()
```

**Return Value**

* `Promise`

**Example**

```javascript
window.ga.debugMode();
```
