Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trackPurchaseEvent to RN GA target #189

Merged
merged 5 commits into from Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/targets/react-native-google-analytics.md
Expand Up @@ -35,6 +35,7 @@ const gaMiddleware = createMiddleware(eventsMap, ga);

* [`screenView`](#screenview)
* [`event`](#event)
* [`purchase`](#purchase)
* [`timing`](#timing)
* [`socialInteraction`](#socialinteraction)
* [`user`](#user)
Expand Down Expand Up @@ -84,6 +85,41 @@ const someEvent = trackEvent((action, prevState, nextState) => {

<br>

#### purchase
##### Docs:
* https://developers.google.com/analytics/devguides/collection/ios/v3/ecommerce

```js
import { trackPurchase } from 'redux-beacon/targets/rn/google-analytics';

const somePurchaseEvent = trackPurchase((action, prevState, nextState) => {
return {
product: { /* fill me in */
id: /* fill me in */,
name: /* fill me in */,
category: /* (optional) */,
brand: /* (optional) */,
variant: /* (optional) */,
price: /* (optional) */,
quantity: /* (optional) */,
couponCode: /* (optional) */,
},
transaction: { /* fill me in */
id: /* fill me in */,
affiliation: /* (optional) */,
revenue: /* (optional) */,
tax: /* (optional) */,
shipping: /* (optional) */,
couponCode: /* (optional) */,
},
action: /* fill me in */,
category: /* fill me in */,
};
});
```

<br>

#### timing
##### Docs:
https://developers.google.com/analytics/devguides/collection/ios/v3/usertimings
Expand Down
Expand Up @@ -225,6 +225,34 @@ describe('Target: React Native Google Analytics', () => {
);
});

it('calls trackPurchaseEvent for purchase hitType', () => {
const events = [
{
hitType: 'purchase',
product: { id: '123', name: 'variable' },
transaction: { id: '123' },
eventCategory: 'ecommerce',
eventAction: 'purchase',
},
];

const tracker = {
trackPurchaseEvent: jest.fn(),
};

const trackingId = 'UA-XXXXXX-Y';
const trackerConstructor = () => tracker;
const target = GoogleAnalytics(trackingId, trackerConstructor);
target(events);

expect(tracker.trackPurchaseEvent).toHaveBeenCalledWith(
events[0].product,
events[0].transaction,
events[0].eventCategory,
events[0].eventAction
);
});

it('calls trackException for exception hitType', () => {
const events = [
{
Expand Down
58 changes: 53 additions & 5 deletions src/targets/react-native/google-analytics/google-analytics.js
Expand Up @@ -37,14 +37,14 @@ export function GoogleAnalytics(trackingId, GoogleAnalyticsTracker) {
event.eventCategory,
event.eventAction,
options,
event.customDimensionDict,
event.customDimensionDict
);
} else {
tracker.trackEventWithCustomDimensionValues(
event.eventCategory,
event.eventAction,
{},
event.customDimensionDict,
event.customDimensionDict
);
}
break;
Expand All @@ -58,7 +58,7 @@ export function GoogleAnalytics(trackingId, GoogleAnalyticsTracker) {
case 'pageviewCustomDimensions': {
tracker.trackScreenViewWithCustomDimensionValues(
event.page,
event.customDimensionDict,
event.customDimensionDict
);
break;
}
Expand All @@ -75,7 +75,7 @@ export function GoogleAnalytics(trackingId, GoogleAnalyticsTracker) {
tracker.trackTiming(
event.timingCategory,
event.timingValue,
options,
options
);
} else {
tracker.trackTiming(event.timingCategory, event.timingValue);
Expand All @@ -87,7 +87,7 @@ export function GoogleAnalytics(trackingId, GoogleAnalyticsTracker) {
tracker.trackSocialInteraction(
event.socialNetwork,
event.socialAction,
event.socialTarget,
event.socialTarget
);
break;
}
Expand All @@ -102,6 +102,54 @@ export function GoogleAnalytics(trackingId, GoogleAnalyticsTracker) {
break;
}

case 'purchase': {
const productOptions = {};
const optionalProductOptions = [
'category',
'brand',
'variant',
'price',
'quantity',
'couponCode',
];

const transactionOptions = {};
const optionalTransactionOptions = [
'affiliation',
'revenue',
'tax',
'shipping',
'couponCode',
];

optionalProductOptions.forEach(option => {
if (event.product[option] !== undefined) {
productOptions[option] = event.product[option];
}
});

optionalTransactionOptions.forEach(option => {
if (event.transaction[option] !== undefined) {
transactionOptions[option] = event.transaction[option];
}
});

tracker.trackPurchaseEvent(
{
id: event.product.id, // required
name: event.product.name, // required
...productOptions,
},
{
id: event.transaction.id, // required
...transactionOptions,
},
event.eventCategory, // required
event.eventAction // required
);
break;
}

case 'exception': {
tracker.trackException(event.exDescription, event.exFatal);
break;
Expand Down