rn60-analytics
Version:
React Native for using native Google Analytics libraries
833 lines (604 loc) • 28.2 kB
Markdown
**Google Analytics Bridge** is built to provide an easy interface to the native Google Analytics libraries on both **iOS** and **Android**.
## Why a native bridge? Why not use just JavaScript?
The key difference with the native bridge is that you get a lot of the metadata handled automatically by the Google Analytics native library. This will include the device UUID, device model, viewport size, OS version etc.
You will only have to send in a few parameteres when tracking, e.g:
```javascript
import { GoogleAnalyticsTracker } from "rn-google-analytics";
let tracker = new GoogleAnalyticsTracker("UA-12345-1");
tracker.trackScreenView("Home");
tracker.trackEvent("testcategory", "testaction");
```
## Content
* [Installation](#installation-and-linking-libraries)
* [Usage](#usage)
* [JavaScript API](#javascript-api)
* [Problems with the library?](https://github.com/thanhcuong1990/react-native-google-analytics/wiki/Troubleshooting)
* [See wiki for more helpful topics](https://github.com/thanhcuong1990/react-native-google-analytics/wiki)
## Installation and linking libraries
Install with npm: `npm install --save rn-google-analytics`
Or, install with yarn: `yarn add rn-google-analytics`
**Important**: Does this library work with Expo? We have to sort of invert the question a bit, because it should be: does Expo work with other libraries? And the [answer is no](https://docs.expo.io/versions/latest/introduction/faq.html#what-is-the-difference-between-expo-and-react-native):
> The most limiting thing about Expo is that you can’t add in your own native modules without `detach`ing and using ExpoKit.
This includes using [`create-react-native-app`](https://github.com/react-community/create-react-native-app#what-are-the-limitations-of-create-react-native-app) which also makes use of Expo.
## Usage
```javascript
// You have access to three classes in this module:
import {
GoogleAnalyticsTracker,
GoogleTagManager,
GoogleAnalyticsSettings
} from "rn-google-analytics";
// The tracker must be constructed, and you can have multiple:
let tracker1 = new GoogleAnalyticsTracker("UA-12345-1");
let tracker2 = new GoogleAnalyticsTracker("UA-12345-2");
tracker1.trackScreenView("Home");
tracker1.trackEvent("Customer", "New");
// The GoogleAnalyticsSettings is static, and settings are applied across all trackers:
GoogleAnalyticsSettings.setDispatchInterval(30);
// Setting `dryRun` to `true` lets you test tracking without sending data to GA
GoogleAnalyticsSettings.setDryRun(true);
// GoogleTagManager is also static, and works only with one container. All functions here are Promises:
GoogleTagManager.openContainerWithId("GT-NZT48")
.then(() => {
return GoogleTagManager.stringForKey("pack");
})
.then(pack => {
console.log("Pack: ", pack);
})
.catch(err => {
console.log(err);
});
// You can also register Function Call tag handlers when the container is open.
GoogleTagManager.registerFunctionCallTagHandler(
"some_function", // Must be equal to Function Name field when the tag was configured.
(functionName, tagArguments) => {
// functionName is passed for convenience. In this example it will be equal to "some_function".
// tagArguments is an object and is populated based on Tag configuration in TagManager interface.
console.log("Handling Function Call tag:", functionName);
}
)
```
## JavaScript API
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
#### Table of Contents
* [GoogleAnalyticsSettings](#googleanalyticssettings)
* [setOptOut](#setoptout)
* [Parameters](#parameters)
* [Examples](#examples)
* [setDispatchInterval](#setdispatchinterval)
* [Parameters](#parameters-1)
* [Examples](#examples-1)
* [setDryRun](#setdryrun)
* [Parameters](#parameters-2)
* [Examples](#examples-2)
* [GoogleAnalyticsTracker](#googleanalyticstracker)
* [Examples](#examples-3)
* [trackScreenView](#trackscreenview)
* [Parameters](#parameters-3)
* [Examples](#examples-4)
* [trackEvent](#trackevent)
* [Parameters](#parameters-4)
* [Examples](#examples-5)
* [trackTiming](#tracktiming)
* [Parameters](#parameters-5)
* [Examples](#examples-6)
* [trackException](#trackexception)
* [Parameters](#parameters-6)
* [Examples](#examples-7)
* [trackSocialInteraction](#tracksocialinteraction)
* [Parameters](#parameters-7)
* [Examples](#examples-8)
* [setUser](#setuser)
* [Parameters](#parameters-8)
* [Examples](#examples-9)
* [setClient](#setclient)
* [Parameters](#parameters-9)
* [Examples](#examples-10)
* [getClientId](#getclientid)
* [Examples](#examples-11)
* [allowIDFA](#allowidfa)
* [Parameters](#parameters-10)
* [Examples](#examples-12)
* [setAppName](#setappname)
* [Parameters](#parameters-11)
* [Examples](#examples-13)
* [setAppVersion](#setappversion)
* [Parameters](#parameters-12)
* [Examples](#examples-14)
* [setAnonymizeIp](#setanonymizeip)
* [Parameters](#parameters-13)
* [Examples](#examples-15)
* [setSamplingRate](#setsamplingrate)
* [Parameters](#parameters-14)
* [Examples](#examples-16)
* [setCurrency](#setcurrency)
* [Parameters](#parameters-15)
* [Examples](#examples-17)
* [setTrackUncaughtExceptions](#settrackuncaughtexceptions)
* [Parameters](#parameters-16)
* [dispatch](#dispatch)
* [Examples](#examples-18)
* [dispatchWithTimeout](#dispatchwithtimeout)
* [Parameters](#parameters-17)
* [Examples](#examples-19)
* [EventMetadata](#eventmetadata)
* [Parameters](#parameters-18)
* [Examples](#examples-20)
* [TimingMetadata](#timingmetadata)
* [Parameters](#parameters-19)
* [Examples](#examples-21)
* [HitPayload](#hitpayload)
* [Parameters](#parameters-20)
* [Examples](#examples-22)
* [CustomDimensionsFieldIndexMap](#customdimensionsfieldindexmap)
* [Examples](#examples-23)
* [CustomDimensionsByIndex](#customdimensionsbyindex)
* [Examples](#examples-24)
* [CustomDimensionsByField](#customdimensionsbyfield)
* [Examples](#examples-25)
* [CustomMetrics](#custommetrics)
* [Examples](#examples-26)
* [DataLayerEvent](#datalayerevent)
* [Parameters](#parameters-21)
* [Examples](#examples-27)
* [ProductActionEnum](#productactionenum)
* [Product](#product)
* [Parameters](#parameters-22)
* [Examples](#examples-28)
* [Transaction](#transaction)
* [Parameters](#parameters-23)
* [Examples](#examples-29)
* [ProductAction](#productaction)
* [Parameters](#parameters-24)
* [Examples](#examples-30)
### GoogleAnalyticsSettings
Settings which are applied across all trackers.
#### setOptOut
Sets if OptOut is active and disables Google Analytics. This is disabled by default. Note: This has to be set each time the App starts.
##### Parameters
* `enabled` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
##### Examples
```javascript
GoogleAnalyticsSettings.setOptOut(true);
```
#### setDispatchInterval
Sets the trackers dispatch interval.
Events, screen views, etc, are sent in batches to your tracker. This function allows you to configure how often (in seconds) the batches are sent to your tracker. Recommended to keep this around 20-120 seconds to preserve battery and network traffic. This is set to 20 seconds by default.
##### Parameters
* `intervalInSeconds` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**
##### Examples
```javascript
GoogleAnalyticsSettings.setDispatchInterval(30);
```
#### setDryRun
When enabled the native library prevents any data from being sent to Google Analytics. This allows you to test or debug the implementation, without your test data appearing in your Google Analytics reports.
##### Parameters
* `enabled` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
##### Examples
```javascript
GoogleAnalyticsSettings.setDryRun(true);
```
### GoogleAnalyticsTracker
#### Examples
```javascript
// Constructing a tracker is simple:
import { GoogleAnalyticsTracker } from "rn-google-analytics";
const tracker = new GoogleAnalyticsTracker("UA-12345-1");
tracker.trackScreenView("Home");
// You can have multiple trackers if you have several tracking ids
const tracker2 = new GoogleAnalyticsTracker("UA-12345-2");
```
```javascript
// One optional feature as well is constructing a tracker with a CustomDimensionsFieldIndexMap, to map custom dimension field names to index keys:
const fieldIndexMap = { customerType: 1 };
const tracker3 = new GoogleAnalyticsTracker("UA-12345-3", fieldIndexMap);
// This is because the Google Analytics API expects custom dimensions to be tracked by index keys, and not field names.
// Here the underlying logic will transform the custom dimension, so what ends up being sent to GA is { 1: 'Premium' }:
tracker3.trackScreenView("Home", { customDimensions: { customerType: "Premium" } });
// If you do not use a CustomDimensionsFieldIndexMap, you will have to use index as keys instead for custom dimensions:
tracker.trackScreenView("Home", { customDimensions: { 1: "Premium" } });
```
#### trackScreenView
Track the current screen/view. Calling this will also set the "current view" for other calls.
So events tracked will be tagged as having occured on the current view, `Home` in this example.
This means it is important to track navigation, especially if events can fire on different views.
##### Parameters
* `screenName` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Required) The name of the current screen
* `payload` **[HitPayload](#hitpayload)** (Optional) An object containing the hit payload (optional, default `null`)
##### Examples
```javascript
tracker.trackScreenView('Home');
```
```javascript
// With payload:
const payload = { impressionList: "Sale", impressionProducts: [ { id: "PW928", name: "Premium bundle" } ] };
tracker.trackScreenView("SplashModal", payload);
```
#### trackEvent
Track an event that has occured
##### Parameters
* `category` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Required) The event category
* `action` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Required) The event action
* `eventMetadata` **[EventMetadata](#eventmetadata)** (Optional) An object containing event metadata
* `payload` **[HitPayload](#hitpayload)** (Optional) An object containing the hit payload (optional, default `null`)
##### Examples
```javascript
tracker.trackEvent("DetailsButton", "Click");
```
```javascript
// Track event with label and value
tracker.trackEvent("AppVersionButton", "Click", { label: "v1.0.3", value: 22 });
```
```javascript
// Track with a payload (ecommerce in this case):
const product = {
id: "P12345",
name: "Android Warhol T-Shirt",
category: "Apparel/T-Shirts",
brand: "Google",
variant: "Black",
price: 29.2,
quantity: 1,
couponCode: "APPARELSALE"
};
const transaction = {
id: "T12345",
affiliation: "Google Store - Online",
revenue: 37.39,
tax: 2.85,
shipping: 5.34,
couponCode: "SUMMER2013"
};
const productAction = {
transaction,
action: 7 // Purchase action, see ProductActionEnum
}
const payload = { products: [ product ], productAction: productAction }
tracker.trackEvent("FinalizeOrderButton", "Click", null, payload);
```
#### trackTiming
Track a timing measurement
##### Parameters
* `category` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Required) The event category
* `interval` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (Required) The timing measurement in milliseconds
* `timingMetadata` **[TimingMetadata](#timingmetadata)** (Required) An object containing timing metadata
* `payload` **[HitPayload](#hitpayload)** (Optional) An object containing the hit payload (optional, default `null`)
##### Examples
```javascript
tracker.trackTiming("testcategory", 2000, { name: "LoadList" }); // name metadata is required
```
```javascript
// With optional label:
tracker.trackTiming("testcategory", 2000, { name: "LoadList", label: "v1.0.3" });
```
#### trackException
Track an exception
##### Parameters
* `error` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Required) The description of the error
* `fatal` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (Optional) A value indiciating if the error was fatal, defaults to false (optional, default `false`)
* `payload` **[HitPayload](#hitpayload)** (Optional) An object containing the hit payload (optional, default `null`)
##### Examples
```javascript
try {
...
} catch(error) {
tracker.trackException(error.message, false);
}
```
#### trackSocialInteraction
Track a social interaction, Facebook, Twitter, etc.
##### Parameters
* `network` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
* `action` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
* `targetUrl` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
* `payload` **[HitPayload](#hitpayload)** (Optional) An object containing the hit payload
##### Examples
```javascript
tracker.trackSocialInteraction("Twitter", "Post");
```
#### setUser
Sets the current userId for tracking.
##### Parameters
* `userId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** An anonymous identifier that complies with Google Analytic's user ID policy
##### Examples
```javascript
tracker.setUser("12345678");
```
#### setClient
Sets the current clientId for tracking.
##### Parameters
* `clientId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A anonymous identifier that complies with Google Analytic's client ID policy
##### Examples
```javascript
tracker.setClient("35009a79-1a05-49d7-b876-2b884d0f825b");
```
#### getClientId
Get the client id to be used for purpose of logging etc.
##### Examples
```javascript
tracker.getClientId().then(clientId => console.log("Client id is: ", clientId));
```
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**
#### allowIDFA
Also called advertising identifier collection, and is used for advertising features.
**Important**: For iOS you can only use this method if you have done the optional step 6 from the installation guide. Only enable this (and link the appropriate libraries) if you plan to use advertising features in your app, or else your app may get rejected from the AppStore.
##### Parameters
* `enabled` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (Optional) Defaults to true (optional, default `true`)
##### Examples
```javascript
tracker.allowIDFA(true);
```
#### setAppName
Overrides the app name logged in Google Analytics. The Bundle name is used by default. Note: This has to be set each time the App starts.
##### Parameters
* `appName` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Required)
##### Examples
```javascript
tracker.setAppName("YourAwesomeApp");
```
#### setAppVersion
Sets the trackers appVersion
##### Parameters
* `appVersion` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Required)
##### Examples
```javascript
tracker.setAppVersion("1.3.2");
```
#### setAnonymizeIp
Sets if AnonymizeIp is enabled
If enabled the last octet of the IP address will be removed
##### Parameters
* `enabled` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (Required)
##### Examples
```javascript
tracker.setAnonymizeIp(true);
```
#### setSamplingRate
Sets tracker sampling rate.
##### Parameters
* `sampleRatio` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (Required) Percentage 0 - 100
##### Examples
```javascript
tracker.setSamplingRate(50);
```
#### setCurrency
Sets the currency for tracking.
##### Parameters
* `currencyCode` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Required) The currency ISO 4217 code
##### Examples
```javascript
tracker.setCurrency("EUR");
```
#### setTrackUncaughtExceptions
Sets if uncaught exceptions should be tracked
Important to note: On iOS this option is set on all trackers. On Android it is set per tracker.
If you are using multiple trackers on iOS, this will enable & disable on all trackers.
##### Parameters
* `enabled` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
#### dispatch
This function lets you manually dispatch all hits which are queued.
Use this function sparingly, as it will normally happen automatically
as a batch. This function will also dispatch for all trackers.
##### Examples
```javascript
tracker.dispatch().then(done => console.log("Dispatch is done: ", done));
```
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>** Returns when done
#### dispatchWithTimeout
The same as `dispatch`, but also gives you the ability to time out
the Promise in case dispatch takes too long.
##### Parameters
* `timeout` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** The timeout. Default value is 15 sec. (optional, default `-1`)
##### Examples
```javascript
tracker
.dispatchWithTimeout(10000)
.then(done => console.log("Dispatch is done: ", done));
```
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>** Returns when done or timed out
### EventMetadata
Used when tracking event
#### Parameters
* `label` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
* `value` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (Optional)
#### Examples
```javascript
const eventMetadata = { label: "v1.0.3", value: 22 }
tracker.trackEvent("FinalizeOrderButton", "Click", eventMetadata);
```
### TimingMetadata
Used when tracking time measurements
#### Parameters
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Required)
* `label` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
#### Examples
```javascript
const timingMetadata = { name: "LoadList" } // name is a required value when tracking timing
tracker.trackTiming("testcategory", 13000, timingMetadata);
```
### HitPayload
The HitPayload object and possible values
Used by the different tracking methods for adding metadata to the hit.
#### Parameters
* `products` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Product](#product)>** (Optional) Used for ecommerce
* `impressionProducts` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Product](#product)>** (Optional) Used for ecommerce
* `impressionList` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional) Used for ecommerce
* `impressionSource` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional) Used for ecommerce
* `productAction` **[ProductAction](#productaction)** (Optional) Used for ecommerce
* `customDimensions` **([CustomDimensionsByIndex](#customdimensionsbyindex) | [CustomDimensionsByField](#customdimensionsbyfield))** (Optional)
* `customMetrics` **[CustomMetrics](#custommetrics)** (Optional)
* `utmCampaignUrl` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional) Used for campaigns
* `session` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional) Only two possible values, "start" or "end". This will either start or end a session.
#### Examples
```javascript
// If you want to do send a purchase payload with an event:
const product = {
id: "P12345",
name: "Android Warhol T-Shirt",
category: "Apparel/T-Shirts",
brand: "Google",
variant: "Black",
price: 29.2,
quantity: 1,
couponCode: "APPARELSALE"
};
const transaction = {
id: "T12345",
affiliation: "Google Store - Online",
revenue: 37.39,
tax: 2.85,
shipping: 5.34,
couponCode: "SUMMER2013"
};
const productAction = {
transaction,
action: 7 // Purchase action, see ProductActionEnum
}
const payload = { products: [ product ], productAction: productAction }
tracker.trackEvent("FinalizeOrderButton", "Click", null, payload);
```
```javascript
// If you want to send custom dimensions with a screen view:
const customDimensions = {
1: "Beta",
3: "Premium"
};
const payload = { customDimensions };
tracker.trackScreenView("SaleScreen", payload);
```
### CustomDimensionsFieldIndexMap
* **See**: CustomDimensionsFieldIndexMap
* **See**: CustomDimensionsByField
A dictionary describing mapping of field names to indices for custom dimensions.
This is an optional object used by the tracker.
#### Examples
```javascript
// Create something like:
const fieldIndexMap = { customerType: 1 };
// Construct tracker with it:
const tracker = new GoogleAnalyticsTracker("UA-12345-3", fieldIndexMap);
// This allows you to send in customDimensions in the`HitPayload by field name instead of index:
tracker.trackScreenView("Home", { customDimensions: { customerType: "Premium" } });
// If you do not provide a map, you instead have to send in by index:
tracker.trackScreenView("Home", { customDimensions: { 1: "Premium" } });
```
### CustomDimensionsByIndex
* **See**: CustomDimensionsFieldIndexMap
* **See**: CustomDimensionsByField
A dictionary with custom dimensions values and their index keys.
#### Examples
```javascript
const customDimensions = { 1: "Premium", 3: "Beta", 5: 1200 }
tracker.trackScreenView("Home", { customDimensions });
```
### CustomDimensionsByField
* **See**: CustomDimensionsFieldIndexMap
* **See**: CustomDimensionsByIndex
A dictionary with custom dimensions values and their (mapped) field name keys.
In order to use this and send in custom dimensions by field name, you must have
provided a `CustomDimensionsFieldIndexMap` when constructing the tracker.
#### Examples
```javascript
const customDimensions = { customerType: "Premium", appType: "Beta", credit: 1200 }
tracker.trackScreenView("Home", { customDimensions });
```
### CustomMetrics
A dictionary with custom metric values and their index keys.
#### Examples
```javascript
const customMetrics = { 1: 2389, 4: 15000 }
tracker.trackScreenView("Home", { customMetrics });
```
### DataLayerEvent
The Google Tag Manager DataLayerEvent dictionary.
Populate this event-object with values to push to the DataLayer. The only required property is `event`.
#### Parameters
* `event` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
#### Examples
```javascript
const dataLayerEvent = {
event: "eventName",
pageId: "/home"
};
GoogleTagManager.pushDataLayerEvent(dataLayerEvent);
```
### ProductActionEnum
Enhanced Ecommerce ProductActionEnum
Used by `ProductAction` when describing the type of product action. The possible values (numbers) are:
* Detail = 1,
* Click = 2,
* Add = 3,
* Remove = 4,
* Checkout = 5,
* CheckoutOption = 6,
* Purchase = 7,
* Refund = 8
Type: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)
### Product
Enhanced Ecommerce Product
Used by `HitPayload` when populating product actions or impressions
#### Parameters
* `id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
* `category` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
* `brand` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
* `variant` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
* `price` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (Optional)
* `couponCode` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
* `quantity` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (Optional)
#### Examples
```javascript
const product = {
id: "P12345",
name: "Android Warhol T-Shirt",
category: "Apparel/T-Shirts",
brand: "Google",
variant: "Black",
price: 29.2,
quantity: 1,
couponCode: "APPARELSALE"
};
```
### Transaction
Enhanced Ecommerce Transaction
Used by `ProductAction` when populating describing a purchase/transaction
#### Parameters
* `id` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
* `affiliation` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
* `revenue` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (Optional - but not really)
* `tax` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (Optional)
* `shipping` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (Optional)
* `couponCode` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
#### Examples
```javascript
const transaction = {
id: "T12345",
affiliation: "Google Store - Online",
revenue: 37.39,
tax: 2.85,
shipping: 5.34,
couponCode: "SUMMER2013"
};
```
### ProductAction
Enhanced Ecommerce Product Action
Used by `HitPayload` when describing a product action
#### Parameters
* `action` **[ProductActionEnum](#productactionenum)**
* `transaction` **[Transaction](#transaction)** (Optional)
* `checkoutStep` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** (Optional)
* `checkoutOption` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
* `productActionList` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
* `productListSource` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (Optional)
#### Examples
```javascript
const productAction = {
transaction,
action: 7 // Purchase action, see ProductActionEnum
}
```
```javascript
const productAction = {
action: 3 // Add action, see ProductActionEnum
}
```