@ninetailed/experience.js 2.0.2 → 2.1.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
  });
@@ -2655,7 +2889,7 @@
2655
2889
  return __generator(this, function (_b) {
2656
2890
  switch (_b.label) {
2657
2891
  case 0:
2658
- log('Sending Page event.');
2892
+ logger.info('Sending Page event.');
2659
2893
  ctx = buildClientNinetailedRequestContext();
2660
2894
  localOnly = payload.localOnly;
2661
2895
  return [4
@@ -2687,7 +2921,7 @@
2687
2921
  return __generator(this, function (_b) {
2688
2922
  switch (_b.label) {
2689
2923
  case 0:
2690
- log('Sending Track event.');
2924
+ logger.info('Sending Track event.');
2691
2925
  ctx = buildClientNinetailedRequestContext();
2692
2926
  localOnly = payload.localOnly;
2693
2927
  return [4
@@ -2720,7 +2954,7 @@
2720
2954
  return __generator(this, function (_b) {
2721
2955
  switch (_b.label) {
2722
2956
  case 0:
2723
- log('Sending Identify event.');
2957
+ logger.info('Sending Identify event.');
2724
2958
  localOnly = payload.localOnly;
2725
2959
  ctx = buildClientNinetailedRequestContext();
2726
2960
  return [4
@@ -2797,14 +3031,58 @@
2797
3031
  });
2798
3032
  });
2799
3033
  },
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.');
3034
+ debug: function () {
3035
+ var args = [];
3036
+
3037
+ for (var _i = 0; _i < arguments.length; _i++) {
3038
+ args[_i] = arguments[_i];
2807
3039
  }
3040
+
3041
+ return __awaiter(void 0, void 0, void 0, function () {
3042
+ var enabled, instance, consoleLogSink;
3043
+ return __generator(this, function (_a) {
3044
+ switch (_a.label) {
3045
+ case 0:
3046
+ enabled = args[0];
3047
+ instance = args[args.length - 1];
3048
+ consoleLogSink = new ConsoleLogSink();
3049
+ if (!enabled) return [3
3050
+ /*break*/
3051
+ , 2];
3052
+ return [4
3053
+ /*yield*/
3054
+ , instance.storage.setItem(DEBUG_FLAG_KEY, true)];
3055
+
3056
+ case 1:
3057
+ _a.sent(); // cleanup other console log sinks if there are already some.
3058
+
3059
+
3060
+ logger.removeSink(consoleLogSink.name);
3061
+ logger.addSink(consoleLogSink);
3062
+ logger.info('Debug mode enabled.');
3063
+ return [3
3064
+ /*break*/
3065
+ , 4];
3066
+
3067
+ case 2:
3068
+ return [4
3069
+ /*yield*/
3070
+ , instance.storage.removeItem(DEBUG_FLAG_KEY)];
3071
+
3072
+ case 3:
3073
+ _a.sent();
3074
+
3075
+ logger.info('Debug mode disabled.');
3076
+ logger.removeSink(consoleLogSink.name);
3077
+ _a.label = 4;
3078
+
3079
+ case 4:
3080
+ return [2
3081
+ /*return*/
3082
+ ];
3083
+ }
3084
+ });
3085
+ });
2808
3086
  }
2809
3087
  }
2810
3088
  };
@@ -2825,7 +3103,9 @@
2825
3103
  url = _c.url,
2826
3104
  profile = _c.profile,
2827
3105
  locale = _c.locale,
2828
- requestTimeout = _c.requestTimeout;
3106
+ requestTimeout = _c.requestTimeout,
3107
+ onLog = _c.onLog,
3108
+ onError = _c.onError;
2829
3109
 
2830
3110
  this.page = function (data, options) {
2831
3111
  return _this.instance.page(data, _this.buildOptions(options));
@@ -2895,6 +3175,15 @@
2895
3175
  };
2896
3176
  }
2897
3177
 
3178
+ if (typeof onLog === 'function') {
3179
+ logger.addSink(new OnLogLogSink(onLog));
3180
+ }
3181
+
3182
+ if (typeof onError === 'function') {
3183
+ logger.addSink(new OnErrorLogSink(onError));
3184
+ }
3185
+
3186
+ this.logger = logger;
2898
3187
  this.instance = Analytics__default["default"]({
2899
3188
  app: 'ninetailed',
2900
3189
  plugins: __spreadArray(__spreadArray([], this.plugins, true), [ninetailedPlugin({
@@ -3834,13 +4123,13 @@
3834
4123
  activeExperiments = _a.activeExperiments,
3835
4124
  profile = _a.profile;
3836
4125
  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, "."));
4126
+ 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
4127
  var isInTrafficRange = experience.trafficAllocation > trafficRandom;
3839
4128
  var matchesAudience = !experience.audience || includes__default["default"](profile.audiences, experience.audience.id);
3840
4129
  var hasActiveExperiment = !!find__default["default"](activeExperiments, {
3841
4130
  id: experience.id
3842
4131
  });
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', "."));
4132
+ 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
4133
  return isInTrafficRange && (matchesAudience || // if the expriment is active already then it's selectible without further contraints to be fullfilled
3845
4134
  hasActiveExperiment);
3846
4135
  };
@@ -3867,7 +4156,7 @@
3867
4156
  var experience = _a.experience,
3868
4157
  profile = _a.profile;
3869
4158
  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), "."));
4159
+ 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
4160
  var distribution = find__default["default"](experience.distribution, function (_a) {
3872
4161
  var start = _a.start,
3873
4162
  end = _a.end;
@@ -4277,6 +4566,7 @@
4277
4566
  exports.PLUGIN_NAME = PLUGIN_NAME;
4278
4567
  exports.decodeExperienceVariantsMap = decodeExperienceVariantsMap;
4279
4568
  exports.isExperienceMatch = isExperienceMatch;
4569
+ exports.logger = logger;
4280
4570
  exports.ninetailedPlugin = ninetailedPlugin;
4281
4571
  exports.selectActiveExperiments = selectActiveExperiments;
4282
4572
  exports.selectDistribution = selectDistribution;
@@ -2,6 +2,8 @@ import { PageData, AnalyticsPlugin, DetachListeners } from 'analytics';
2
2
  import { Locale, Traits, Profile, Variant } from '@ninetailed/experience.js-shared';
3
3
  import { ProfileState } from './types';
4
4
  import { ExperienceConfiguration } from './experience';
5
+ import { Logger, OnLogHandler, OnErrorHandler } from './logger';
6
+ export type { OnErrorHandler, OnLogHandler } from './logger';
5
7
  declare global {
6
8
  interface Window {
7
9
  ninetailed?: {
@@ -17,6 +19,8 @@ declare type Options = {
17
19
  plugins?: (AnalyticsPlugin | AnalyticsPlugin[])[];
18
20
  profile?: Profile;
19
21
  requestTimeout?: number;
22
+ onLog?: OnLogHandler;
23
+ onError?: OnErrorHandler;
20
24
  };
21
25
  declare type EventFunctionOptions = {
22
26
  plugins?: {
@@ -57,16 +61,18 @@ export interface NinetailedInstance {
57
61
  profileState: ProfileState;
58
62
  onProfileChange: OnProfileChange;
59
63
  plugins: AnalyticsPlugin[];
64
+ logger: Logger;
60
65
  }
61
66
  export declare class Ninetailed implements NinetailedInstance {
62
67
  private readonly instance;
63
68
  private _profileState;
64
69
  readonly plugins: AnalyticsPlugin[];
65
- constructor({ clientId, environment, preview }: {
70
+ readonly logger: Logger;
71
+ constructor({ clientId, environment, preview, }: {
66
72
  clientId: string;
67
73
  environment?: string;
68
74
  preview?: boolean;
69
- }, { plugins, url, profile, locale, requestTimeout }?: Options);
75
+ }, { plugins, url, profile, locale, requestTimeout, onLog, onError, }?: Options);
70
76
  page: (data?: PageData, options?: EventFunctionOptions) => Promise<any>;
71
77
  track: (event: string, payload?: unknown, options?: EventFunctionOptions) => Promise<any>;
72
78
  trackHasSeenComponent: TrackHasSeenComponent;
@@ -79,4 +85,3 @@ export declare class Ninetailed implements NinetailedInstance {
79
85
  private buildOptions;
80
86
  private registerWindowHandlers;
81
87
  }
82
- export {};
@@ -1,2 +1 @@
1
1
  export * from './apiClient';
2
- export * from './debug';
@@ -0,0 +1,10 @@
1
+ import { LogSink } from './LogSink';
2
+ export declare class ConsoleLogSink implements LogSink {
3
+ name: string;
4
+ debug(message: string, ...args: unknown[]): void;
5
+ info(message: string, ...args: unknown[]): void;
6
+ log(message: string, ...args: unknown[]): void;
7
+ warn(message: string, ...args: unknown[]): void;
8
+ error(message: string | Error, ...args: unknown[]): void;
9
+ fatal(message: string | Error, ...args: unknown[]): void;
10
+ }
@@ -0,0 +1,10 @@
1
+ import { LogFn, LogFnAsError } from 'diary';
2
+ export interface LogSink {
3
+ name: string;
4
+ debug?: LogFn;
5
+ info?: LogFn;
6
+ log?: LogFn;
7
+ warn?: LogFn;
8
+ error?: LogFnAsError;
9
+ fatal?: LogFnAsError;
10
+ }
@@ -0,0 +1,17 @@
1
+ import { LogSink } from './LogSink';
2
+ export declare class Logger {
3
+ readonly name = "@ninetailed/experience.js";
4
+ private readonly diary;
5
+ private sinks;
6
+ constructor();
7
+ addSink(sink: LogSink): void;
8
+ removeSink(name: string): void;
9
+ debug(message: string, ...args: unknown[]): void;
10
+ info(message: string, ...args: unknown[]): void;
11
+ log(message: string, ...args: unknown[]): void;
12
+ warn(message: string, ...args: unknown[]): void;
13
+ error(message: string | Error, ...args: unknown[]): void;
14
+ fatal(message: string | Error, ...args: unknown[]): void;
15
+ private onLogEvent;
16
+ }
17
+ export declare const logger: Logger;
@@ -0,0 +1,10 @@
1
+ import { LogFnAsError } from 'diary';
2
+ import { LogSink } from './LogSink';
3
+ export declare type OnErrorHandler = LogFnAsError;
4
+ export declare class OnErrorLogSink implements LogSink {
5
+ private readonly onError;
6
+ name: string;
7
+ constructor(onError: OnErrorHandler);
8
+ error(message: string | Error, ...args: unknown[]): void;
9
+ fatal(message: string | Error, ...args: unknown[]): void;
10
+ }
@@ -0,0 +1,12 @@
1
+ import { LogFnAsError } from 'diary';
2
+ import { LogSink } from './LogSink';
3
+ export declare type OnLogHandler = LogFnAsError;
4
+ export declare class OnLogLogSink implements LogSink {
5
+ private readonly onLog;
6
+ name: string;
7
+ constructor(onLog: OnLogHandler);
8
+ debug(message: string, ...args: unknown[]): void;
9
+ info(message: string, ...args: unknown[]): void;
10
+ log(message: string, ...args: unknown[]): void;
11
+ warn(message: string, ...args: unknown[]): void;
12
+ }
@@ -0,0 +1,7 @@
1
+ export { Logger, logger } from './Logger';
2
+ export { ConsoleLogSink } from './ConsoleLogSink';
3
+ export { OnLogLogSink } from './OnLogLogSink';
4
+ export { OnErrorLogSink } from './OnErrorLogSink';
5
+ export type { LogSink } from './LogSink';
6
+ export type { OnLogHandler } from './OnLogLogSink';
7
+ export type { OnErrorHandler } from './OnErrorLogSink';