Skip to content

Commit 249297d

Browse files
Convert track purchase to react native
1 parent f83c879 commit 249297d

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

Iterable.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ class IterableAttributionInfo {
6363
}
6464
}
6565

66+
class IterableCommerceItem {
67+
id: String
68+
name: String
69+
price: number
70+
quantity: number
71+
72+
constructor(id: String, name: String, price: number, quantity: number) {
73+
this.id = id
74+
this.name = name
75+
this.price = price
76+
this.quantity = quantity
77+
}
78+
}
79+
6680
enum EventName {
6781
handleUrlCalled = "handleUrlCalled",
6882
handleCustomActionCalled = "handleCustomActionCalled",
@@ -189,6 +203,17 @@ class Iterable {
189203
RNIterableAPI.trackPushOpenWithCampaignId(campaignId, templateId, messageId, appAlreadyRunning, dataFields)
190204
}
191205

206+
/**
207+
*
208+
* @param {number} total
209+
* @param {Array<IterableCommerceItem>} items
210+
* @param {any | null} dataFields
211+
*/
212+
static trackPurchase(total: number, items: Array<IterableCommerceItem>, dataFields: any | null) {
213+
console.log("trackPurchase")
214+
RNIterableAPI.trackPurchaseWithTotal(total, items, dataFields)
215+
}
216+
192217
private static createConfigDict(config: IterableConfig): any {
193218
return {
194219
"pushIntegrationName": config.pushIntegrationName,
@@ -223,4 +248,4 @@ class Iterable {
223248
}
224249
}
225250
}
226-
export { Iterable, IterableConfig, PushServicePlatform, IterableAction, IterableActionContext, IterableAttributionInfo };
251+
export { Iterable, IterableConfig, PushServicePlatform, IterableAction, IterableActionContext, IterableAttributionInfo, IterableCommerceItem };

index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class Iterable {
1313
static setAttributionInfo(attributionInfo?: IterableAttributionInfo): void
1414
static trackPushOpenWithPayload(payload: any, dataFields: any | null): void
1515
static trackPushOpenWithCampaignId(campaignId: number, templateId: number, messageId: String | null, appAlreadyRunning: Boolean, dataFields: any | null): void
16+
static trackPurchase(total: number, items: Array<IterableCommerceItem>, dataFields: any | null): void
1617
}
1718

1819
export enum PushServicePlatform {
@@ -61,3 +62,11 @@ export class IterableAttributionInfo {
6162
constructor(campaignId: number, templateId: number, messageId: String)
6263
}
6364

65+
export class IterableCommerceItem {
66+
id: String
67+
name: String
68+
price: number
69+
quantity: number
70+
71+
constructor(id: String, name: String, price: number, quantity: number)
72+
}

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* @module react-native-iterable-sdk
77
*/
88

9-
import { Iterable, IterableConfig, PushServicePlatform, IterableAction, IterableActionContext, IterableAttributionInfo } from './Iterable';
9+
import { Iterable, IterableConfig, PushServicePlatform, IterableAction, IterableActionContext, IterableAttributionInfo, IterableCommerceItem } from './Iterable';
1010

11-
export { Iterable, IterableConfig, PushServicePlatform, IterableAction, IterableActionContext, IterableAttributionInfo };
11+
export { Iterable, IterableConfig, PushServicePlatform, IterableAction, IterableActionContext, IterableAttributionInfo, IterableCommerceItem };

ios/RNIterableAPI/RNIterableAPI.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ @interface RCT_EXTERN_REMAP_MODULE(RNIterableAPI, ReactIterableAPI, NSObject)
4343
appAlreadyRunning: (BOOL) appAlreadyRunning
4444
dataFields: (NSDictionary *) dataFields)
4545

46+
RCT_EXTERN_METHOD(trackPurchaseWithTotal: (NSNumber) total
47+
items: (NSArray *) items
48+
dataFields: (NSDictionary *) dataFields)
49+
4650

4751
RCT_EXTERN_METHOD(getInAppMessages: (RCTPromiseResolveBlock) resolve
4852
rejecter: (RCTPromiseRejectBlock) reject)

ios/RNIterableAPI/ReactIterableAPI.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ class ReactIterableAPI: RCTEventEmitter {
147147
ITBInfo()
148148
IterableAPI.track(pushOpen: campaignId, templateId: templateId, messageId: messageId, appAlreadyRunning: appAlreadyRunning, dataFields: dataFields)
149149
}
150+
151+
@objc(trackPurchaseWithTotal:items:dataFields:)
152+
func trackPurchase(total: NSNumber,
153+
items: [[AnyHashable: Any]],
154+
dataFields: [AnyHashable: Any]?) {
155+
ITBInfo()
156+
IterableAPI.track(purchase: total,
157+
items: items.compactMap(ReactIterableAPI.dictionaryToCommerceItem),
158+
dataFields: dataFields)
159+
}
150160

151161
@objc(getInAppMessages:rejecter:)
152162
func getInAppMessages(resolver: RCTPromiseResolveBlock, rejecter: RCTPromiseRejectBlock) {
@@ -253,6 +263,24 @@ extension ReactIterableAPI: IterableURLDelegate {
253263
return actionDict
254264
}
255265

266+
// TODO: convert CommerceItem to Codable
267+
private static func dictionaryToCommerceItem(dict: [AnyHashable: Any]) -> CommerceItem? {
268+
guard let id = dict["id"] as? String else {
269+
return nil
270+
}
271+
guard let name = dict["name"] as? String else {
272+
return nil
273+
}
274+
guard let price = dict["price"] as? NSNumber else {
275+
return nil
276+
}
277+
guard let quantity = dict["quantity"] as? UInt else {
278+
return nil
279+
}
280+
281+
return CommerceItem(id: id, name: name, price: price, quantity: quantity)
282+
}
283+
256284
private static func codableToDictionary<T>(codable: T) -> [AnyHashable: Any]? where T: Codable {
257285
guard let data = try? JSONEncoder().encode(codable) else {
258286
return nil

0 commit comments

Comments
 (0)