@ninetailed/experience.js 2.0.2 → 2.1.1-beta.0

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.
package/index.umd.js CHANGED
@@ -1,32 +1,13 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash/get'), require('lodash/unionBy'), require('@ninetailed/experience.js-shared'), require('loglevel'), require('analytics'), require('lodash/flatten'), require('lodash/find'), require('lodash/includes'), require('lodash/pickBy'), require('murmurhash-js')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'lodash/get', 'lodash/unionBy', '@ninetailed/experience.js-shared', 'loglevel', 'analytics', 'lodash/flatten', 'lodash/find', 'lodash/includes', 'lodash/pickBy', 'murmurhash-js'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Javascript = {}, global.get$2, global.unionBy, global.experience_jsShared, global.loglevel, global.Analytics, global.flatten, global.find, global.includes, global.pickBy, global.murmurhashJs));
5
- })(this, (function (exports, get$2, unionBy, experience_jsShared, loglevel, Analytics, flatten, find, includes, pickBy, murmurhashJs) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash/get'), require('lodash/unionBy'), require('@ninetailed/experience.js-shared'), require('diary'), require('analytics'), require('lodash/flatten'), require('lodash/find'), require('lodash/includes'), require('lodash/pickBy'), require('murmurhash-js')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'lodash/get', 'lodash/unionBy', '@ninetailed/experience.js-shared', 'diary', 'analytics', 'lodash/flatten', 'lodash/find', 'lodash/includes', 'lodash/pickBy', 'murmurhash-js'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Javascript = {}, global.get$2, global.unionBy, global.experience_jsShared, global.diary, global.Analytics, global.flatten, global.find, global.includes, global.pickBy, global.murmurhashJs));
5
+ })(this, (function (exports, get$2, unionBy, experience_jsShared, diary, Analytics, flatten, find, includes, pickBy, murmurhashJs) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
9
- function _interopNamespace(e) {
10
- if (e && e.__esModule) return e;
11
- var n = Object.create(null);
12
- if (e) {
13
- Object.keys(e).forEach(function (k) {
14
- if (k !== 'default') {
15
- var d = Object.getOwnPropertyDescriptor(e, k);
16
- Object.defineProperty(n, k, d.get ? d : {
17
- enumerable: true,
18
- get: function () { return e[k]; }
19
- });
20
- }
21
- });
22
- }
23
- n["default"] = e;
24
- return Object.freeze(n);
25
- }
26
-
27
9
  var get__default = /*#__PURE__*/_interopDefaultLegacy(get$2);
28
10
  var unionBy__default = /*#__PURE__*/_interopDefaultLegacy(unionBy);
29
- var loglevel__namespace = /*#__PURE__*/_interopNamespace(loglevel);
30
11
  var Analytics__default = /*#__PURE__*/_interopDefaultLegacy(Analytics);
31
12
  var flatten__default = /*#__PURE__*/_interopDefaultLegacy(flatten);
32
13
  var find__default = /*#__PURE__*/_interopDefaultLegacy(find);
@@ -2313,15 +2294,6 @@
2313
2294
  };
2314
2295
  };
2315
2296
 
2316
- var enable = function enable() {
2317
- loglevel__namespace.enableAll();
2318
- };
2319
- var disable = function disable() {
2320
- loglevel__namespace.disableAll();
2321
- };
2322
- var Logger = loglevel__namespace.getLogger('ninetailed:experience.js');
2323
- var log = Logger.log;
2324
-
2325
2297
  var BASE_URL = 'https://api.ninetailed.co';
2326
2298
 
2327
2299
  var fetchWithTimeout = function fetchWithTimeout(input, init) {
@@ -2333,7 +2305,7 @@
2333
2305
  timeout = init.timeout;
2334
2306
  controller = new AbortController();
2335
2307
  id = setTimeout(function () {
2336
- log("Profile Request timed out.");
2308
+ logger.error(new Error('Ninetailed Request timed out.'));
2337
2309
  controller.abort();
2338
2310
  }, timeout);
2339
2311
  return [4
@@ -2373,11 +2345,11 @@
2373
2345
  return __generator(this, function (_a) {
2374
2346
  switch (_a.label) {
2375
2347
  case 0:
2376
- log('Sending Profile Request.');
2348
+ logger.info('Sending Profile Request.');
2377
2349
  events = options.events;
2378
2350
  anonymousId = events[0].anonymousId;
2379
2351
  body = options;
2380
- log('Profile Request Body: ', body);
2352
+ logger.debug('Profile Request Body: ', body);
2381
2353
  _a.label = 1;
2382
2354
 
2383
2355
  case 1:
@@ -2396,14 +2368,14 @@
2396
2368
 
2397
2369
  case 2:
2398
2370
  request = _a.sent();
2399
- log('Profile Request: ', request);
2371
+ logger.debug('Profile Request: ', request);
2400
2372
  return [4
2401
2373
  /*yield*/
2402
2374
  , request.json()];
2403
2375
 
2404
2376
  case 3:
2405
2377
  data = _a.sent().data;
2406
- log('Profile Request completed.');
2378
+ logger.debug('Profile Request completed.');
2407
2379
  return [2
2408
2380
  /*return*/
2409
2381
  , data];
@@ -2411,7 +2383,7 @@
2411
2383
  case 4:
2412
2384
  error_1 = _a.sent(); // TODO only during debug.
2413
2385
 
2414
- log(error_1);
2386
+ logger.error(error_1);
2415
2387
  throw error_1;
2416
2388
 
2417
2389
  case 5:
@@ -2426,6 +2398,262 @@
2426
2398
  return NinetailedApiClient;
2427
2399
  }();
2428
2400
 
2401
+ var Logger =
2402
+ /** @class */
2403
+ function () {
2404
+ function Logger() {
2405
+ this.name = '@ninetailed/experience.js';
2406
+ this.sinks = [];
2407
+ this.diary = diary.diary(this.name, this.onLogEvent.bind(this));
2408
+ diary.enable(this.name);
2409
+ }
2410
+
2411
+ Logger.prototype.addSink = function (sink) {
2412
+ this.sinks = __spreadArray(__spreadArray([], this.sinks, true), [sink], false);
2413
+ };
2414
+
2415
+ Logger.prototype.removeSink = function (name) {
2416
+ this.sinks = this.sinks.filter(function (sink) {
2417
+ return sink.name !== name;
2418
+ });
2419
+ };
2420
+
2421
+ Logger.prototype.debug = function (message) {
2422
+ var _a;
2423
+
2424
+ var args = [];
2425
+
2426
+ for (var _i = 1; _i < arguments.length; _i++) {
2427
+ args[_i - 1] = arguments[_i];
2428
+ }
2429
+
2430
+ (_a = this.diary).debug.apply(_a, __spreadArray([message], args, false));
2431
+ };
2432
+
2433
+ Logger.prototype.info = function (message) {
2434
+ var _a;
2435
+
2436
+ var args = [];
2437
+
2438
+ for (var _i = 1; _i < arguments.length; _i++) {
2439
+ args[_i - 1] = arguments[_i];
2440
+ }
2441
+
2442
+ (_a = this.diary).info.apply(_a, __spreadArray([message], args, false));
2443
+ };
2444
+
2445
+ Logger.prototype.log = function (message) {
2446
+ var _a;
2447
+
2448
+ var args = [];
2449
+
2450
+ for (var _i = 1; _i < arguments.length; _i++) {
2451
+ args[_i - 1] = arguments[_i];
2452
+ }
2453
+
2454
+ (_a = this.diary).log.apply(_a, __spreadArray([message], args, false));
2455
+ };
2456
+
2457
+ Logger.prototype.warn = function (message) {
2458
+ var _a;
2459
+
2460
+ var args = [];
2461
+
2462
+ for (var _i = 1; _i < arguments.length; _i++) {
2463
+ args[_i - 1] = arguments[_i];
2464
+ }
2465
+
2466
+ (_a = this.diary).warn.apply(_a, __spreadArray([message], args, false));
2467
+ };
2468
+
2469
+ Logger.prototype.error = function (message) {
2470
+ var _a;
2471
+
2472
+ var args = [];
2473
+
2474
+ for (var _i = 1; _i < arguments.length; _i++) {
2475
+ args[_i - 1] = arguments[_i];
2476
+ }
2477
+
2478
+ (_a = this.diary).error.apply(_a, __spreadArray([message], args, false));
2479
+ };
2480
+
2481
+ Logger.prototype.fatal = function (message) {
2482
+ var _a;
2483
+
2484
+ var args = [];
2485
+
2486
+ for (var _i = 1; _i < arguments.length; _i++) {
2487
+ args[_i - 1] = arguments[_i];
2488
+ }
2489
+
2490
+ (_a = this.diary).fatal.apply(_a, __spreadArray([message], args, false));
2491
+ };
2492
+
2493
+ Logger.prototype.onLogEvent = function (event) {
2494
+ this.sinks.forEach(function (sink) {
2495
+ if (sink[event.level]) {
2496
+ sink[event.level].apply(sink, __spreadArray([event.message], event.extra, false));
2497
+ }
2498
+ });
2499
+ };
2500
+
2501
+ return Logger;
2502
+ }();
2503
+ var logger = new Logger();
2504
+
2505
+ var ConsoleLogSink =
2506
+ /** @class */
2507
+ function () {
2508
+ function ConsoleLogSink() {
2509
+ this.name = "ConsoleLogSink";
2510
+ }
2511
+
2512
+ ConsoleLogSink.prototype.debug = function (message) {
2513
+ var args = [];
2514
+
2515
+ for (var _i = 1; _i < arguments.length; _i++) {
2516
+ args[_i - 1] = arguments[_i];
2517
+ }
2518
+
2519
+ console.debug.apply(console, __spreadArray([message], args, false));
2520
+ };
2521
+
2522
+ ConsoleLogSink.prototype.info = function (message) {
2523
+ var args = [];
2524
+
2525
+ for (var _i = 1; _i < arguments.length; _i++) {
2526
+ args[_i - 1] = arguments[_i];
2527
+ }
2528
+
2529
+ console.info.apply(console, __spreadArray([message], args, false));
2530
+ };
2531
+
2532
+ ConsoleLogSink.prototype.log = function (message) {
2533
+ var args = [];
2534
+
2535
+ for (var _i = 1; _i < arguments.length; _i++) {
2536
+ args[_i - 1] = arguments[_i];
2537
+ }
2538
+
2539
+ console.log.apply(console, __spreadArray([message], args, false));
2540
+ };
2541
+
2542
+ ConsoleLogSink.prototype.warn = function (message) {
2543
+ var args = [];
2544
+
2545
+ for (var _i = 1; _i < arguments.length; _i++) {
2546
+ args[_i - 1] = arguments[_i];
2547
+ }
2548
+
2549
+ console.warn.apply(console, __spreadArray([message], args, false));
2550
+ };
2551
+
2552
+ ConsoleLogSink.prototype.error = function (message) {
2553
+ var args = [];
2554
+
2555
+ for (var _i = 1; _i < arguments.length; _i++) {
2556
+ args[_i - 1] = arguments[_i];
2557
+ }
2558
+
2559
+ console.error.apply(console, __spreadArray([message], args, false));
2560
+ };
2561
+
2562
+ ConsoleLogSink.prototype.fatal = function (message) {
2563
+ var args = [];
2564
+
2565
+ for (var _i = 1; _i < arguments.length; _i++) {
2566
+ args[_i - 1] = arguments[_i];
2567
+ }
2568
+
2569
+ console.error.apply(console, __spreadArray([message], args, false));
2570
+ };
2571
+
2572
+ return ConsoleLogSink;
2573
+ }();
2574
+
2575
+ var OnLogLogSink =
2576
+ /** @class */
2577
+ function () {
2578
+ function OnLogLogSink(onLog) {
2579
+ this.onLog = onLog;
2580
+ this.name = 'OnLogLogSink';
2581
+ }
2582
+
2583
+ OnLogLogSink.prototype.debug = function (message) {
2584
+ var args = [];
2585
+
2586
+ for (var _i = 1; _i < arguments.length; _i++) {
2587
+ args[_i - 1] = arguments[_i];
2588
+ }
2589
+
2590
+ this.onLog.apply(this, __spreadArray([message], args, false));
2591
+ };
2592
+
2593
+ OnLogLogSink.prototype.info = function (message) {
2594
+ var args = [];
2595
+
2596
+ for (var _i = 1; _i < arguments.length; _i++) {
2597
+ args[_i - 1] = arguments[_i];
2598
+ }
2599
+
2600
+ this.onLog.apply(this, __spreadArray([message], args, false));
2601
+ };
2602
+
2603
+ OnLogLogSink.prototype.log = function (message) {
2604
+ var args = [];
2605
+
2606
+ for (var _i = 1; _i < arguments.length; _i++) {
2607
+ args[_i - 1] = arguments[_i];
2608
+ }
2609
+
2610
+ this.onLog.apply(this, __spreadArray([message], args, false));
2611
+ };
2612
+
2613
+ OnLogLogSink.prototype.warn = function (message) {
2614
+ var args = [];
2615
+
2616
+ for (var _i = 1; _i < arguments.length; _i++) {
2617
+ args[_i - 1] = arguments[_i];
2618
+ }
2619
+
2620
+ this.onLog.apply(this, __spreadArray([message], args, false));
2621
+ };
2622
+
2623
+ return OnLogLogSink;
2624
+ }();
2625
+
2626
+ var OnErrorLogSink =
2627
+ /** @class */
2628
+ function () {
2629
+ function OnErrorLogSink(onError) {
2630
+ this.onError = onError;
2631
+ this.name = 'OnErrorLogSink';
2632
+ }
2633
+
2634
+ OnErrorLogSink.prototype.error = function (message) {
2635
+ var args = [];
2636
+
2637
+ for (var _i = 1; _i < arguments.length; _i++) {
2638
+ args[_i - 1] = arguments[_i];
2639
+ }
2640
+
2641
+ this.onError.apply(this, __spreadArray([message], args, false));
2642
+ };
2643
+
2644
+ OnErrorLogSink.prototype.fatal = function (message) {
2645
+ var args = [];
2646
+
2647
+ for (var _i = 1; _i < arguments.length; _i++) {
2648
+ args[_i - 1] = arguments[_i];
2649
+ }
2650
+
2651
+ this.onError.apply(this, __spreadArray([message], args, false));
2652
+ };
2653
+
2654
+ return OnErrorLogSink;
2655
+ }();
2656
+
2429
2657
  var NINETAILED_TRACKER_EVENTS = {
2430
2658
  /**
2431
2659
  * `profile` - Fires when the profile is returned by the cdp API.
@@ -2433,6 +2661,7 @@
2433
2661
  profile: 'profile'
2434
2662
  };
2435
2663
  var PLUGIN_NAME = 'ninetailed';
2664
+ var DEBUG_FLAG_KEY = 'nt-debug';
2436
2665
 
2437
2666
  var delay = function delay(ms) {
2438
2667
  return new Promise(function (resolve) {
@@ -2507,7 +2736,7 @@
2507
2736
  switch (_b.label) {
2508
2737
  case 0:
2509
2738
  events = Object.assign([], queue);
2510
- log('Start flushing events.');
2739
+ logger.info('Start flushing events.');
2511
2740
  queue = [];
2512
2741
 
2513
2742
  if (!events.length) {
@@ -2524,7 +2753,7 @@
2524
2753
  case 1:
2525
2754
  _b.trys.push([1, 3,, 4]);
2526
2755
 
2527
- log('Cache: ', cache);
2756
+ logger.debug('Cache: ', cache);
2528
2757
  return [4
2529
2758
  /*yield*/
2530
2759
  , apiClient.profile({
@@ -2542,7 +2771,7 @@
2542
2771
 
2543
2772
  case 2:
2544
2773
  _a = _b.sent(), profile_1 = _a.profile, signals = _a.signals, traitsUpdatedAt = _a.traitsUpdatedAt, sessions = _a.sessions;
2545
- log('Profile from api: ', profile_1);
2774
+ logger.debug('Profile from api: ', profile_1);
2546
2775
  updatedCache = set({
2547
2776
  id: profile_1.id,
2548
2777
  random: profile_1.random,
@@ -2567,7 +2796,7 @@
2567
2796
 
2568
2797
  case 3:
2569
2798
  error_1 = _b.sent();
2570
- log('An error occurred during flushing the events: ', error_1);
2799
+ logger.debug('An error occurred during flushing the events: ', error_1);
2571
2800
  instance.dispatch({
2572
2801
  type: NINETAILED_TRACKER_EVENTS.profile,
2573
2802
  profile: {
@@ -2625,6 +2854,11 @@
2625
2854
  config: {},
2626
2855
  initialize: function (_a) {
2627
2856
  var instance = _a.instance;
2857
+
2858
+ if (instance.storage.getItem(DEBUG_FLAG_KEY)) {
2859
+ logger.addSink(new ConsoleLogSink());
2860
+ }
2861
+
2628
2862
  var cache = get({
2629
2863
  instance: instance
2630
2864
  });
@@ -2646,6 +2880,7 @@
2646
2880
  }
2647
2881
 
2648
2882
  isInitialized = true;
2883
+ logger.debug('Ninetailed Core plugin initialized.');
2649
2884
  },
2650
2885
  page: function (_a) {
2651
2886
  var payload = _a.payload,
@@ -2655,7 +2890,7 @@
2655
2890
  return __generator(this, function (_b) {
2656
2891
  switch (_b.label) {
2657
2892
  case 0:
2658
- log('Sending Page event.');
2893
+ logger.info('Sending Page event.');
2659
2894
  ctx = buildClientNinetailedRequestContext();
2660
2895
  localOnly = payload.localOnly;
2661
2896
  return [4
@@ -2687,7 +2922,7 @@
2687
2922
  return __generator(this, function (_b) {
2688
2923
  switch (_b.label) {
2689
2924
  case 0:
2690
- log('Sending Track event.');
2925
+ logger.info('Sending Track event.');
2691
2926
  ctx = buildClientNinetailedRequestContext();
2692
2927
  localOnly = payload.localOnly;
2693
2928
  return [4
@@ -2720,7 +2955,7 @@
2720
2955
  return __generator(this, function (_b) {
2721
2956
  switch (_b.label) {
2722
2957
  case 0:
2723
- log('Sending Identify event.');
2958
+ logger.info('Sending Identify event.');
2724
2959
  localOnly = payload.localOnly;
2725
2960
  ctx = buildClientNinetailedRequestContext();
2726
2961
  return [4
@@ -2797,14 +3032,58 @@
2797
3032
  });
2798
3033
  });
2799
3034
  },
2800
- debug: function (enabled) {
2801
- if (enabled) {
2802
- enable();
2803
- log('Debug mode is now enabled.');
2804
- } else {
2805
- disable();
2806
- log('Debug mode is now disabled.');
3035
+ debug: function () {
3036
+ var args = [];
3037
+
3038
+ for (var _i = 0; _i < arguments.length; _i++) {
3039
+ args[_i] = arguments[_i];
2807
3040
  }
3041
+
3042
+ return __awaiter(void 0, void 0, void 0, function () {
3043
+ var enabled, instance, consoleLogSink;
3044
+ return __generator(this, function (_a) {
3045
+ switch (_a.label) {
3046
+ case 0:
3047
+ enabled = args[0];
3048
+ instance = args[args.length - 1];
3049
+ consoleLogSink = new ConsoleLogSink();
3050
+ if (!enabled) return [3
3051
+ /*break*/
3052
+ , 2];
3053
+ return [4
3054
+ /*yield*/
3055
+ , instance.storage.setItem(DEBUG_FLAG_KEY, true)];
3056
+
3057
+ case 1:
3058
+ _a.sent(); // cleanup other console log sinks if there are already some.
3059
+
3060
+
3061
+ logger.removeSink(consoleLogSink.name);
3062
+ logger.addSink(consoleLogSink);
3063
+ logger.info('Debug mode enabled.');
3064
+ return [3
3065
+ /*break*/
3066
+ , 4];
3067
+
3068
+ case 2:
3069
+ return [4
3070
+ /*yield*/
3071
+ , instance.storage.removeItem(DEBUG_FLAG_KEY)];
3072
+
3073
+ case 3:
3074
+ _a.sent();
3075
+
3076
+ logger.info('Debug mode disabled.');
3077
+ logger.removeSink(consoleLogSink.name);
3078
+ _a.label = 4;
3079
+
3080
+ case 4:
3081
+ return [2
3082
+ /*return*/
3083
+ ];
3084
+ }
3085
+ });
3086
+ });
2808
3087
  }
2809
3088
  }
2810
3089
  };
@@ -2825,22 +3104,64 @@
2825
3104
  url = _c.url,
2826
3105
  profile = _c.profile,
2827
3106
  locale = _c.locale,
2828
- requestTimeout = _c.requestTimeout;
3107
+ requestTimeout = _c.requestTimeout,
3108
+ onLog = _c.onLog,
3109
+ onError = _c.onError;
3110
+
3111
+ this.isInitialized = false;
2829
3112
 
2830
3113
  this.page = function (data, options) {
2831
- return _this.instance.page(data, _this.buildOptions(options));
3114
+ return __awaiter(_this, void 0, void 0, function () {
3115
+ return __generator(this, function (_a) {
3116
+ switch (_a.label) {
3117
+ case 0:
3118
+ return [4
3119
+ /*yield*/
3120
+ , this.waitUntilInitialized()];
3121
+
3122
+ case 1:
3123
+ _a.sent();
3124
+
3125
+ return [2
3126
+ /*return*/
3127
+ , this.instance.page(data, this.buildOptions(options))];
3128
+ }
3129
+ });
3130
+ });
2832
3131
  };
2833
3132
 
2834
3133
  this.track = function (event, payload, options) {
2835
- return _this.instance.track(event, payload, _this.buildOptions(options));
3134
+ return __awaiter(_this, void 0, void 0, function () {
3135
+ return __generator(this, function (_a) {
3136
+ switch (_a.label) {
3137
+ case 0:
3138
+ return [4
3139
+ /*yield*/
3140
+ , this.waitUntilInitialized()];
3141
+
3142
+ case 1:
3143
+ _a.sent();
3144
+
3145
+ return [2
3146
+ /*return*/
3147
+ , this.instance.track(event, payload, this.buildOptions(options))];
3148
+ }
3149
+ });
3150
+ });
2836
3151
  };
2837
3152
 
2838
3153
  this.trackHasSeenComponent = function (payload) {
2839
- return _this.track('hasSeenComponent', payload, {
2840
- plugins: {
2841
- all: true,
2842
- ninetailed: false
2843
- }
3154
+ return __awaiter(_this, void 0, void 0, function () {
3155
+ return __generator(this, function (_a) {
3156
+ return [2
3157
+ /*return*/
3158
+ , this.track('hasSeenComponent', payload, {
3159
+ plugins: {
3160
+ all: true,
3161
+ ninetailed: false
3162
+ }
3163
+ })];
3164
+ });
2844
3165
  });
2845
3166
  };
2846
3167
 
@@ -2854,15 +3175,35 @@
2854
3175
  };
2855
3176
 
2856
3177
  this.identify = function (uid, traits, options) {
2857
- return _this.instance.identify(uid, traits, _this.buildOptions(options));
3178
+ return __awaiter(_this, void 0, void 0, function () {
3179
+ return __generator(this, function (_a) {
3180
+ switch (_a.label) {
3181
+ case 0:
3182
+ return [4
3183
+ /*yield*/
3184
+ , this.waitUntilInitialized()];
3185
+
3186
+ case 1:
3187
+ _a.sent();
3188
+
3189
+ return [2
3190
+ /*return*/
3191
+ , this.instance.identify(uid, traits, this.buildOptions(options))];
3192
+ }
3193
+ });
3194
+ });
2858
3195
  };
2859
3196
 
2860
3197
  this.reset = function () {
2861
- return _this.instance.plugins[PLUGIN_NAME].reset();
3198
+ _this.onIsInitialized(function () {
3199
+ _this.instance.plugins[PLUGIN_NAME].reset();
3200
+ });
2862
3201
  };
2863
3202
 
2864
3203
  this.debug = function (enabled) {
2865
- return _this.instance.plugins[PLUGIN_NAME].debug(enabled);
3204
+ _this.onIsInitialized(function () {
3205
+ _this.instance.plugins[PLUGIN_NAME].debug(enabled);
3206
+ });
2866
3207
  };
2867
3208
 
2868
3209
  this.onProfileChange = function (cb) {
@@ -2877,6 +3218,25 @@
2877
3218
  });
2878
3219
  };
2879
3220
 
3221
+ this.onIsInitialized = function (onIsInitialized) {
3222
+ if (typeof onIsInitialized === 'function') {
3223
+ if (_this.isInitialized) {
3224
+ onIsInitialized();
3225
+ } else {
3226
+ var detachOnReadyListener_1 = _this.instance.on('ready', function () {
3227
+ onIsInitialized();
3228
+ detachOnReadyListener_1();
3229
+ });
3230
+ }
3231
+ }
3232
+ };
3233
+
3234
+ this.waitUntilInitialized = function () {
3235
+ return new Promise(function (resolve) {
3236
+ _this.onIsInitialized(resolve);
3237
+ });
3238
+ };
3239
+
2880
3240
  this.plugins = flatten__default["default"](plugins || []);
2881
3241
 
2882
3242
  if (profile) {
@@ -2895,6 +3255,15 @@
2895
3255
  };
2896
3256
  }
2897
3257
 
3258
+ if (typeof onLog === 'function') {
3259
+ logger.addSink(new OnLogLogSink(onLog));
3260
+ }
3261
+
3262
+ if (typeof onError === 'function') {
3263
+ logger.addSink(new OnErrorLogSink(onError));
3264
+ }
3265
+
3266
+ this.logger = logger;
2898
3267
  this.instance = Analytics__default["default"]({
2899
3268
  app: 'ninetailed',
2900
3269
  plugins: __spreadArray(__spreadArray([], this.plugins, true), [ninetailedPlugin({
@@ -2906,6 +3275,11 @@
2906
3275
  requestTimeout: requestTimeout,
2907
3276
  preview: preview
2908
3277
  })], false)
3278
+ });
3279
+ var detachOnReadyListener = this.instance.on('ready', function () {
3280
+ _this.isInitialized = true;
3281
+ logger.info('Ninetailed Experience.js SDK is completely initialized.');
3282
+ detachOnReadyListener();
2909
3283
  }); // put in private method
2910
3284
 
2911
3285
  this.onProfileChange(function (profileState) {
@@ -3834,13 +4208,13 @@
3834
4208
  activeExperiments = _a.activeExperiments,
3835
4209
  profile = _a.profile;
3836
4210
  var trafficRandom = getTrafficRandom(profile, experience);
3837
- log("The traffic random factor for experience ".concat(experience.id, " is ").concat(trafficRandom, ". It's traffic allocation is set to ").concat(experience.trafficAllocation, "."));
4211
+ logger.info("The traffic random factor for experience ".concat(experience.id, " is ").concat(trafficRandom, ". It's traffic allocation is set to ").concat(experience.trafficAllocation, "."));
3838
4212
  var isInTrafficRange = experience.trafficAllocation > trafficRandom;
3839
4213
  var matchesAudience = !experience.audience || includes__default["default"](profile.audiences, experience.audience.id);
3840
4214
  var hasActiveExperiment = !!find__default["default"](activeExperiments, {
3841
4215
  id: experience.id
3842
4216
  });
3843
- log("Is the profile in traffic allocation range? ".concat(isInTrafficRange ? 'yes' : 'no', ".\n\n Does the profile match the audience of the experience? ").concat(matchesAudience ? 'yes' : 'no', ".\n\n Is there an active experiment for this profile? ").concat(hasActiveExperiment ? 'yes' : 'no', "."));
4217
+ logger.info("Is the profile in traffic allocation range? ".concat(isInTrafficRange ? 'yes' : 'no', ".\n\n Does the profile match the audience of the experience? ").concat(matchesAudience ? 'yes' : 'no', ".\n\n Is there an active experiment for this profile? ").concat(hasActiveExperiment ? 'yes' : 'no', "."));
3844
4218
  return isInTrafficRange && (matchesAudience || // if the expriment is active already then it's selectible without further contraints to be fullfilled
3845
4219
  hasActiveExperiment);
3846
4220
  };
@@ -3867,7 +4241,7 @@
3867
4241
  var experience = _a.experience,
3868
4242
  profile = _a.profile;
3869
4243
  var distributionRandom = getDistributionRandom(profile, experience);
3870
- log("The distribution random factor for experience ".concat(experience.id, " is ").concat(distributionRandom, ". It's distribution is set to ").concat(JSON.stringify(experience.distribution, null, 2), "."));
4244
+ logger.log("The distribution random factor for experience ".concat(experience.id, " is ").concat(distributionRandom, ". It's distribution is set to ").concat(JSON.stringify(experience.distribution, null, 2), "."));
3871
4245
  var distribution = find__default["default"](experience.distribution, function (_a) {
3872
4246
  var start = _a.start,
3873
4247
  end = _a.end;
@@ -4277,6 +4651,7 @@
4277
4651
  exports.PLUGIN_NAME = PLUGIN_NAME;
4278
4652
  exports.decodeExperienceVariantsMap = decodeExperienceVariantsMap;
4279
4653
  exports.isExperienceMatch = isExperienceMatch;
4654
+ exports.logger = logger;
4280
4655
  exports.ninetailedPlugin = ninetailedPlugin;
4281
4656
  exports.selectActiveExperiments = selectActiveExperiments;
4282
4657
  exports.selectDistribution = selectDistribution;