Skip to content

Commit

Permalink
feat: add logProductItem (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
santiagofm committed Sep 30, 2021
1 parent b533eb5 commit 2f2b49a
Show file tree
Hide file tree
Showing 5 changed files with 470 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package com.facebook.reactnative.androidsdk;

import android.os.Bundle;

import androidx.annotation.Nullable;

import com.facebook.appevents.AppEventsConstants;
Expand All @@ -36,6 +38,7 @@
import java.math.BigDecimal;
import java.util.Currency;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
Expand Down Expand Up @@ -139,7 +142,7 @@ public String getName() {
*/
@ReactMethod
public void setFlushBehavior(String flushBehavior) {
AppEventsLogger.setFlushBehavior(AppEventsLogger.FlushBehavior.valueOf(flushBehavior.toUpperCase()));
AppEventsLogger.setFlushBehavior(AppEventsLogger.FlushBehavior.valueOf(flushBehavior.toUpperCase(Locale.ROOT)));
}

/**
Expand Down Expand Up @@ -198,6 +201,52 @@ public void logPushNotificationOpen(@Nullable ReadableMap payload) {
mAppEventLogger.logPushNotificationOpen(Arguments.toBundle(payload));
}

/**
* Uploads product catalog product item as an app event.
*
* @param itemID – Unique ID for the item. Can be a variant for a product. Max size is 100.
* @param availability – If item is in stock. Accepted values are: in stock - Item ships immediately out of stock - No plan to restock preorder - Available in future available for order - Ships in 1-2 weeks discontinued - Discontinued
* @param condition – Product condition: new, refurbished or used.
* @param description – Short text describing product. Max size is 5000.
* @param imageLink – Link to item image used in ad.
* @param link – Link to merchant's site where someone can buy the item.
* @param title – Title of item.
* @param priceAmount – Amount of purchase, in the currency specified by the 'currency' parameter. This value will be rounded to the thousandths place (e.g., 12.34567 becomes 12.346).
* @param currencyCode – Currency used to specify the amount.
* @param gtin – Global Trade Item Number including UPC, EAN, JAN and ISBN
* @param mpn – Unique manufacture ID for product
* @param brand – Name of the brand Note: Either gtin, mpn or brand is required.
* @param parameters – Optional fields for deep link specification.
*/
@ReactMethod
public void logProductItem(String itemID,
String availability,
String condition,
String description,
String imageLink,
String link,
String title,
double priceAmount,
String currencyCode,
String gtin,
String mpn,
String brand,
@Nullable ReadableMap parameters) {
mAppEventLogger.logProductItem(itemID,
AppEventsLogger.ProductAvailability.valueOf(availability.toUpperCase(Locale.ROOT)),
AppEventsLogger.ProductCondition.valueOf(condition.toUpperCase(Locale.ROOT)),
description,
imageLink,
link,
title,
BigDecimal.valueOf(priceAmount),
Currency.getInstance(currencyCode),
gtin,
mpn,
brand,
Arguments.toBundle(parameters));
}

/**
* Sets a user id to associate with all app events. This can be used to associate your own
* user id with the app events logged from this instance of an application.
Expand Down
43 changes: 43 additions & 0 deletions ios/RCTFBSDK/core/RCTFBSDKAppEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ @implementation RCTConvert (RCTFBSDKAppEvents)
@"explicit_only": @(FBSDKAppEventsFlushBehaviorExplicitOnly),
}), FBSDKAppEventsFlushBehaviorAuto, unsignedIntegerValue)

RCT_ENUM_CONVERTER(FBSDKProductAvailability, (@{
@"in_stock": @(FBSDKProductAvailabilityInStock),
@"out_of_stock": @(FBSDKProductAvailabilityOutOfStock),
@"preorder": @(FBSDKProductAvailabilityPreOrder),
@"avaliable_for_order": @(FBSDKProductAvailabilityAvailableForOrder),
@"discontinued": @(FBSDKProductAvailabilityDiscontinued),
}), FBSDKProductAvailabilityInStock, unsignedIntegerValue)

RCT_ENUM_CONVERTER(FBSDKProductCondition, (@{
@"new": @(FBSDKProductConditionNew),
@"refurbished": @(FBSDKProductConditionRefurbished),
@"used": @(FBSDKProductConditionUsed),
}), FBSDKProductConditionNew, unsignedIntegerValue)

@end

@implementation RCTFBSDKAppEvents
Expand Down Expand Up @@ -67,6 +81,35 @@ - (dispatch_queue_t)methodQueue
accessToken:nil];
}

RCT_EXPORT_METHOD(logProductItem:(NSString *)itemID
availability:(FBSDKProductAvailability)availability
condition:(FBSDKProductCondition)condition
description:(NSString *)description
imageLink:(NSString *)imageLink
link:(NSString *)link
title:(NSString *)title
priceAmount:(double)priceAmount
currency:(NSString *)currency
gtin:(NSString *)gtin
mpn:(NSString *)mpn
brand:(NSString *)brand
parameters:(NSDictionary *)parameters)
{
[FBSDKAppEvents logProductItem:itemID
availability:availability
condition:condition
description:description
imageLink:imageLink
link:link
title:title
priceAmount:priceAmount
currency:currency
gtin:gtin
mpn:mpn
brand:brand
parameters:parameters];
}

RCT_EXPORT_METHOD(logPushNotificationOpen:(NSDictionary *)payload)
{
[FBSDKAppEvents logPushNotificationOpen:payload];
Expand Down
130 changes: 130 additions & 0 deletions src/FBAppEventsLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

const AppEventsLogger = require('react-native').NativeModules.FBAppEventsLogger;
const {Platform} = require('react-native');
const {isDefined, isNumber, isOneOf, isString} = require('./util/validate');

/**
* Controls when an AppEventsLogger sends log events to the server
*/
Expand All @@ -37,6 +39,37 @@ type AppEventsFlushBehavior =
* Only flush when AppEventsLogger.flush() is explicitly invoked.
*/
| 'explicit_only';

/**
* Specifies product availability for Product Catalog product item update
*/
type ProductAvailability =
/**
* Item ships immediately
*/
| 'in_stock'
/**
* No plan to restock
*/
| 'out_of_stock'
/**
* Available in future
*/
| 'preorder'
/**
* Ships in 1-2 weeks
*/
| 'avaliable_for_order'
/**
* Discontinued
*/
| 'discontinued';

/**
* Specifies product condition for Product Catalog product item update
*/
type ProductCondition = 'new' | 'refurbished' | 'used';

type Params = {[key: string]: string | number};

/**
Expand Down Expand Up @@ -158,6 +191,103 @@ module.exports = {
AppEventsLogger.logPushNotificationOpen(payload);
},

/**
* Uploads product catalog product item as an app event
* @param itemID – Unique ID for the item. Can be a variant for a product. Max size is 100.
* @param availability – If item is in stock. Accepted values are: in stock - Item ships immediately out of stock - No plan to restock preorder - Available in future available for order - Ships in 1-2 weeks discontinued - Discontinued
* @param condition – Product condition: new, refurbished or used.
* @param description – Short text describing product. Max size is 5000.
* @param imageLink – Link to item image used in ad.
* @param link – Link to merchant's site where someone can buy the item.
* @param title – Title of item.
* @param priceAmount – Amount of purchase, in the currency specified by the 'currency' parameter. This value will be rounded to the thousandths place (e.g., 12.34567 becomes 12.346).
* @param currency – Currency used to specify the amount.
* @param gtin – Global Trade Item Number including UPC, EAN, JAN and ISBN
* @param mpn – Unique manufacture ID for product
* @param brand – Name of the brand Note: Either gtin, mpn or brand is required.
* @param parameters – Optional fields for deep link specification.
*/
logProductItem(
itemID: string,
availability: ProductAvailability,
condition: ProductCondition,
description: string,
imageLink: string,
link: string,
title: string,
priceAmount: number,
currency: string,
gtin?: ?string,
mpn?: ?string,
brand?: ?string,
parameters?: ?Params,
) {
if (!isDefined(itemID) || !isString(itemID)) {
throw new Error("logProductItem expected 'itemID' to be a string");
}
if (
!isDefined(availability) ||
!isOneOf(availability, [
'in_stock',
'out_of_stock',
'preorder',
'avaliable_for_order',
'discontinued',
])
) {
throw new Error(
"logProductItem expected 'availability' to be one of 'in_stock' | 'out_of_stock' | 'preorder' | 'avaliable_for_order' | 'discontinued'",
);
}
if (
!isDefined(condition) ||
!isOneOf(condition, ['new', 'refurbished', 'used'])
) {
throw new Error(
"logProductItem expected 'condition' to be one of 'new' | 'refurbished' | 'used'",
);
}
if (!isDefined(description) || !isString(description)) {
throw new Error("logProductItem expected 'description' to be a string");
}
if (!isDefined(imageLink) || !isString(imageLink)) {
throw new Error("logProductItem expected 'imageLink' to be a string");
}
if (!isDefined(link) || !isString(link)) {
throw new Error("logProductItem expected 'link' to be a string");
}
if (!isDefined(title) || !isString(title)) {
throw new Error("logProductItem expected 'title' to be a string");
}
if (!isDefined(priceAmount) || !isNumber(priceAmount)) {
throw new Error("logProductItem expected 'priceAmount' to be a number");
}
if (!isDefined(currency) || !isString(currency)) {
throw new Error("logProductItem expected 'currency' to be a string");
}
if (!isDefined(gtin) && !isDefined(mpn) && !isDefined(brand)) {
throw new Error(
'logProductItem expected either gtin, mpn or brand to be defined',
);
}

AppEventsLogger.logProductItem(
itemID,
availability,
condition,
description,
imageLink,
link,
title,
priceAmount,
currency,
gtin,
mpn,
brand,
parameters,
);
},

/**
* Explicitly kicks off flushing of events to Facebook.
*/
Expand Down

0 comments on commit 2f2b49a

Please sign in to comment.