Skip to content

Commit

Permalink
Merge pull request aksonov#69 from malaman/trackMultiProductsPurchase…
Browse files Browse the repository at this point in the history
…EventWithCustomDimensionValues

 - implement trackMultiProductsPurchaseEventWithCustomDimensionValues
  • Loading branch information
cbrevik authored Oct 21, 2016
2 parents a82e315 + 1c7ac5a commit ff851bb
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,49 @@ GoogleAnalytics.trackPurchaseEvent({

same as trackPurchaseEvent but instead of one product you can provide an Array of products

### trackMultiProductsPurchaseEventWithCustomDimensionValues(products, transaction, eventCategory, eventAction, dimensionIndexValueDict)

* **products (required):** Array, array of products
* **transaction (required):** Object, transaction object
* **eventCategory (required):** String, defaults to "Ecommerce"
* **eventAction (required):** String, defaults to "Purchase"
* **dimensionIndexValueDict (required):** Dict of dimension index / values.

```javascript
GoogleAnalytics.trackMultiProductsPurchaseEventWithCustomDimensionValues([
{
id: 'P12345',
name: 'Android Warhol T-Shirt',
category: 'Apparel/T-Shirts',
brand: 'Google',
variant: 'Black',
price: 29.20,
quantity: 1,
couponCode: 'APPARELSALE'
},
{
id: 'P54321',
name: 'IOS T-Shirt',
category: 'Apparel/T-Shirts',
brand: 'Apple',
variant: 'Black',
price: 10.10,
quantity: 1,
couponCode: 'APPARELSALE'
}],
{
id: 'T12345',
affiliation: 'Store - Online',
revenue: 52.5,
tax: 7.86,
shipping: 5.34,
couponCode: 'SUMMER2013'
},
'Ecommerce',
'Purchase',
{'1':'premium', '5':'foo'}
);
```
### trackException(error, fatal)

* **error:** String, a description of the exception (up to 100 characters), accepts nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,33 @@ public void trackMultiProductsPurchaseEvent(String trackerId, ReadableArray prod
}
}

@ReactMethod
public void trackMultiProductsPurchaseEventWithCustomDimensionValues(String trackerId, ReadableArray products, ReadableMap transaction, String eventCategory, String eventAction, ReadableMap dimensionIndexValues) {
Tracker tracker = getTracker(trackerId);

if (tracker != null) {

HitBuilders.EventBuilder hit = new HitBuilders.EventBuilder()
.setProductAction(this.getPurchaseTransaction(transaction))
.setCategory(eventCategory)
.setAction(eventAction);

for (int i = 0; i < products.size(); i++) {
ReadableMap product = products.getMap(i);
hit.addProduct(this.getPurchaseProduct(product));
}

ReadableMapKeySetIterator iterator = dimensionIndexValues.keySetIterator();
while (iterator.hasNextKey()) {
String dimensionIndex = iterator.nextKey();
String dimensionValue = dimensionIndexValues.getString(dimensionIndex);
hit.setCustomDimension(Integer.parseInt(dimensionIndex), dimensionValue);
}

tracker.send(hit.build());
}
}

private ProductAction getPurchaseTransaction(ReadableMap transaction) {
ProductAction productAction = new ProductAction(ProductAction.ACTION_PURCHASE)
.setTransactionId(transaction.getString("id"))
Expand Down
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class GoogleAnalytics {
}

/**
* Track a purchase event. This uses the Enhanced Ecommerce GA feature.
* Track a multiproduct purchase event. This uses the Enhanced Ecommerce GA feature.
* @param {Array} products An array with products
* @param {Object} transaction An object with transaction values
* @param {String} eventCategory The event category, defaults to Ecommerce
Expand All @@ -83,6 +83,18 @@ class GoogleAnalytics {
GoogleAnalyticsBridge.trackMultiProductsPurchaseEvent(getTrackerId(), products, transaction, eventCategory, eventAction);
}

/**
* Track a multiproduct purchase event with custom dimension values. This uses the Enhanced Ecommerce GA feature.
* @param {Array} products An array of products
* @param {Object} transaction An object with transaction values
* @param {String} eventCategory The event category, defaults to Ecommerce
* @param {String} eventAction The event action, defaults to Purchase
* @param {Object} customDimensionValues An object containing custom dimension key/value pairs
*/
static trackMultiProductsPurchaseEventWithCustomDimensionValues(products = [], transaction = {}, eventCategory = "Ecommerce", eventAction = "Purchase", customDimensionValues) {
GoogleAnalyticsBridge.trackMultiProductsPurchaseEventWithCustomDimensionValues(getTrackerId(), products, transaction, eventCategory, eventAction, customDimensionValues);
}

/**
* Track an exception
* @param {String} error The description of the error
Expand Down Expand Up @@ -228,7 +240,7 @@ class GoogleTagManager {
* push a datalayer event for Google Analytics through Google Tag Manager.
* @param {String} eventName
* @param {Object} dictionaly An Map<String, Object> containing key and value pairs.
it must have atleast one key "event" with event name
it must have atleast one key "event" with event name
* example: {event: "eventName", pageId: "/home"}
*/
static pushDataLayerEvent(dictionaly = {}){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,56 @@ - (NSDictionary *)constantsToExport
[tracker send:[builder build]];
}

RCT_EXPORT_METHOD(trackMultiProductsPurchaseEventWithCustomDimensionValues:(NSString *)trackerId products:(NSArray *)products transaction:(NSDictionary *)transaction eventCategory:(NSString *)eventCategory eventAction:(NSString *)eventAction dimensionIndexValues:(NSDictionary *)dimensionIndexValues) {

id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:trackerId];
NSString *transactionId = [RCTConvert NSString:transaction[@"id"]];
NSString *transactionAffiliation = [RCTConvert NSString:transaction[@"affiliation"]];
NSNumber *transactionRevenue = [RCTConvert NSNumber:transaction[@"revenue"]];
NSNumber *transactionTax = [RCTConvert NSNumber:transaction[@"tax"]];
NSNumber *transactionShipping = [RCTConvert NSNumber:transaction[@"shipping"]];
NSString *transactionCouponCode = [RCTConvert NSString:transaction[@"couponCode"]];
GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createEventWithCategory:eventCategory
action:eventAction
label:nil
value:nil];
for (NSString *dimensionIndex in dimensionIndexValues)
[builder set:[dimensionIndexValues objectForKey:dimensionIndex] forKey:[GAIFields customDimensionForIndex:[dimensionIndex intValue]]];

GAIEcommerceProductAction *action = [[GAIEcommerceProductAction alloc] init];
[action setAction:kGAIPAPurchase];
[action setTransactionId:transactionId];
[action setAffiliation:transactionAffiliation];
[action setRevenue:transactionRevenue];
[action setTax:transactionTax];
[action setShipping:transactionShipping];
[action setCouponCode:transactionCouponCode];
[builder setProductAction:action];
for (id product in products) {
NSString *productId = [RCTConvert NSString:product[@"id"]];
NSString *productName = [RCTConvert NSString:product[@"name"]];
NSString *productBrand = [RCTConvert NSString:product[@"brand"]];
NSNumber *productPrice = [RCTConvert NSNumber:product[@"price"]];
NSString *productVariant = [RCTConvert NSString:product[@"variant"]];
NSString *productCategory = [RCTConvert NSString:product[@"category"]];
NSNumber *productQuantity = [RCTConvert NSNumber:product[@"quantity"]];
GAIEcommerceProduct *ecommerceProduct = [[GAIEcommerceProduct alloc] init];
[ecommerceProduct setId:productId];
[ecommerceProduct setName:productName];
[ecommerceProduct setCategory:productCategory];
[ecommerceProduct setBrand:productBrand];
[ecommerceProduct setVariant:productVariant];
[ecommerceProduct setPrice:productPrice];
[ecommerceProduct setQuantity:productQuantity];
if ([product objectForKey:@"couponCode"]) {
NSString *productCouponCode = [RCTConvert NSString:product[@"couponCode"]];
[ecommerceProduct setCouponCode:productCouponCode];
}
[builder addProduct:ecommerceProduct];
}
[tracker send:[builder build]];
}

RCT_EXPORT_METHOD(trackException:(NSString *)trackerId error:(NSString *)error fatal:(BOOL)fatal)
{
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:trackerId];
Expand Down

0 comments on commit ff851bb

Please sign in to comment.