@ninetailed/experience.js 3.0.0-beta.11 → 3.0.0-beta.14

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.esm.js CHANGED
@@ -2303,19 +2303,17 @@ const NINETAILED_TRACKER_EVENTS = {
2303
2303
  /**
2304
2304
  * `profile` - Fires when the profile is returned by the cdp API.
2305
2305
  */
2306
- profile: 'profile'
2306
+ profile: 'profile',
2307
+
2308
+ /**
2309
+ * `reset` - gets fired when the profile gets reset, so other plugins can react.
2310
+ */
2311
+ reset: 'reset-profile'
2307
2312
  };
2308
2313
  const PLUGIN_NAME = 'ninetailed';
2309
2314
 
2310
2315
  const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
2311
2316
 
2312
- const emptyProfile = {
2313
- id: '',
2314
- random: 0,
2315
- audiences: [],
2316
- traits: {},
2317
- location: {}
2318
- };
2319
2317
  const ninetailedPlugin = ({
2320
2318
  clientId,
2321
2319
  environment,
@@ -2342,7 +2340,12 @@ const ninetailedPlugin = ({
2342
2340
 
2343
2341
  try {
2344
2342
  const anonymousId = instance.storage.getItem(ANONYMOUS_ID);
2345
- const profile = yield apiClient.upsertProfile(events, anonymousId);
2343
+ const profile = yield apiClient.upsertProfile({
2344
+ profileId: anonymousId,
2345
+ events
2346
+ }, {
2347
+ locale
2348
+ });
2346
2349
  instance.storage.setItem(ANONYMOUS_ID, profile.id);
2347
2350
  instance.storage.setItem(PROFILE_FALLBACK_CACHE, profile);
2348
2351
  logger.debug('Profile from api: ', profile);
@@ -2352,11 +2355,22 @@ const ninetailedPlugin = ({
2352
2355
  });
2353
2356
  } catch (error) {
2354
2357
  logger.debug('An error occurred during flushing the events: ', error);
2355
- const fallbackProfile = instance.storage.getItem(PROFILE_FALLBACK_CACHE) || emptyProfile;
2356
- instance.dispatch({
2357
- type: NINETAILED_TRACKER_EVENTS.profile,
2358
- profile: fallbackProfile
2359
- });
2358
+ const fallbackProfile = instance.storage.getItem(PROFILE_FALLBACK_CACHE);
2359
+
2360
+ if (fallbackProfile) {
2361
+ logger.debug('Found a fallback profile - will use this.');
2362
+ instance.dispatch({
2363
+ type: NINETAILED_TRACKER_EVENTS.profile,
2364
+ profile: fallbackProfile
2365
+ });
2366
+ } else {
2367
+ logger.debug('No fallback profile found - setting profile to null.');
2368
+ instance.dispatch({
2369
+ type: NINETAILED_TRACKER_EVENTS.profile,
2370
+ profile: null,
2371
+ error
2372
+ });
2373
+ }
2360
2374
  } // This is necessary to make sure that the cache is updated before the next flush is performed
2361
2375
 
2362
2376
 
@@ -2370,6 +2384,17 @@ const ninetailedPlugin = ({
2370
2384
  throttledFlush(instance);
2371
2385
  });
2372
2386
 
2387
+ const abortNonClientEvents = ({
2388
+ abort,
2389
+ payload
2390
+ }) => {
2391
+ if (typeof window !== 'object') {
2392
+ return abort();
2393
+ }
2394
+
2395
+ return payload;
2396
+ };
2397
+
2373
2398
  return {
2374
2399
  name: 'ninetailed',
2375
2400
  config: {},
@@ -2396,6 +2421,9 @@ const ninetailedPlugin = ({
2396
2421
 
2397
2422
  logger.debug('Ninetailed Core plugin initialized.');
2398
2423
  },
2424
+ pageStart: params => {
2425
+ return abortNonClientEvents(params);
2426
+ },
2399
2427
  page: ({
2400
2428
  payload,
2401
2429
  instance
@@ -2409,6 +2437,9 @@ const ninetailedPlugin = ({
2409
2437
  ctx
2410
2438
  }), instance);
2411
2439
  }),
2440
+ trackStart: params => {
2441
+ return abortNonClientEvents(params);
2442
+ },
2412
2443
  track: ({
2413
2444
  payload,
2414
2445
  instance
@@ -2423,6 +2454,9 @@ const ninetailedPlugin = ({
2423
2454
  ctx
2424
2455
  }), instance);
2425
2456
  }),
2457
+ identifyStart: params => {
2458
+ return abortNonClientEvents(params);
2459
+ },
2426
2460
  identify: ({
2427
2461
  payload,
2428
2462
  instance
@@ -2450,12 +2484,18 @@ const ninetailedPlugin = ({
2450
2484
  },
2451
2485
  methods: {
2452
2486
  reset: (...args) => __awaiter(void 0, void 0, void 0, function* () {
2487
+ // TODO it seems reset gets called during Next.js SSR builds. Need to verify that.
2488
+ logger.debug('Resetting profile.');
2453
2489
  const instance = args[args.length - 1];
2454
- yield instance.storage.removeItem(ANONYMOUS_ID);
2455
2490
  instance.dispatch({
2456
- type: NINETAILED_TRACKER_EVENTS.profile,
2457
- profile: emptyProfile
2491
+ type: NINETAILED_TRACKER_EVENTS.reset
2458
2492
  });
2493
+ yield instance.storage.removeItem(ANONYMOUS_ID);
2494
+ yield instance.storage.removeItem(PROFILE_FALLBACK_CACHE);
2495
+ logger.debug('Removed old profile data from localstorage.'); // sending a new page event, when no anonymousId is set will create a new profile.
2496
+
2497
+ yield instance.page();
2498
+ logger.info('Profile reset successful.');
2459
2499
  yield delay(10);
2460
2500
  }),
2461
2501
  debug: (...args) => __awaiter(void 0, void 0, void 0, function* () {
@@ -3009,11 +3049,19 @@ class Ninetailed {
3009
3049
  return this.instance.on(NINETAILED_TRACKER_EVENTS.profile, ({
3010
3050
  payload
3011
3051
  }) => {
3012
- cb(Object.assign(Object.assign({}, this._profileState), {
3013
- status: 'success',
3014
- profile: payload.profile,
3015
- error: null
3016
- }));
3052
+ if (payload.error) {
3053
+ cb(Object.assign(Object.assign({}, this._profileState), {
3054
+ status: 'error',
3055
+ profile: payload.profile,
3056
+ error: payload.error
3057
+ }));
3058
+ } else {
3059
+ cb(Object.assign(Object.assign({}, this._profileState), {
3060
+ status: 'success',
3061
+ profile: payload.profile,
3062
+ error: null
3063
+ }));
3064
+ }
3017
3065
  });
3018
3066
  };
3019
3067
 
package/index.umd.js CHANGED
@@ -2374,7 +2374,12 @@
2374
2374
  /**
2375
2375
  * `profile` - Fires when the profile is returned by the cdp API.
2376
2376
  */
2377
- profile: 'profile'
2377
+ profile: 'profile',
2378
+
2379
+ /**
2380
+ * `reset` - gets fired when the profile gets reset, so other plugins can react.
2381
+ */
2382
+ reset: 'reset-profile'
2378
2383
  };
2379
2384
  var PLUGIN_NAME = 'ninetailed';
2380
2385
 
@@ -2384,20 +2389,13 @@
2384
2389
  });
2385
2390
  };
2386
2391
 
2387
- var emptyProfile = {
2388
- id: '',
2389
- random: 0,
2390
- audiences: [],
2391
- traits: {},
2392
- location: {}
2393
- };
2394
2392
  var ninetailedPlugin = function ninetailedPlugin(_a) {
2395
2393
  var clientId = _a.clientId,
2396
2394
  environment = _a.environment;
2397
2395
  _a.preview;
2398
2396
  var url = _a.url,
2399
- profile = _a.profile;
2400
- _a.locale;
2397
+ profile = _a.profile,
2398
+ locale = _a.locale;
2401
2399
  var apiClient = new experience_jsShared.NinetailedApiClient({
2402
2400
  clientId: clientId,
2403
2401
  environment: environment,
@@ -2429,7 +2427,12 @@
2429
2427
  anonymousId = instance.storage.getItem(ANONYMOUS_ID);
2430
2428
  return [4
2431
2429
  /*yield*/
2432
- , apiClient.upsertProfile(events, anonymousId)];
2430
+ , apiClient.upsertProfile({
2431
+ profileId: anonymousId,
2432
+ events: events
2433
+ }, {
2434
+ locale: locale
2435
+ })];
2433
2436
 
2434
2437
  case 2:
2435
2438
  profile_1 = _a.sent();
@@ -2447,11 +2450,23 @@
2447
2450
  case 3:
2448
2451
  error_1 = _a.sent();
2449
2452
  experience_jsShared.logger.debug('An error occurred during flushing the events: ', error_1);
2450
- fallbackProfile = instance.storage.getItem(PROFILE_FALLBACK_CACHE) || emptyProfile;
2451
- instance.dispatch({
2452
- type: NINETAILED_TRACKER_EVENTS.profile,
2453
- profile: fallbackProfile
2454
- });
2453
+ fallbackProfile = instance.storage.getItem(PROFILE_FALLBACK_CACHE);
2454
+
2455
+ if (fallbackProfile) {
2456
+ experience_jsShared.logger.debug('Found a fallback profile - will use this.');
2457
+ instance.dispatch({
2458
+ type: NINETAILED_TRACKER_EVENTS.profile,
2459
+ profile: fallbackProfile
2460
+ });
2461
+ } else {
2462
+ experience_jsShared.logger.debug('No fallback profile found - setting profile to null.');
2463
+ instance.dispatch({
2464
+ type: NINETAILED_TRACKER_EVENTS.profile,
2465
+ profile: null,
2466
+ error: error_1
2467
+ });
2468
+ }
2469
+
2455
2470
  return [3
2456
2471
  /*break*/
2457
2472
  , 4];
@@ -2488,6 +2503,17 @@
2488
2503
  });
2489
2504
  };
2490
2505
 
2506
+ var abortNonClientEvents = function abortNonClientEvents(_a) {
2507
+ var abort = _a.abort,
2508
+ payload = _a.payload;
2509
+
2510
+ if (typeof window !== 'object') {
2511
+ return abort();
2512
+ }
2513
+
2514
+ return payload;
2515
+ };
2516
+
2491
2517
  return {
2492
2518
  name: 'ninetailed',
2493
2519
  config: {},
@@ -2514,6 +2540,9 @@
2514
2540
 
2515
2541
  experience_jsShared.logger.debug('Ninetailed Core plugin initialized.');
2516
2542
  },
2543
+ pageStart: function (params) {
2544
+ return abortNonClientEvents(params);
2545
+ },
2517
2546
  page: function (_a) {
2518
2547
  var payload = _a.payload,
2519
2548
  instance = _a.instance;
@@ -2543,6 +2572,9 @@
2543
2572
  });
2544
2573
  });
2545
2574
  },
2575
+ trackStart: function (params) {
2576
+ return abortNonClientEvents(params);
2577
+ },
2546
2578
  track: function (_a) {
2547
2579
  var payload = _a.payload,
2548
2580
  instance = _a.instance;
@@ -2573,6 +2605,9 @@
2573
2605
  });
2574
2606
  });
2575
2607
  },
2608
+ identifyStart: function (params) {
2609
+ return abortNonClientEvents(params);
2610
+ },
2576
2611
  identify: function (_a) {
2577
2612
  var payload = _a.payload,
2578
2613
  instance = _a.instance;
@@ -2627,7 +2662,12 @@
2627
2662
  return __generator(this, function (_a) {
2628
2663
  switch (_a.label) {
2629
2664
  case 0:
2665
+ // TODO it seems reset gets called during Next.js SSR builds. Need to verify that.
2666
+ experience_jsShared.logger.debug('Resetting profile.');
2630
2667
  instance = args[args.length - 1];
2668
+ instance.dispatch({
2669
+ type: NINETAILED_TRACKER_EVENTS.reset
2670
+ });
2631
2671
  return [4
2632
2672
  /*yield*/
2633
2673
  , instance.storage.removeItem(ANONYMOUS_ID)];
@@ -2635,17 +2675,31 @@
2635
2675
  case 1:
2636
2676
  _a.sent();
2637
2677
 
2638
- instance.dispatch({
2639
- type: NINETAILED_TRACKER_EVENTS.profile,
2640
- profile: emptyProfile
2641
- });
2642
2678
  return [4
2643
2679
  /*yield*/
2644
- , delay(10)];
2680
+ , instance.storage.removeItem(PROFILE_FALLBACK_CACHE)];
2645
2681
 
2646
2682
  case 2:
2647
2683
  _a.sent();
2648
2684
 
2685
+ experience_jsShared.logger.debug('Removed old profile data from localstorage.'); // sending a new page event, when no anonymousId is set will create a new profile.
2686
+
2687
+ return [4
2688
+ /*yield*/
2689
+ , instance.page()];
2690
+
2691
+ case 3:
2692
+ // sending a new page event, when no anonymousId is set will create a new profile.
2693
+ _a.sent();
2694
+
2695
+ experience_jsShared.logger.info('Profile reset successful.');
2696
+ return [4
2697
+ /*yield*/
2698
+ , delay(10)];
2699
+
2700
+ case 4:
2701
+ _a.sent();
2702
+
2649
2703
  return [2
2650
2704
  /*return*/
2651
2705
  ];
@@ -2835,11 +2889,20 @@
2835
2889
  cb(_this.profileState);
2836
2890
  return _this.instance.on(NINETAILED_TRACKER_EVENTS.profile, function (_a) {
2837
2891
  var payload = _a.payload;
2838
- cb(__assign(__assign({}, _this._profileState), {
2839
- status: 'success',
2840
- profile: payload.profile,
2841
- error: null
2842
- }));
2892
+
2893
+ if (payload.error) {
2894
+ cb(__assign(__assign({}, _this._profileState), {
2895
+ status: 'error',
2896
+ profile: payload.profile,
2897
+ error: payload.error
2898
+ }));
2899
+ } else {
2900
+ cb(__assign(__assign({}, _this._profileState), {
2901
+ status: 'success',
2902
+ profile: payload.profile,
2903
+ error: null
2904
+ }));
2905
+ }
2843
2906
  });
2844
2907
  };
2845
2908
 
@@ -14,6 +14,10 @@ export declare const NINETAILED_TRACKER_EVENTS: {
14
14
  * `profile` - Fires when the profile is returned by the cdp API.
15
15
  */
16
16
  profile: string;
17
+ /**
18
+ * `reset` - gets fired when the profile gets reset, so other plugins can react.
19
+ */
20
+ reset: string;
17
21
  };
18
22
  export declare const PLUGIN_NAME = "ninetailed";
19
23
  export declare const ninetailedPlugin: ({ clientId, environment, preview, url, profile, locale, }: AnalyticsPluginNinetailedConfig) => AnalyticsPlugin;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js",
3
- "version": "3.0.0-beta.11",
3
+ "version": "3.0.0-beta.14",
4
4
  "main": "./index.umd.js",
5
5
  "module": "./index.esm.js",
6
6
  "typings": "./index.d.ts",
7
7
  "dependencies": {
8
- "@ninetailed/experience.js-shared": "3.0.0-beta.11",
8
+ "@ninetailed/experience.js-shared": "3.0.0-beta.14",
9
9
  "ts-toolbelt": "^9.6.0",
10
10
  "diary": "^0.3.1",
11
11
  "zod": "^3.18.0",