kv-test-lib 1.0.26 → 3.4.1-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +12 -0
  2. package/es5_dist/adapters/adapter.browser.js +3 -0
  3. package/es5_dist/adapters/adapter.browser.js.map +1 -0
  4. package/es5_dist/adapters/browser.js +117 -0
  5. package/es5_dist/adapters/browser.js.map +1 -0
  6. package/es5_dist/adapters/crypto.browser.js +11 -0
  7. package/es5_dist/adapters/crypto.browser.js.map +1 -0
  8. package/es5_dist/browser/browser.js +49 -0
  9. package/es5_dist/browser/browser.js.map +1 -0
  10. package/es5_dist/browser/cookies.js +36 -0
  11. package/es5_dist/browser/cookies.js.map +1 -0
  12. package/es5_dist/browser/persist.js +549 -0
  13. package/es5_dist/browser/persist.js.map +1 -0
  14. package/es5_dist/core/crypto.js +2 -0
  15. package/es5_dist/core/crypto.js.map +1 -0
  16. package/es5_dist/core/storage.js +2 -0
  17. package/es5_dist/core/storage.js.map +1 -0
  18. package/es5_dist/generated/buildInfo.js +3 -0
  19. package/es5_dist/generated/buildInfo.js.map +1 -0
  20. package/es5_dist/http.js +133 -0
  21. package/es5_dist/http.js.map +1 -0
  22. package/{dist → es5_dist}/interfaces.js +9 -7
  23. package/es5_dist/interfaces.js.map +1 -0
  24. package/es5_dist/jobqueue.js +484 -0
  25. package/es5_dist/jobqueue.js.map +1 -0
  26. package/es5_dist/kochava.js +791 -0
  27. package/es5_dist/kochava.js.map +1 -0
  28. package/es5_dist/measurementEvent.js +253 -0
  29. package/es5_dist/measurementEvent.js.map +1 -0
  30. package/es5_dist/payloads/event.js +192 -0
  31. package/es5_dist/payloads/event.js.map +1 -0
  32. package/es5_dist/payloads/identityLink.js +170 -0
  33. package/es5_dist/payloads/identityLink.js.map +1 -0
  34. package/es5_dist/payloads/install.js +324 -0
  35. package/es5_dist/payloads/install.js.map +1 -0
  36. package/es5_dist/payloads/kvinit.js +296 -0
  37. package/es5_dist/payloads/kvinit.js.map +1 -0
  38. package/{dist → es5_dist}/payloads/payload.js +9 -9
  39. package/es5_dist/payloads/payload.js.map +1 -0
  40. package/es5_dist/polyfills/browser.js +3 -0
  41. package/es5_dist/polyfills/browser.js.map +1 -0
  42. package/es5_dist/product.js +18 -0
  43. package/es5_dist/product.js.map +1 -0
  44. package/es5_dist/utils/log.js +140 -0
  45. package/es5_dist/utils/log.js.map +1 -0
  46. package/es5_dist/utils/utils.js +26 -0
  47. package/es5_dist/utils/utils.js.map +1 -0
  48. package/package.json +4 -4
  49. package/dist/browser/browser.d.ts +0 -8
  50. package/dist/browser/browser.js +0 -40
  51. package/dist/browser/cookies.d.ts +0 -3
  52. package/dist/browser/cookies.js +0 -40
  53. package/dist/browser/persist.d.ts +0 -33
  54. package/dist/browser/persist.js +0 -195
  55. package/dist/http.d.ts +0 -6
  56. package/dist/http.js +0 -23
  57. package/dist/interfaces.d.ts +0 -117
  58. package/dist/jobqueue.d.ts +0 -35
  59. package/dist/jobqueue.js +0 -220
  60. package/dist/kochava.d.ts +0 -52
  61. package/dist/kochava.js +0 -477
  62. package/dist/payloads/event.d.ts +0 -6
  63. package/dist/payloads/event.js +0 -92
  64. package/dist/payloads/identityLink.d.ts +0 -7
  65. package/dist/payloads/identityLink.js +0 -73
  66. package/dist/payloads/install.d.ts +0 -6
  67. package/dist/payloads/install.js +0 -148
  68. package/dist/payloads/kvinit.d.ts +0 -5
  69. package/dist/payloads/kvinit.js +0 -134
  70. package/dist/payloads/payload.d.ts +0 -33
  71. package/dist/utils/log.d.ts +0 -30
  72. package/dist/utils/log.js +0 -104
  73. package/dist/utils/utils.d.ts +0 -5
  74. package/dist/utils/utils.js +0 -21
@@ -0,0 +1,791 @@
1
+ var __assign = this && this.__assign || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
6
+ }
7
+ return t;
8
+ };
9
+ return __assign.apply(this, arguments);
10
+ };
11
+ var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
12
+ function adopt(value) {
13
+ return value instanceof P ? value : new P(function (resolve) {
14
+ resolve(value);
15
+ });
16
+ }
17
+ return new (P || (P = Promise))(function (resolve, reject) {
18
+ function fulfilled(value) {
19
+ try {
20
+ step(generator.next(value));
21
+ } catch (e) {
22
+ reject(e);
23
+ }
24
+ }
25
+ function rejected(value) {
26
+ try {
27
+ step(generator["throw"](value));
28
+ } catch (e) {
29
+ reject(e);
30
+ }
31
+ }
32
+ function step(result) {
33
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
34
+ }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ };
38
+ var __generator = this && this.__generator || function (thisArg, body) {
39
+ var _ = { label: 0, sent: function () {
40
+ if (t[0] & 1) throw t[1];return t[1];
41
+ }, trys: [], ops: [] },
42
+ f,
43
+ y,
44
+ t,
45
+ g;
46
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
47
+ return this;
48
+ }), g;
49
+ function verb(n) {
50
+ return function (v) {
51
+ return step([n, v]);
52
+ };
53
+ }
54
+ function step(op) {
55
+ if (f) throw new TypeError("Generator is already executing.");
56
+ while (_) try {
57
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
58
+ if (y = 0, t) op = [op[0] & 2, t.value];
59
+ switch (op[0]) {
60
+ case 0:case 1:
61
+ t = op;break;
62
+ case 4:
63
+ _.label++;return { value: op[1], done: false };
64
+ case 5:
65
+ _.label++;y = op[1];op = [0];continue;
66
+ case 7:
67
+ op = _.ops.pop();_.trys.pop();continue;
68
+ default:
69
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
70
+ _ = 0;continue;
71
+ }
72
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
73
+ _.label = op[1];break;
74
+ }
75
+ if (op[0] === 6 && _.label < t[1]) {
76
+ _.label = t[1];t = op;break;
77
+ }
78
+ if (t && _.label < t[2]) {
79
+ _.label = t[2];_.ops.push(op);break;
80
+ }
81
+ if (t[2]) _.ops.pop();
82
+ _.trys.pop();continue;
83
+ }
84
+ op = body.call(thisArg, _);
85
+ } catch (e) {
86
+ op = [6, e];y = 0;
87
+ } finally {
88
+ f = t = 0;
89
+ }
90
+ if (op[0] & 5) throw op[1];return { value: op[0] ? op[1] : void 0, done: true };
91
+ }
92
+ };
93
+ /*
94
+ Authored by Jacob Bechler on 06/09/25.
95
+ Copyright (c) Kochava, Inc. All rights reserved.
96
+ */
97
+ import './polyfills/browser.js';
98
+ import 'whatwg-fetch';
99
+ import { Log } from "./utils/log";
100
+ import JobQueue from "./jobqueue";
101
+ import * as Kvinit from "./payloads/kvinit";
102
+ import * as Install from "./payloads/install";
103
+ import { KochavaMeasurementEvent } from "./measurementEvent";
104
+ export { KochavaMeasurementEventType } from "./measurementEvent";
105
+ export { Product } from "./product";
106
+ import { DEFAULTS } from "./interfaces";
107
+ import { deleteAllPersisted, readAndUpdatePersistedValue, updatePersistedValue, PersistKey, checkDuplicateIdLink, addPersistedIdLinks, checkInstallIdChange, readAndUpdateSessionCount, readAndUpdateDeviceId, readAndUpdateUTM, getItem, configureStorage } from "./browser/persist";
108
+ import * as utils from "./utils/utils";
109
+ import { getPageName } from "./browser/browser";
110
+ import adapter from './adapters/adapter.browser.js';
111
+ import { Product } from "./product";
112
+ configureStorage(adapter);
113
+ var Kochava = /** @class */function () {
114
+ // User will use the below factories instead of directly calling
115
+ // the constructor
116
+ function Kochava() {
117
+ this.product = Product.measurement;
118
+ this._resetInstance();
119
+ this._jobQueue = new JobQueue();
120
+ }
121
+ // ============================= PUBLIC =============================== //
122
+ Kochava.create = function () {
123
+ return new Kochava();
124
+ };
125
+ Kochava.createForReactNative = function () {
126
+ var kochava = new Kochava();
127
+ kochava.executeAdvancedInstruction("wrapper", JSON.stringify({ name: "ReactNative", version: "" }));
128
+ return kochava;
129
+ };
130
+ Kochava.createForNode = function () {
131
+ var kochava = new Kochava();
132
+ kochava.executeAdvancedInstruction("wrapper", JSON.stringify({ name: "Node", version: "" }));
133
+ return kochava;
134
+ };
135
+ Kochava.createForReact = function () {
136
+ var kochava = new Kochava();
137
+ kochava.executeAdvancedInstruction("wrapper", JSON.stringify({ name: "React", version: "" }));
138
+ return kochava;
139
+ };
140
+ Kochava.createForVue = function () {
141
+ var kochava = new Kochava();
142
+ kochava.executeAdvancedInstruction("wrapper", JSON.stringify({ name: "Vue", version: "" }));
143
+ return kochava;
144
+ };
145
+ Kochava.createForAngular = function () {
146
+ var kochava = new Kochava();
147
+ kochava.executeAdvancedInstruction("wrapper", JSON.stringify({ name: "Angular", version: "1.0.0" }));
148
+ return kochava;
149
+ };
150
+ /*
151
+ Set if the SDK should also store persisted values in cookie storage.
152
+ - Not all values can be persisted in cookies, such as event/idlink queues
153
+ due to size constraints.
154
+ - true = yes use cookies
155
+ - no = no don't use cookies
156
+ - defaults to false
157
+ */
158
+ Kochava.prototype.useCookies = function (condition) {
159
+ if (condition === void 0) {
160
+ condition = false;
161
+ }
162
+ this._instance.useCookies = condition;
163
+ };
164
+ /*
165
+ - Set whether the sdk shouldn't automatically send a page or should.
166
+ - true = no auto page
167
+ - false = yes auto page
168
+ - defaults to false
169
+ */
170
+ Kochava.prototype.disableAutoPage = function (condition) {
171
+ if (condition === void 0) {
172
+ condition = false;
173
+ }
174
+ this._instance.disableAutoPage = condition;
175
+ };
176
+ Kochava.prototype.buildEventWithEventType = function (type) {
177
+ if (!type) {
178
+ Log.warn("Invalid event type, ignoring call.");
179
+ return;
180
+ }
181
+ return new KochavaMeasurementEvent(this, type);
182
+ };
183
+ Kochava.prototype.buildEventWithEventName = function (eventName) {
184
+ if (!eventName) {
185
+ Log.warn("Invalid event name, ignoring call.");
186
+ return;
187
+ }
188
+ return new KochavaMeasurementEvent(this, eventName);
189
+ };
190
+ /*
191
+ The primary means for starting the sdk.
192
+ Responsibilites:
193
+ - Checks for migrations.
194
+ - Creates the sdk instance.
195
+ - Checks for persisted state.
196
+ - Determines and sends kvinit.
197
+ - Determines and sends install.
198
+ - Optionally enqueues an auto_page.
199
+ - Starts the job queue.
200
+ */
201
+ Kochava.prototype.startWithAppGuid = function (appGuid) {
202
+ this._startWithAppGuid(appGuid);
203
+ };
204
+ Kochava.prototype._startWithAppGuid = function (appGuid) {
205
+ return __awaiter(this, void 0, void 0, function () {
206
+ return __generator(this, function (_a) {
207
+ switch (_a.label) {
208
+ case 0:
209
+ if (!appGuid) {
210
+ Log.error("Invalid appGuid ".concat(appGuid, ", start failed."));
211
+ return [2 /*return*/];
212
+ }
213
+ if (!this._instance) this._resetInstance();
214
+ if (this._instance.started) {
215
+ Log.warn("Kochava SDK already started.");
216
+ return [2 /*return*/];
217
+ }
218
+ Log.diagDebug("Host called API: Start With App Guid ".concat(appGuid));
219
+ this._instance.started = true;
220
+ return [4 /*yield*/, this._checkFirstLaunchAndMigrate()];
221
+ case 1:
222
+ _a.sent();
223
+ this._initInstance(appGuid);
224
+ if (!this._instance.disableAutoPage) this.sendPageEvent();
225
+ return [4 /*yield*/, this._checkPersistedState()];
226
+ case 2:
227
+ _a.sent();
228
+ return [4 /*yield*/, this._checkPersistedKvinit()];
229
+ case 3:
230
+ _a.sent();
231
+ this._printStartupMsgs(appGuid);
232
+ return [4 /*yield*/, this._beginStart()];
233
+ case 4:
234
+ _a.sent();
235
+ return [2 /*return*/];
236
+ }
237
+ });
238
+ });
239
+ };
240
+ /*
241
+ Primary means for stopping the sdk.
242
+ Will optionally delete all persisted data, and will shutdown the
243
+ sdk and job queue.
244
+ */
245
+ Kochava.prototype.shutdown = function (deleteData) {
246
+ this._shutdown(deleteData);
247
+ };
248
+ Kochava.prototype._shutdown = function (deleteData) {
249
+ return __awaiter(this, void 0, void 0, function () {
250
+ return __generator(this, function (_a) {
251
+ switch (_a.label) {
252
+ case 0:
253
+ Log.diagDebug("Host called API: Shutdown and ".concat(deleteData ? "delete data" : "keep data"));
254
+ if (!deleteData) return [3 /*break*/, 2];
255
+ Log.debug("Deleting persisted values");
256
+ return [4 /*yield*/, deleteAllPersisted()];
257
+ case 1:
258
+ _a.sent();
259
+ _a.label = 2;
260
+ case 2:
261
+ if (!this._instance.started) {
262
+ Log.warn("SDK already shutdown.");
263
+ }
264
+ Log.info("SDK shutting down.");
265
+ Kvinit.cancelRetries();
266
+ Install.cancelRetries();
267
+ this._jobQueue.stop();
268
+ // wipe whatever was previously in the queue
269
+ this._jobQueue = new JobQueue();
270
+ this._resetInstance();
271
+ return [2 /*return*/];
272
+ }
273
+ });
274
+ });
275
+ };
276
+ /*
277
+ Changes the current logLevel.
278
+ Options include:
279
+ - Off :: No logging whatsoever
280
+ - Error :: Only critical errors
281
+ - Warn :: Only critical errors and non-critical warnings
282
+ - Info :: High-level sdk behavior logs (default)
283
+ - Debug :: More in-depth sdk behavior logs and payload logs
284
+ - Trace :: Everything, granular detail
285
+ */
286
+ Kochava.prototype.setLogLevel = function (logLevel) {
287
+ Log.diagDebug("Host called API: Set Log Level ".concat(logLevel));
288
+ Log.setLogLevel(logLevel);
289
+ };
290
+ /*
291
+ Used internally, for special SDK behavior not utilized by a client.
292
+ Examples include:
293
+ - urls :: purposefully changing an endpoint for testing
294
+ - wrapper :: overwriting the version of a wrapper
295
+ several more ...
296
+ */
297
+ Kochava.prototype.executeAdvancedInstruction = function (key, valueStr, callback) {
298
+ Log.diagDebug("Host called API: Execute Advanced Instruction ".concat(key));
299
+ switch (key) {
300
+ case "wrapper":
301
+ {
302
+ var wrapperVersion = JSON.parse(valueStr);
303
+ if (!this._instance.version) this._instance.version = "WebTracker " + Product.measurement.sdkVersion;
304
+ switch (wrapperVersion.name) {
305
+ case "Angular":
306
+ this._instance.version += " (".concat(wrapperVersion.name, " ").concat(wrapperVersion.version, ")");
307
+ break;
308
+ default:
309
+ this._instance.version += " (".concat(wrapperVersion.name, ")");
310
+ break;
311
+ }
312
+ }
313
+ break;
314
+ case "urls":
315
+ {
316
+ var overrideUrls = JSON.parse(valueStr);
317
+ if (overrideUrls.init) this._instance.overrideUrls.init = overrideUrls.init;
318
+ if (overrideUrls.event) this._instance.overrideUrls.event = overrideUrls.event;
319
+ if (overrideUrls.install) this._instance.overrideUrls.install = overrideUrls.install;
320
+ if (overrideUrls.identityLink) this._instance.overrideUrls.identityLink = overrideUrls.identityLink;
321
+ }
322
+ break;
323
+ case "urlsRestore":
324
+ {
325
+ var restoreUrls = JSON.parse(valueStr);
326
+ for (var _i = 0, restoreUrls_1 = restoreUrls; _i < restoreUrls_1.length; _i++) {
327
+ var url = restoreUrls_1[_i];
328
+ if (url === "init") this._instance.overrideUrls.init = DEFAULTS.networking.urls.init;
329
+ if (url === "event") this._instance.overrideUrls.event = DEFAULTS.networking.urls.event;
330
+ if (url === "install") this._instance.overrideUrls.install = DEFAULTS.networking.urls.install;
331
+ if (url === "identityLink") this._instance.overrideUrls.identityLink = DEFAULTS.networking.urls.identityLink;
332
+ }
333
+ }
334
+ break;
335
+ case "logFilter":
336
+ {
337
+ var disabled = JSON.parse(valueStr);
338
+ disabled.forEach(function (level) {
339
+ return Log.disableLogType(level);
340
+ });
341
+ }
342
+ break;
343
+ case "getInstance":
344
+ {
345
+ var currInstance = JSON.stringify(this._instance);
346
+ callback(currInstance);
347
+ Log.debug("capturing instance: ".concat(valueStr));
348
+ }
349
+ break;
350
+ case "logObjects":
351
+ Log.setLogObjects(JSON.parse(valueStr));
352
+ break;
353
+ default:
354
+ break;
355
+ }
356
+ };
357
+ /*
358
+ Builds and enqueues a kochava event. Must include an event_name string,
359
+ with optional event_data as either another string or object.
360
+ */
361
+ Kochava.prototype.sendEvent = function (name, data) {
362
+ this._sendEvent(name, data);
363
+ };
364
+ Kochava.prototype._sendEvent = function (name, data) {
365
+ return __awaiter(this, void 0, void 0, function () {
366
+ return __generator(this, function (_a) {
367
+ switch (_a.label) {
368
+ case 0:
369
+ Log.diagDebug("Host called API: Send Event");
370
+ if (!name) {
371
+ Log.warn("Invalid event name, ignoring call.");
372
+ return [2 /*return*/];
373
+ }
374
+ return [4 /*yield*/, this._jobQueue.enqueueEvent(this._instance, [name, data])];
375
+ case 1:
376
+ _a.sent();
377
+ return [2 /*return*/];
378
+ }
379
+ });
380
+ });
381
+ };
382
+ /*
383
+ Wraps the sendEvent call with predefined data specific to page events.
384
+ */
385
+ Kochava.prototype.sendPageEvent = function (pageName, additionalData) {
386
+ if (pageName) this.sendEvent("page", __assign({ page_name: pageName }, additionalData));else this.sendEvent("page", __assign({ page_name: getPageName() }, additionalData));
387
+ };
388
+ /*
389
+ - Registers new identity links with the sdk, either to be sent out with
390
+ the install, or standalone.
391
+ - Will update an identity link if its key already exists in storage.
392
+ - A max of 10 identity links are currently allowed to be stored
393
+ at any one time.
394
+ */
395
+ Kochava.prototype.registerIdentityLink = function (name, identifier) {
396
+ this._registerIdentityLink(name, identifier);
397
+ };
398
+ Kochava.prototype._registerIdentityLink = function (name, identifier) {
399
+ return __awaiter(this, void 0, void 0, function () {
400
+ var idLink;
401
+ return __generator(this, function (_a) {
402
+ switch (_a.label) {
403
+ case 0:
404
+ Log.diagDebug("Host called API: Register Identity Link ".concat(name));
405
+ if (!name || !identifier) {
406
+ Log.warn("Invalid identity link, ignoring call.");
407
+ return [2 /*return*/];
408
+ }
409
+ return [4 /*yield*/, checkDuplicateIdLink(name, identifier)];
410
+ case 1:
411
+ if (_a.sent()) {
412
+ Log.debug("Duplicate Identity Link found, ignoring.");
413
+ return [2 /*return*/];
414
+ }
415
+ idLink = {};
416
+ idLink[name] = identifier;
417
+ return [4 /*yield*/, addPersistedIdLinks(name, identifier, this._instance.useCookies)];
418
+ case 2:
419
+ _a.sent();
420
+ return [4 /*yield*/, getItem("com.kochava.tracker.InstallSentDate")];
421
+ case 3:
422
+ if (!_a.sent()) return [3 /*break*/, 5];
423
+ // it will be sent standalone
424
+ return [4 /*yield*/, this._jobQueue.enqueueIdLink(this._instance, idLink)];
425
+ case 4:
426
+ // it will be sent standalone
427
+ _a.sent();
428
+ _a.label = 5;
429
+ case 5:
430
+ return [2 /*return*/];
431
+ }
432
+ });
433
+ });
434
+ };
435
+ // Allows the client to attach arbitrary data to certain payloads.
436
+ Kochava.prototype.registerCustomValue = function (name, value) {
437
+ Log.diagDebug("Host called API: Register Custom Value ".concat(value ? 'setting' : 'clearing', " ").concat(name));
438
+ if (!name) {
439
+ Log.warn("Invalid custom value, ignoring call.");
440
+ return;
441
+ }
442
+ if (!value) {
443
+ this._instance.customValues = this._instance.customValues.filter(function (cv) {
444
+ return !Object.prototype.hasOwnProperty.call(cv.data, name);
445
+ });
446
+ return;
447
+ }
448
+ var dataToAdorn = {};
449
+ dataToAdorn[name] = value;
450
+ this._instance.customValues.push({ data: dataToAdorn, isDeviceId: false });
451
+ };
452
+ // Allows the client to attach arbitrary identifiers to go out in the install.
453
+ Kochava.prototype.registerCustomDeviceIdentifier = function (name, value) {
454
+ Log.diagDebug("Host called API: Register Custom Device Identifier ".concat(value ? 'setting' : 'clearing', " ").concat(name));
455
+ if (!name) {
456
+ Log.warn("Invalid custom device identifier, ignoring call.");
457
+ return;
458
+ }
459
+ if (!value) {
460
+ this._instance.customValues = this._instance.customValues.filter(function (cv) {
461
+ return !Object.prototype.hasOwnProperty.call(cv.data, name) || !cv.isDeviceId;
462
+ });
463
+ return;
464
+ }
465
+ var dataToAdorn = {};
466
+ dataToAdorn[name] = value;
467
+ this._instance.customValues.push({ data: dataToAdorn, isDeviceId: true });
468
+ };
469
+ /*
470
+ Returns whether or not the sdk is in a "started" state.
471
+ This basically amounts to if start has been called,
472
+ not if kvinit is done or the queue is started.
473
+ Likewise, shutdown will set "started" to false;
474
+ */
475
+ Kochava.prototype.getStarted = function () {
476
+ return this._instance.started;
477
+ };
478
+ /*
479
+ Returns the current kochavaDeviceId, or "" if called too early.
480
+ */
481
+ Kochava.prototype.getDeviceId = function () {
482
+ Log.diagDebug("Host called API: Get Kochava Device Id");
483
+ if (this._instance.started) return this._instance.kochavaDeviceId;else return "";
484
+ };
485
+ /*
486
+ Puts the sdk in a "sleep" state. This will stop the sdk from dequeuing
487
+ new jobs and retrying failed jobs. Different than shutdown, because the
488
+ current state is not destroyed.
489
+ */
490
+ Kochava.prototype.setSleep = function (sleep) {
491
+ return this._setSleep(sleep);
492
+ };
493
+ Kochava.prototype._setSleep = function (sleep) {
494
+ return __awaiter(this, void 0, void 0, function () {
495
+ return __generator(this, function (_a) {
496
+ switch (_a.label) {
497
+ case 0:
498
+ Log.diagDebug("Host called API: Sleep ".concat(sleep ? "Stop" : "Start"));
499
+ if (!(sleep && !this._instance.sleep)) return [3 /*break*/, 1];
500
+ // only pause if it was running
501
+ this._instance.sleep = sleep;
502
+ this._jobQueue.pause();
503
+ return [3 /*break*/, 3];
504
+ case 1:
505
+ if (!(!sleep && this._instance.sleep)) return [3 /*break*/, 3];
506
+ // only resume queueing if it was paused
507
+ this._instance.sleep = sleep;
508
+ return [4 /*yield*/, this._beginStart()];
509
+ case 2:
510
+ _a.sent();
511
+ _a.label = 3;
512
+ case 3:
513
+ return [2 /*return*/];
514
+ }
515
+ });
516
+ });
517
+ };
518
+ // ============================= =============================== //
519
+ // Unintialized/invalid state.
520
+ // start must be called before these values will be correct.
521
+ Kochava.prototype._resetInstance = function () {
522
+ this._instance = {
523
+ appGuid: "",
524
+ started: false,
525
+ installStarted: false,
526
+ kvinitDone: false,
527
+ installDone: false,
528
+ disableAutoPage: false,
529
+ useCookies: false,
530
+ sleep: false,
531
+ version: "",
532
+ buildDate: "",
533
+ overrideUrls: {
534
+ init: "",
535
+ install: "",
536
+ event: "",
537
+ identityLink: ""
538
+ },
539
+ customValues: [],
540
+ kochavaSession: "",
541
+ retryWaterfall: [],
542
+ startTimeMS: 0,
543
+ sdkDisabled: false,
544
+ utm: "",
545
+ installCount: 0,
546
+ kochavaDeviceId: "",
547
+ kochavaInstallId: "",
548
+ kochavaSessionCount: -1,
549
+ kochavaInstallDate: -1,
550
+ kochavaConfig: undefined
551
+ };
552
+ };
553
+ Kochava.prototype._initInstance = function (appGuid) {
554
+ // init instance with defaults
555
+ this._instance.appGuid = appGuid;
556
+ this._instance.disableAutoPage = this._instance.disableAutoPage || false;
557
+ this._instance.useCookies = this._instance.useCookies || false;
558
+ this._instance.version = this._instance.version || "WebTracker " + Product.measurement.sdkVersion;
559
+ this._instance.buildDate = Product.measurement.sdkBuildDate;
560
+ this._instance.kochavaSession = utils.uuidv4().substring(0, 5);
561
+ this._instance.startTimeMS = utils.getCurrTimeMS();
562
+ this._instance.retryWaterfall = [7, 30, 300, 1800];
563
+ this._instance.kochavaConfig = JSON.parse(JSON.stringify(DEFAULTS));
564
+ };
565
+ Kochava.prototype._checkFirstLaunchAndMigrate = function () {
566
+ return __awaiter(this, void 0, void 0, function () {
567
+ var firstLaunchDate, oldKvId;
568
+ return __generator(this, function (_a) {
569
+ switch (_a.label) {
570
+ case 0:
571
+ return [4 /*yield*/, readAndUpdatePersistedValue(PersistKey.FirstStartDate, this._instance.useCookies)];
572
+ case 1:
573
+ firstLaunchDate = _a.sent();
574
+ if (!!firstLaunchDate) return [3 /*break*/, 6];
575
+ return [4 /*yield*/, updatePersistedValue(PersistKey.FirstStartDate, String(utils.getCurrTimeSec()), this._instance.useCookies)];
576
+ case 2:
577
+ _a.sent();
578
+ return [4 /*yield*/, readAndUpdatePersistedValue(PersistKey.OldKvid, this._instance.useCookies)];
579
+ case 3:
580
+ oldKvId = _a.sent();
581
+ if (!oldKvId) return [3 /*break*/, 6];
582
+ return [4 /*yield*/, updatePersistedValue(PersistKey.DeviceId, oldKvId, this._instance.useCookies)];
583
+ case 4:
584
+ _a.sent();
585
+ return [4 /*yield*/, updatePersistedValue(PersistKey.InstallSentDate, JSON.stringify(utils.getCurrTimeSec()), this._instance.useCookies)];
586
+ case 5:
587
+ _a.sent();
588
+ _a.label = 6;
589
+ case 6:
590
+ return [2 /*return*/];
591
+ }
592
+ });
593
+ });
594
+ };
595
+ Kochava.prototype._checkPersistedKvinit = function () {
596
+ return __awaiter(this, void 0, void 0, function () {
597
+ var persistedKvinit, persistedKvinitStr, persistedKvinitDateStr, lastKvinitDate, refreshMin;
598
+ return __generator(this, function (_a) {
599
+ switch (_a.label) {
600
+ case 0:
601
+ persistedKvinit = {};
602
+ return [4 /*yield*/, readAndUpdatePersistedValue(PersistKey.LastKvinit, this._instance.useCookies)];
603
+ case 1:
604
+ persistedKvinitStr = _a.sent();
605
+ if (!persistedKvinitStr) return [3 /*break*/, 3];
606
+ persistedKvinit = JSON.parse(persistedKvinitStr);
607
+ if (!persistedKvinit) return [3 /*break*/, 3];
608
+ // if persisted kvinit, apply it
609
+ Log.trace("Found persisted kvinit.", persistedKvinit);
610
+ return [4 /*yield*/, Kvinit.applyKvinitResp(this._instance, persistedKvinit)];
611
+ case 2:
612
+ _a.sent();
613
+ Log.trace("KochavaConfig after persistedKvinit:", JSON.parse(JSON.stringify(this._instance.kochavaConfig)));
614
+ _a.label = 3;
615
+ case 3:
616
+ return [4 /*yield*/, readAndUpdatePersistedValue(PersistKey.KvinitSentDate, this._instance.useCookies)];
617
+ case 4:
618
+ persistedKvinitDateStr = _a.sent();
619
+ if (persistedKvinitDateStr) {
620
+ lastKvinitDate = JSON.parse(persistedKvinitDateStr);
621
+ if (lastKvinitDate) {
622
+ refreshMin = this._instance.kochavaConfig.config.refresh_minimum;
623
+ if (utils.getCurrTimeSec() - lastKvinitDate < refreshMin) this._instance.kvinitDone = true;
624
+ }
625
+ }
626
+ return [2 /*return*/];
627
+ }
628
+ });
629
+ });
630
+ };
631
+ Kochava.prototype._checkPersistedState = function () {
632
+ return __awaiter(this, void 0, void 0, function () {
633
+ var _a, _b, _c, _d, _e, installCountStr, parsedInstallCount;
634
+ return __generator(this, function (_f) {
635
+ switch (_f.label) {
636
+ case 0:
637
+ _a = this._instance;
638
+ return [4 /*yield*/, readAndUpdatePersistedValue(PersistKey.InstallSentDate, this._instance.useCookies)];
639
+ case 1:
640
+ _a.installDone = _f.sent().length > 0;
641
+ _b = this._instance;
642
+ return [4 /*yield*/, readAndUpdatePersistedValue(PersistKey.InstallId, this._instance.useCookies)];
643
+ case 2:
644
+ _b.kochavaInstallId = _f.sent();
645
+ _c = this._instance;
646
+ return [4 /*yield*/, readAndUpdateDeviceId(this._instance.useCookies)];
647
+ case 3:
648
+ _c.kochavaDeviceId = _f.sent();
649
+ _d = this._instance;
650
+ return [4 /*yield*/, readAndUpdateSessionCount(this._instance.useCookies)];
651
+ case 4:
652
+ _d.kochavaSessionCount = _f.sent();
653
+ _e = this._instance;
654
+ return [4 /*yield*/, readAndUpdateUTM(this._instance.appGuid, this._instance.useCookies)];
655
+ case 5:
656
+ _e.utm = _f.sent();
657
+ return [4 /*yield*/, readAndUpdatePersistedValue(PersistKey.InstallCount, this._instance.useCookies)];
658
+ case 6:
659
+ installCountStr = _f.sent();
660
+ parsedInstallCount = parseInt(installCountStr, 10);
661
+ this._instance.installCount = isNaN(parsedInstallCount) ? 0 : parsedInstallCount;
662
+ return [2 /*return*/];
663
+ }
664
+ });
665
+ });
666
+ };
667
+ Kochava.prototype._printStartupMsgs = function (appGuid) {
668
+ Log.diagInfo("Started SDK ".concat(this._instance.version, "\n published ").concat(this._instance.buildDate));
669
+ Log.diagInfo("The log level is set to ".concat(Log.getLogLevel()));
670
+ Log.diagDebug("This ".concat(!this._instance.installDone ? "is" : "is not", " the first tracker SDK launch"));
671
+ Log.diagDebug("The kochava device id is ".concat(this._instance.kochavaDeviceId));
672
+ Log.diagDebug("The kochava app GUID provided was ".concat(appGuid));
673
+ };
674
+ Kochava.prototype._beginStart = function () {
675
+ return __awaiter(this, void 0, void 0, function () {
676
+ return __generator(this, function (_a) {
677
+ switch (_a.label) {
678
+ case 0:
679
+ if (!!this._instance.kvinitDone) return [3 /*break*/, 2];
680
+ Log.diagDebug("A new kvinit will be sent");
681
+ return [4 /*yield*/, this.performNewKvinit()];
682
+ case 1:
683
+ _a.sent();
684
+ return [3 /*break*/, 3];
685
+ case 2:
686
+ Log.diagDebug("A new kvinit will not be sent");
687
+ _a.label = 3;
688
+ case 3:
689
+ return [4 /*yield*/, this.checkResendId()];
690
+ case 4:
691
+ // if the install_id changed and thus a new install must go out
692
+ if (_a.sent()) this._instance.installDone = false;
693
+ Log.diagDebug("The install ".concat(this._instance.installDone ? "has already" : "has not yet", " been sent"));
694
+ if (this._instance.sleep) return [2 /*return*/];
695
+ if (!!this._instance.installDone) return [3 /*break*/, 6];
696
+ return [4 /*yield*/, this.performInstall()];
697
+ case 5:
698
+ _a.sent();
699
+ _a.label = 6;
700
+ case 6:
701
+ if (!(this._instance.kvinitDone && this._instance.installDone)) return [3 /*break*/, 8];
702
+ return [4 /*yield*/, this._jobQueue.start(this._instance)];
703
+ case 7:
704
+ _a.sent();
705
+ _a.label = 8;
706
+ case 8:
707
+ return [2 /*return*/];
708
+ }
709
+ });
710
+ });
711
+ };
712
+ Kochava.prototype.performNewKvinit = function () {
713
+ return __awaiter(this, void 0, void 0, function () {
714
+ var _a, newKvinit, newKvinitStr;
715
+ return __generator(this, function (_b) {
716
+ switch (_b.label) {
717
+ case 0:
718
+ _a = this._instance;
719
+ return [4 /*yield*/, Kvinit.send(this._instance, this._instance.retryWaterfall)];
720
+ case 1:
721
+ _a.kvinitDone = _b.sent();
722
+ return [4 /*yield*/, updatePersistedValue(PersistKey.KvinitSentDate, String(utils.getCurrTimeSec()), this._instance.useCookies)];
723
+ case 2:
724
+ _b.sent();
725
+ newKvinit = {};
726
+ return [4 /*yield*/, readAndUpdatePersistedValue(PersistKey.LastKvinit, this._instance.useCookies)];
727
+ case 3:
728
+ newKvinitStr = _b.sent();
729
+ if (newKvinitStr) {
730
+ newKvinit = JSON.parse(newKvinitStr);
731
+ }
732
+ return [4 /*yield*/, Kvinit.applyKvinitResp(this._instance, newKvinit)];
733
+ case 4:
734
+ _b.sent();
735
+ Log.trace("KochavaConfig after new Kvinit:", JSON.parse(JSON.stringify(this._instance.kochavaConfig)));
736
+ return [2 /*return*/];
737
+ }
738
+ });
739
+ });
740
+ };
741
+ Kochava.prototype.checkResendId = function () {
742
+ return __awaiter(this, void 0, void 0, function () {
743
+ var resendId, needsNewInstall;
744
+ return __generator(this, function (_a) {
745
+ switch (_a.label) {
746
+ case 0:
747
+ resendId = "";
748
+ if (this._instance.kochavaConfig.install) {
749
+ resendId = this._instance.kochavaConfig.install.resend_id;
750
+ }
751
+ return [4 /*yield*/, checkInstallIdChange(resendId, this._instance.useCookies)];
752
+ case 1:
753
+ needsNewInstall = _a.sent();
754
+ if (needsNewInstall) {
755
+ Log.debug("resend_id ".concat(resendId, " found, forcing new install"));
756
+ }
757
+ return [2 /*return*/, needsNewInstall];
758
+ }
759
+ });
760
+ });
761
+ };
762
+ Kochava.prototype.performInstall = function () {
763
+ return __awaiter(this, void 0, void 0, function () {
764
+ var request, _a;
765
+ return __generator(this, function (_b) {
766
+ switch (_b.label) {
767
+ case 0:
768
+ return [4 /*yield*/, Install.build(this._instance)];
769
+ case 1:
770
+ request = _b.sent();
771
+ _a = this._instance;
772
+ return [4 /*yield*/, Install.send(this._instance, request)];
773
+ case 2:
774
+ _a.installDone = _b.sent();
775
+ if (!this._instance.installDone) return [2 /*return*/];
776
+ // If the install succeeded, remove all idLink that were passed to it
777
+ Install.onSuccess(this._instance);
778
+ return [4 /*yield*/, this._jobQueue.persistIdLinkQueue()];
779
+ case 3:
780
+ _b.sent();
781
+ return [2 /*return*/];
782
+ }
783
+ });
784
+ });
785
+ };
786
+ return Kochava;
787
+ }();
788
+ export { Kochava };
789
+ // Only here for generic Web integration
790
+ window.kochava = Kochava.create();
791
+ //# sourceMappingURL=kochava.js.map