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