UNPKG

8.16 kBJavaScriptView Raw
1import { NativeModules } from 'react-native'
2const { RNBranch } = NativeModules
3
4/**
5 * Class for generating standard and custom events with the Branch SDK.
6 * @example
7 * new BranchEvent(BranchEvent.ViewEvent, buo).logEvent()
8 */
9export default class BranchEvent {
10 /**
11 * The event name. May be a standard event name or a custom event name.
12 * @type {string}
13 */
14 name = null
15
16 /**
17 * Array containing any Branch Universal Objects associated with this event.
18 * @type {Object[]}
19 */
20 contentItems = []
21
22 /**
23 * Transaction ID associated with this event
24 * @type {?string}
25 */
26 transactionID = null
27
28 /**
29 * ISO currency identifier associated with this event
30 * @type {?string}
31 */
32 currency = null
33
34 /**
35 * Revenue associated with this event
36 * @type {?(string|number)}
37 */
38 revenue = null
39
40 /**
41 * Shipping cost associated with this event
42 * @type {?(string|number)}
43 */
44 shipping = null
45
46 /**
47 * Tax associated with this event
48 * @type {?(string|number)}
49 */
50 tax = null
51
52 /**
53 * Coupon associated with this event
54 * @type {?string}
55 */
56 coupon = null
57
58 /**
59 * Affiliation associated with this event
60 * @type {?string}
61 */
62 affiliation = null
63
64 /**
65 * Description of this event
66 * @type {?string}
67 */
68 description = null
69
70 /**
71 * Search query associated with this event
72 * @type {?string}
73 */
74 searchQuery = null
75
76 /**
77 * Optional object containing custom data to associate with this event.
78 * Values must be strings.
79 * @type {?Object}
80 */
81 customData = null
82
83 /**
84 * Constructs a new BranchEvent from arguments
85 *
86 * @param {!string} name - The name of the event. May be a standard Branch event
87 * or a custom event name.
88 * @param {?(Object|Object[])} contentItems - One or more Branch Universal Objects associated with this event, or null.
89 * @param {?Object} params - Object containing params to be set in the constructor
90 * @param {?string} params.transactionID - Initial value for the transactionID property
91 * @param {?string} params.currency - Initial value for the currency property
92 * @param {?(string|number)} params.revenue - Initial value for the revenue property
93 * @param {?(string|number)} params.shipping - Initial value for the shipping property
94 * @param {?(string|number)} params.tax - Initial value for the tax property
95 * @param {?string} params.coupon - Initial value for the coupon property
96 * @param {?string} params.affiliation - Initial value for the affiliation property
97 * @param {?string} params.description - Initial value for the description property
98 * @param {?string} params.searchQuery - Initial value for the searchQuery property
99 * @param {?Object} params.customData - Initial value for the customData property
100 */
101 constructor(name, contentItems = null, params = {}) {
102 this.name = name
103
104 if (Array.isArray(contentItems)) {
105 this.contentItems = contentItems
106 }
107 else if (contentItems) {
108 this.contentItems = [contentItems]
109 }
110
111 if (params.transactionID) this.transactionID = params.transactionID
112 if (params.currency) this.currency = params.currency
113 if (params.revenue) this.revenue = params.revenue
114 if (params.shipping) this.shipping = params.shipping
115 if (params.tax) this.tax = params.tax
116 if (params.coupon) this.coupon = params.coupon
117 if (params.affiliation) this.affiliation = params.affiliation
118 if (params.description) this.description = params.description
119 if (params.searchQuery) this.searchQuery = params.searchQuery
120 if (params.customData) this.customData = params.customData
121 }
122
123 /**
124 * Log this event. This method is always successful. It queues events to be
125 * transmitted whenever the service is available. It returns a promise that
126 * is resolved once the native logEvent call is complete. The promise always
127 * returns null.
128 *
129 * @return {null} Always returns null
130 */
131 async logEvent() {
132 const idents = this.contentItems.map((b) => b.ident)
133
134 try {
135 return await RNBranch.logEvent(idents, this.name, this._convertParams())
136 }
137 catch (error) {
138 if (error.code != 'RNBranch::Error::BUONotFound') {
139 // This is the only reason this promise should ever be rejected,
140 // but in case anything else is ever thrown, throw it out to the
141 // caller.
142 throw error
143 }
144
145 // Native BUO not found (expired from cache). Find the JS instance and
146 // have it create a new native instance with a new ident.
147 const ident = this._identFromMessage(error.message)
148 const buo = this.contentItems.find((b) => b.ident == ident)
149 await buo._newIdent()
150
151 // Now that a fresh BUO has been created, call this method again.
152 return await this.logEvent()
153 }
154 }
155
156 // Parse the ident of the missing BUO out of the error text.
157 _identFromMessage(message) {
158 const match = /^.*ident\s([A-Fa-f0-9-]+).*$/.exec(message)
159 if (match) return match[1]
160 return null
161 }
162
163 _convertParams() {
164 let params = {}
165
166 if (this.transactionID) params.transactionID = this.transactionID
167 if (this.currency) params.currency = this.currency
168
169 // for the benefit of the NSDecimalNumber on iOS
170 if (this.revenue) params.revenue = '' + this.revenue
171 if (this.shipping) params.shipping = '' + this.shipping
172 if (this.tax) params.tax = '' + this.tax
173
174 if (this.coupon) params.coupon = this.coupon
175 if (this.affiliation) params.affiliation = this.affiliation
176 if (this.description) params.description = this.description
177 if (this.searchQuery) params.searchQuery = this.searchQuery
178 if (this.customData) {
179 params.customData = this.customData
180 for (const key in params.customData) {
181 const valueType = typeof params.customData[key]
182 if (valueType == 'string') continue
183 console.warn('[Branch] customMetadata values must be strings. Value for property ' + key + ' has type ' + valueType + '.')
184 // TODO: throw?
185 }
186 }
187
188 return params
189 }
190}
191
192// --- Standard event definitions ---
193
194// Commerce events
195
196/**
197 * Standard Add to Cart event
198 * @type {string}
199 */
200BranchEvent.AddToCart = RNBranch.STANDARD_EVENT_ADD_TO_CART
201
202/**
203 * Standard Add to Wishlist event
204 * @type {string}
205 */
206BranchEvent.AddToWishlist = RNBranch.STANDARD_EVENT_ADD_TO_WISHLIST
207
208/**
209 * Standard View Cart event
210 * @type {string}
211 */
212BranchEvent.ViewCart = RNBranch.STANDARD_EVENT_VIEW_CART
213
214/**
215 * Standard Initiate Purchase event
216 * @type {string}
217 */
218BranchEvent.InitiatePurchase = RNBranch.STANDARD_EVENT_INITIATE_PURCHASE
219
220/**
221 * Standard Add Payment Info event
222 * @type {string}
223 */
224BranchEvent.AddPaymentInfo = RNBranch.STANDARD_EVENT_ADD_PAYMENT_INFO
225
226/**
227 * Standard Purchase event
228 * @type {string}
229 */
230BranchEvent.Purchase = RNBranch.STANDARD_EVENT_PURCHASE
231
232/**
233 * Standard Spend Credits event
234 * @type {string}
235 */
236BranchEvent.SpendCredits = RNBranch.STANDARD_EVENT_SPEND_CREDITS
237
238// Content events
239
240/**
241 * Standard Search event
242 * @type {string}
243 */
244BranchEvent.Search = RNBranch.STANDARD_EVENT_SEARCH
245
246/**
247 * Standard View Item event for a single Branch Universal Object
248 * @type {string}
249 */
250BranchEvent.ViewItem = RNBranch.STANDARD_EVENT_VIEW_ITEM
251
252/**
253 * Standard View Items event for multiple Branch Universal Objects
254 * @type {string}
255 */
256BranchEvent.ViewItems = RNBranch.STANDARD_EVENT_VIEW_ITEMS
257
258/**
259 * Standard Rate event
260 * @type {string}
261 */
262BranchEvent.Rate = RNBranch.STANDARD_EVENT_RATE
263
264/**
265 * Standard Share event
266 * @type {string}
267 */
268BranchEvent.Share = RNBranch.STANDARD_EVENT_SHARE
269
270// User Lifecycle Events
271
272/**
273 * Standard Complete Registration event
274 * @type {string}
275 */
276BranchEvent.CompleteRegistration = RNBranch.STANDARD_EVENT_COMPLETE_REGISTRATION
277
278/**
279 * Standard Complete Tutorial event
280 * @type {string}
281 */
282BranchEvent.CompleteTutorial = RNBranch.STANDARD_EVENT_COMPLETE_TUTORIAL
283
284/**
285 * Standard Achieve Level event
286 * @type {string}
287 */
288BranchEvent.AchieveLevel = RNBranch.STANDARD_EVENT_ACHIEVE_LEVEL
289
290/**
291 * Standard Unlock Achievement event
292 * @type {string}
293 */
294BranchEvent.UnlockAchievement = RNBranch.STANDARD_EVENT_UNLOCK_ACHIEVEMENT