UNPKG

8.97 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 * Optional alias for this event
85 * @type {?string}
86 */
87 alias = null
88
89 /**
90 * Constructs a new BranchEvent from arguments
91 *
92 * @param {!string} name - The name of the event. May be a standard Branch event
93 * or a custom event name.
94 * @param {?(Object|Object[])} contentItems - One or more Branch Universal Objects associated with this event, or null.
95 * @param {?Object} params - Object containing params to be set in the constructor
96 * @param {?string} params.transactionID - Initial value for the transactionID property
97 * @param {?string} params.currency - Initial value for the currency property
98 * @param {?(string|number)} params.revenue - Initial value for the revenue property
99 * @param {?(string|number)} params.shipping - Initial value for the shipping property
100 * @param {?(string|number)} params.tax - Initial value for the tax property
101 * @param {?string} params.coupon - Initial value for the coupon property
102 * @param {?string} params.affiliation - Initial value for the affiliation property
103 * @param {?string} params.description - Initial value for the description property
104 * @param {?string} params.searchQuery - Initial value for the searchQuery property
105 * @param {?Object} params.customData - Initial value for the customData property
106 */
107 constructor(name, contentItems = null, params = {}) {
108 this.name = name
109
110 if (Array.isArray(contentItems)) {
111 this.contentItems = contentItems
112 }
113 else if (contentItems) {
114 this.contentItems = [contentItems]
115 }
116
117 if (params.transactionID) this.transactionID = params.transactionID
118 if (params.currency) this.currency = params.currency
119 if (params.revenue) this.revenue = params.revenue
120 if (params.shipping) this.shipping = params.shipping
121 if (params.tax) this.tax = params.tax
122 if (params.coupon) this.coupon = params.coupon
123 if (params.affiliation) this.affiliation = params.affiliation
124 if (params.description) this.description = params.description
125 if (params.searchQuery) this.searchQuery = params.searchQuery
126 if (params.customData) this.customData = params.customData
127 if (params.alias) this.alias = params.alias
128 }
129
130 /**
131 * Log this event. This method is always successful. It queues events to be
132 * transmitted whenever the service is available. It returns a promise that
133 * is resolved once the native logEvent call is complete. The promise always
134 * returns null.
135 *
136 * @return {null} Always returns null
137 */
138 async logEvent() {
139 const idents = this.contentItems.map((b) => b.ident)
140
141 try {
142 return await RNBranch.logEvent(idents, this.name, this._convertParams())
143 }
144 catch (error) {
145 if (error.code != 'RNBranch::Error::BUONotFound') {
146 // This is the only reason this promise should ever be rejected,
147 // but in case anything else is ever thrown, throw it out to the
148 // caller.
149 throw error
150 }
151
152 // Native BUO not found (expired from cache). Find the JS instance and
153 // have it create a new native instance with a new ident.
154 const ident = this._identFromMessage(error.message)
155 const buo = this.contentItems.find((b) => b.ident == ident)
156 await buo._newIdent()
157
158 // Now that a fresh BUO has been created, call this method again.
159 return await this.logEvent()
160 }
161 }
162
163 // Parse the ident of the missing BUO out of the error text.
164 _identFromMessage(message) {
165 const match = /^.*ident\s([A-Fa-f0-9-]+).*$/.exec(message)
166 if (match) return match[1]
167 return null
168 }
169
170 _convertParams() {
171 let params = {}
172
173 if (this.transactionID) params.transactionID = this.transactionID
174 if (this.currency) params.currency = this.currency
175
176 // for the benefit of the NSDecimalNumber on iOS
177 if (this.revenue) params.revenue = '' + this.revenue
178 if (this.shipping) params.shipping = '' + this.shipping
179 if (this.tax) params.tax = '' + this.tax
180
181 if (this.coupon) params.coupon = this.coupon
182 if (this.affiliation) params.affiliation = this.affiliation
183 if (this.description) params.description = this.description
184 if (this.searchQuery) params.searchQuery = this.searchQuery
185 if (this.customData) {
186 params.customData = this.customData
187 for (const key in params.customData) {
188 const valueType = typeof params.customData[key]
189 if (valueType == 'string') continue
190 console.warn('[Branch] customMetadata values must be strings. Value for property ' + key + ' has type ' + valueType + '.')
191 // TODO: throw?
192 }
193 }
194 if (this.alias) params.alias = this.alias
195
196 return params
197 }
198}
199
200// --- Standard event definitions ---
201
202// Commerce events
203
204/**
205 * Standard Add to Cart event
206 * @type {string}
207 */
208BranchEvent.AddToCart = RNBranch.STANDARD_EVENT_ADD_TO_CART
209
210/**
211 * Standard Add to Wishlist event
212 * @type {string}
213 */
214BranchEvent.AddToWishlist = RNBranch.STANDARD_EVENT_ADD_TO_WISHLIST
215
216/**
217 * Standard View Cart event
218 * @type {string}
219 */
220BranchEvent.ViewCart = RNBranch.STANDARD_EVENT_VIEW_CART
221
222/**
223 * Standard Initiate Purchase event
224 * @type {string}
225 */
226BranchEvent.InitiatePurchase = RNBranch.STANDARD_EVENT_INITIATE_PURCHASE
227
228/**
229 * Standard Add Payment Info event
230 * @type {string}
231 */
232BranchEvent.AddPaymentInfo = RNBranch.STANDARD_EVENT_ADD_PAYMENT_INFO
233
234/**
235 * Standard Purchase event
236 * @type {string}
237 */
238BranchEvent.Purchase = RNBranch.STANDARD_EVENT_PURCHASE
239
240/**
241 * Standard View Ad event
242 * @type {string}
243 */
244BranchEvent.ViewAd = RNBranch.STANDARD_EVENT_VIEW_AD
245
246/**
247 * Standard Click Ad event
248 * @type {string}
249 */
250BranchEvent.ClickAd = RNBranch.STANDARD_EVENT_CLICK_AD
251
252// Content events
253
254/**
255 * Standard Search event
256 * @type {string}
257 */
258BranchEvent.Search = RNBranch.STANDARD_EVENT_SEARCH
259
260/**
261 * Standard View Item event for a single Branch Universal Object
262 * @type {string}
263 */
264BranchEvent.ViewItem = RNBranch.STANDARD_EVENT_VIEW_ITEM
265
266/**
267 * Standard View Items event for multiple Branch Universal Objects
268 * @type {string}
269 */
270BranchEvent.ViewItems = RNBranch.STANDARD_EVENT_VIEW_ITEMS
271
272/**
273 * Standard Rate event
274 * @type {string}
275 */
276BranchEvent.Rate = RNBranch.STANDARD_EVENT_RATE
277
278/**
279 * Standard Share event
280 * @type {string}
281 */
282BranchEvent.Share = RNBranch.STANDARD_EVENT_SHARE
283
284// User Lifecycle Events
285
286/**
287 * Standard Complete Registration event
288 * @type {string}
289 */
290BranchEvent.CompleteRegistration = RNBranch.STANDARD_EVENT_COMPLETE_REGISTRATION
291
292/**
293 * Standard Complete Tutorial event
294 * @type {string}
295 */
296BranchEvent.CompleteTutorial = RNBranch.STANDARD_EVENT_COMPLETE_TUTORIAL
297
298/**
299 * Standard Achieve Level event
300 * @type {string}
301 */
302BranchEvent.AchieveLevel = RNBranch.STANDARD_EVENT_ACHIEVE_LEVEL
303
304/**
305 * Standard Unlock Achievement event
306 * @type {string}
307 */
308BranchEvent.UnlockAchievement = RNBranch.STANDARD_EVENT_UNLOCK_ACHIEVEMENT
309
310/**
311 * Standard Invite event
312 * @type {string}
313 */
314BranchEvent.Invite = RNBranch.STANDARD_EVENT_INVITE
315
316/**
317 * Standard Login event
318 * @type {string}
319 */
320BranchEvent.Login = RNBranch.STANDARD_EVENT_LOGIN
321
322/**
323 * Standard Reserve event
324 * @type {string}
325 */
326BranchEvent.Reserve = RNBranch.STANDARD_EVENT_RESERVE
327
328/**
329 * Standard Subscribe event
330 * @type {string}
331 */
332BranchEvent.Subscribe = RNBranch.STANDARD_EVENT_SUBSCRIBE
333
334/**
335 * Standard Start Trial event
336 * @type {string}
337 */
338BranchEvent.StartTrial = RNBranch.STANDARD_EVENT_START_TRIAL