-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathmparticle-instance-manager.ts
More file actions
511 lines (485 loc) · 16.7 KB
/
mparticle-instance-manager.ts
File metadata and controls
511 lines (485 loc) · 16.7 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import Polyfill from './polyfill';
import { CommerceEventType, EventType, IdentityType, PerformanceMarkType, ProductActionType, PromotionActionType } from './types';
import Constants from './constants';
import mParticleInstance, { IMParticleWebSDKInstance } from './mp-instance.js';
import _BatchValidator from './mockBatchCreator';
import MPSideloadedKit from './sideloadedKit';
import { IMParticleInstanceManager } from './sdkRuntimeModels';
import { IStore } from './store';
import { Dictionary } from '@mparticle/web-sdk';
if (!Array.prototype.forEach) {
Array.prototype.forEach = Polyfill.forEach;
}
if (!Array.prototype.map) {
Array.prototype.map = Polyfill.map;
}
if (!Array.prototype.filter) {
Array.prototype.filter = Polyfill.filter;
}
// https://go.mparticle.com/work/SQDSDKS-6768
if (!Array.isArray) {
// @ts-ignore
Array.prototype.isArray = Polyfill.isArray;
}
function mParticleInstanceManager(this: IMParticleInstanceManager) {
const self = this;
// Only leaving this here in case any clients are trying to access mParticle.Store, to prevent from throwing
this.Store = {} as IStore;
this._instances = {} as Dictionary<IMParticleWebSDKInstance>;
this.IdentityType = IdentityType;
this.EventType = EventType;
this.CommerceEventType = CommerceEventType;
this.PromotionType = PromotionActionType;
this.ProductActionType = ProductActionType;
this.MPSideloadedKit = MPSideloadedKit;
if (typeof window !== 'undefined') {
this.isIOS =
window.mParticle && window.mParticle.isIOS
? window.mParticle.isIOS
: false;
this.config =
window.mParticle && window.mParticle.config
? window.mParticle.config
: {};
}
/**
* Initializes the mParticle instance. If no instanceName is provided, an instance name of `default_instance` will be used.
* <p>
* If you'd like to initiate multiple mParticle instances, first review our <a href="https://docs.mparticle.com/developers/sdk/web/multiple-instances/">doc site</a>, and ensure you pass a unique instance name as the third argument as shown below.
* @method init
* @param {String} apiKey your mParticle assigned API key
* @param {Object} [config] an options object for additional configuration
* @param {String} [instanceName] If you are self hosting the JS SDK and working with multiple instances, you would pass an instanceName to `init`. This instance will be selected when invoking other methods. See the above link to the doc site for more info and examples.
*/
this.init = function(apiKey, config, instanceName) {
if (!config && (window.mParticle && window.mParticle.config)) {
console.warn(
'You did not pass a config object to mParticle.init(). Attempting to use the window.mParticle.config if it exists. Please note that in a future release, this may not work and mParticle will not initialize properly'
);
config = window.mParticle ? window.mParticle.config : {};
}
instanceName = (!instanceName || instanceName.length === 0
? Constants.DefaultInstance
: instanceName
).toLowerCase();
let client: IMParticleWebSDKInstance = self._instances[instanceName];
if (client === undefined) {
client = new mParticleInstance(instanceName);
self._instances[instanceName] = client;
}
client.captureTiming(PerformanceMarkType.SdkStart);
client.setLauncherInstanceGuid();
client.init(apiKey, config, instanceName);
};
this.captureTiming = function(metricsName) {
self.getInstance().captureTiming(metricsName);
}
this.getInstance = function getInstance(instanceName) {
let client: IMParticleWebSDKInstance;
if (!instanceName) {
instanceName = Constants.DefaultInstance;
client = self._instances[instanceName];
if (!client) {
client = new mParticleInstance(instanceName);
self._instances[Constants.DefaultInstance] = client;
}
return client;
} else {
client = self._instances[instanceName.toLowerCase()];
if (!client) {
console.log(
'You tried to initialize an instance named ' +
instanceName +
'. This instance does not exist. Check your instance name or initialize a new instance with this name before calling it.'
);
return null;
}
return client;
}
};
this.Rokt = self.getInstance()._RoktManager;
this.getDeviceId = function() {
return self.getInstance().getDeviceId();
};
this.setDeviceId = function(guid) {
return self.getInstance().setDeviceId(guid);
};
this.isInitialized = function() {
return self.getInstance().isInitialized();
};
this.startNewSession = function() {
self.getInstance().startNewSession();
};
this.endSession = function() {
self.getInstance().endSession();
};
this.setLogLevel = function(newLogLevel) {
self.getInstance().setLogLevel(newLogLevel);
};
this.ready = function(argument) {
self.getInstance().ready(argument);
};
this.setAppVersion = function(version) {
self.getInstance().setAppVersion(version);
};
this.getAppName = function() {
return self.getInstance().getAppName();
};
this.setAppName = function(name) {
self.getInstance().setAppName(name);
};
this.getAppVersion = function() {
return self.getInstance().getAppVersion();
};
this.getEnvironment = function() {
return self.getInstance().getEnvironment();
};
this.stopTrackingLocation = function() {
self.getInstance().stopTrackingLocation();
};
this.startTrackingLocation = function(callback) {
self.getInstance().startTrackingLocation(callback);
};
this.setPosition = function(lat, lng) {
self.getInstance().setPosition(lat, lng);
};
this.logBaseEvent = function(event, eventOptions) {
self.getInstance().logBaseEvent(event, eventOptions);
};
this.logEvent = function(
eventName,
eventType,
eventInfo,
customFlags,
eventOptions
) {
self.getInstance().logEvent(
eventName,
eventType,
eventInfo,
customFlags,
eventOptions
);
};
this.logError = function(error, attrs) {
self.getInstance().logError(error, attrs);
};
this.logLink = function(selector, eventName, eventType, eventInfo) {
self.getInstance().logLink(selector, eventName, eventType, eventInfo);
};
this.logForm = function(selector, eventName, eventType, eventInfo) {
self.getInstance().logForm(selector, eventName, eventType, eventInfo);
};
this.logPageView = function(eventName, attrs, customFlags, eventOptions) {
self.getInstance().logPageView(
eventName,
attrs,
customFlags,
eventOptions
);
};
this.upload = function() {
self.getInstance().upload();
};
this.eCommerce = {
Cart: {
add: function(product, logEventBoolean) {
self.getInstance().eCommerce.Cart.add(product, logEventBoolean);
},
remove: function(product, logEventBoolean) {
self.getInstance().eCommerce.Cart.remove(
product,
logEventBoolean
);
},
clear: function() {
self.getInstance().eCommerce.Cart.clear();
},
},
setCurrencyCode: function(code) {
self.getInstance().eCommerce.setCurrencyCode(code);
},
createProduct: function(
name,
sku,
price,
quantity,
variant,
category,
brand,
position,
coupon,
attributes
) {
return self
.getInstance()
.eCommerce.createProduct(
name,
sku,
price,
quantity,
variant,
category,
brand,
position,
coupon,
attributes
);
},
createPromotion: function(id, creative, name, position) {
return self
.getInstance()
.eCommerce.createPromotion(id, creative, name, position);
},
createImpression: function(name, product) {
return self.getInstance().eCommerce.createImpression(name, product);
},
createTransactionAttributes: function(
id,
affiliation,
couponCode,
revenue,
shipping,
tax
) {
return self
.getInstance()
.eCommerce.createTransactionAttributes(
id,
affiliation,
couponCode,
revenue,
shipping,
tax
);
},
logCheckout: function(step, options, attrs, customFlags) {
self.getInstance().eCommerce.logCheckout(
step,
options,
attrs,
customFlags
);
},
logProductAction: function(
productActionType,
product,
attrs,
customFlags,
transactionAttributes,
eventOptions
) {
self.getInstance().eCommerce.logProductAction(
productActionType,
product,
attrs,
customFlags,
transactionAttributes,
eventOptions
);
},
logPurchase: function(
transactionAttributes,
product,
clearCart,
attrs,
customFlags
) {
self.getInstance().eCommerce.logPurchase(
transactionAttributes,
product,
clearCart,
attrs,
customFlags
);
},
logPromotion: function(
type,
promotion,
attrs,
customFlags,
eventOptions
) {
self.getInstance().eCommerce.logPromotion(
type,
promotion,
attrs,
customFlags,
eventOptions
);
},
logImpression: function(impression, attrs, customFlags, eventOptions) {
self.getInstance().eCommerce.logImpression(
impression,
attrs,
customFlags,
eventOptions
);
},
logRefund: function(
transactionAttributes,
product,
clearCart,
attrs,
customFlags
) {
self.getInstance().eCommerce.logRefund(
transactionAttributes,
product,
clearCart,
attrs,
customFlags
);
},
expandCommerceEvent: function(event) {
return self.getInstance().eCommerce.expandCommerceEvent(event);
},
};
this.setSessionAttribute = function(key, value) {
self.getInstance().setSessionAttribute(key, value);
};
this.setOptOut = function(isOptingOut) {
self.getInstance().setOptOut(isOptingOut);
};
this.setIntegrationAttribute = function(integrationId, attrs) {
self.getInstance().setIntegrationAttribute(integrationId, attrs);
};
this.getIntegrationAttributes = function(moduleId) {
return self.getInstance().getIntegrationAttributes(moduleId);
};
this.Identity = {
HTTPCodes: Constants.HTTPCodes,
aliasUsers: function(aliasRequest, callback) {
self.getInstance().Identity.aliasUsers(aliasRequest, callback);
},
createAliasRequest: function(sourceUser, destinationUser, scope) {
return self
.getInstance()
.Identity.createAliasRequest(sourceUser, destinationUser, scope);
},
getCurrentUser: function() {
return self.getInstance().Identity.getCurrentUser();
},
getUser: function(mpid) {
return self.getInstance().Identity.getUser(mpid);
},
getUsers: function() {
return self.getInstance().Identity.getUsers();
},
identify: function(identityApiData, callback) {
self.getInstance().Identity.identify(identityApiData, callback);
},
login: function(identityApiData, callback) {
self.getInstance().Identity.login(identityApiData, callback);
},
logout: function(identityApiData, callback) {
self.getInstance().Identity.logout(identityApiData, callback);
},
modify: function(identityApiData, callback) {
self.getInstance().Identity.modify(identityApiData, callback);
},
};
this.sessionManager = {
getSession: function() {
return self.getInstance()._SessionManager.getSession();
},
};
this.Consent = {
createConsentState: function() {
return self.getInstance().Consent.createConsentState();
},
createGDPRConsent: function(
consented,
timestamp,
consentDocument,
location,
hardwareId
) {
return self
.getInstance()
.Consent.createGDPRConsent(
consented,
timestamp,
consentDocument,
location,
hardwareId
);
},
createCCPAConsent: function(
consented,
timestamp,
consentDocument,
location,
hardwareId
) {
return self
.getInstance()
.Consent.createGDPRConsent(
consented,
timestamp,
consentDocument,
location,
hardwareId
);
},
};
this.reset = function() {
self.getInstance().reset(self.getInstance());
};
this._resetForTests = function(MPConfig, keepPersistence) {
if (typeof keepPersistence === 'boolean') {
self.getInstance()._resetForTests(
MPConfig,
keepPersistence,
self.getInstance()
);
} else {
self.getInstance()._resetForTests(
MPConfig,
false,
self.getInstance()
);
}
};
this.configurePixel = function(settings) {
self.getInstance().configurePixel(settings);
};
this._setIntegrationDelay = function(moduleId, boolean) {
self.getInstance()._setIntegrationDelay(moduleId, boolean);
};
this._getIntegrationDelays = function() {
return self.getInstance()._getIntegrationDelays();
};
this.getVersion = function() {
return self.getInstance().getVersion();
};
this.generateHash = function(string) {
return self.getInstance().generateHash(string);
};
this.addForwarder = function(forwarder) {
self.getInstance().addForwarder(forwarder);
};
this._getActiveForwarders = function() {
return self.getInstance()._getActiveForwarders();
};
this._setWrapperSDKInfo = function(name, version) {
self.getInstance()._setWrapperSDKInfo(name, version);
};
this.registerErrorReportingService = function(service) {
self.getInstance().registerErrorReportingService(service);
};
this.registerLoggingService = function(service) {
self.getInstance().registerLoggingService(service);
};
}
const mParticleManager = new mParticleInstanceManager();
if (typeof window !== 'undefined') {
// mParticle is the global object used to access the SDK and predates instance manager,
// when mParticle was a singleton. We now support multiple instances. Calling methods
// on mParticle directly will access the default instance, but mParticle can also be used
// as the instance manager in self hosted mode.
window.mParticle = mParticleManager;
// https://go.mparticle.com/work/SQDSDKS-5053
window.mParticle._BatchValidator = new _BatchValidator();
}
export default mParticleManager;