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

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'
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
 
@@ -2450,12 +2464,17 @@ const ninetailedPlugin = ({
2450
2464
  },
2451
2465
  methods: {
2452
2466
  reset: (...args) => __awaiter(void 0, void 0, void 0, function* () {
2467
+ logger.debug('Resetting profile.');
2453
2468
  const instance = args[args.length - 1];
2454
- yield instance.storage.removeItem(ANONYMOUS_ID);
2455
2469
  instance.dispatch({
2456
- type: NINETAILED_TRACKER_EVENTS.profile,
2457
- profile: emptyProfile
2470
+ type: NINETAILED_TRACKER_EVENTS.reset
2458
2471
  });
2472
+ yield instance.storage.removeItem(ANONYMOUS_ID);
2473
+ yield instance.storage.removeItem(PROFILE_FALLBACK_CACHE);
2474
+ logger.debug('Removed old profile data from localstorage.'); // sending a new page event, when no anonymousId is set will create a new profile.
2475
+
2476
+ yield instance.page();
2477
+ logger.info('Profile reset successful.');
2459
2478
  yield delay(10);
2460
2479
  }),
2461
2480
  debug: (...args) => __awaiter(void 0, void 0, void 0, function* () {
@@ -3009,11 +3028,19 @@ class Ninetailed {
3009
3028
  return this.instance.on(NINETAILED_TRACKER_EVENTS.profile, ({
3010
3029
  payload
3011
3030
  }) => {
3012
- cb(Object.assign(Object.assign({}, this._profileState), {
3013
- status: 'success',
3014
- profile: payload.profile,
3015
- error: null
3016
- }));
3031
+ if (payload.error) {
3032
+ cb(Object.assign(Object.assign({}, this._profileState), {
3033
+ status: 'error',
3034
+ profile: payload.profile,
3035
+ error: payload.error
3036
+ }));
3037
+ } else {
3038
+ cb(Object.assign(Object.assign({}, this._profileState), {
3039
+ status: 'success',
3040
+ profile: payload.profile,
3041
+ error: null
3042
+ }));
3043
+ }
3017
3044
  });
3018
3045
  };
3019
3046
 
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'
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];
@@ -2627,7 +2642,11 @@
2627
2642
  return __generator(this, function (_a) {
2628
2643
  switch (_a.label) {
2629
2644
  case 0:
2645
+ experience_jsShared.logger.debug('Resetting profile.');
2630
2646
  instance = args[args.length - 1];
2647
+ instance.dispatch({
2648
+ type: NINETAILED_TRACKER_EVENTS.reset
2649
+ });
2631
2650
  return [4
2632
2651
  /*yield*/
2633
2652
  , instance.storage.removeItem(ANONYMOUS_ID)];
@@ -2635,17 +2654,31 @@
2635
2654
  case 1:
2636
2655
  _a.sent();
2637
2656
 
2638
- instance.dispatch({
2639
- type: NINETAILED_TRACKER_EVENTS.profile,
2640
- profile: emptyProfile
2641
- });
2642
2657
  return [4
2643
2658
  /*yield*/
2644
- , delay(10)];
2659
+ , instance.storage.removeItem(PROFILE_FALLBACK_CACHE)];
2645
2660
 
2646
2661
  case 2:
2647
2662
  _a.sent();
2648
2663
 
2664
+ 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.
2665
+
2666
+ return [4
2667
+ /*yield*/
2668
+ , instance.page()];
2669
+
2670
+ case 3:
2671
+ // sending a new page event, when no anonymousId is set will create a new profile.
2672
+ _a.sent();
2673
+
2674
+ experience_jsShared.logger.info('Profile reset successful.');
2675
+ return [4
2676
+ /*yield*/
2677
+ , delay(10)];
2678
+
2679
+ case 4:
2680
+ _a.sent();
2681
+
2649
2682
  return [2
2650
2683
  /*return*/
2651
2684
  ];
@@ -2835,11 +2868,20 @@
2835
2868
  cb(_this.profileState);
2836
2869
  return _this.instance.on(NINETAILED_TRACKER_EVENTS.profile, function (_a) {
2837
2870
  var payload = _a.payload;
2838
- cb(__assign(__assign({}, _this._profileState), {
2839
- status: 'success',
2840
- profile: payload.profile,
2841
- error: null
2842
- }));
2871
+
2872
+ if (payload.error) {
2873
+ cb(__assign(__assign({}, _this._profileState), {
2874
+ status: 'error',
2875
+ profile: payload.profile,
2876
+ error: payload.error
2877
+ }));
2878
+ } else {
2879
+ cb(__assign(__assign({}, _this._profileState), {
2880
+ status: 'success',
2881
+ profile: payload.profile,
2882
+ error: null
2883
+ }));
2884
+ }
2843
2885
  });
2844
2886
  };
2845
2887
 
@@ -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.13",
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.13",
9
9
  "ts-toolbelt": "^9.6.0",
10
10
  "diary": "^0.3.1",
11
11
  "zod": "^3.18.0",