@ninetailed/experience.js 7.0.1-beta.0 → 7.1.0-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.cjs CHANGED
@@ -96,6 +96,7 @@ const LEGACY_ANONYMOUS_ID = '__anon_id';
96
96
  const ANONYMOUS_ID = '__nt_anonymous_id__';
97
97
  const DEBUG_FLAG = '__nt_debug__';
98
98
  const PROFILE_FALLBACK_CACHE = '__nt_profile__';
99
+ const EXPERIENCES_FALLBACK_CACHE = '__nt_experiences__';
99
100
  const PROFILE_CHANGE = 'profile-change';
100
101
  const PROFILE_RESET = 'profile-reset';
101
102
  const CONSENT = '__nt-consent__';
@@ -126,7 +127,10 @@ const ninetailedPlugin = ({
126
127
  }
127
128
  try {
128
129
  const anonymousId = _instance.storage.getItem(ANONYMOUS_ID);
129
- const profile = yield apiClient.upsertProfile({
130
+ const {
131
+ profile,
132
+ experiences
133
+ } = yield apiClient.upsertProfile({
130
134
  profileId: anonymousId,
131
135
  events
132
136
  }, {
@@ -135,10 +139,13 @@ const ninetailedPlugin = ({
135
139
  });
136
140
  _instance.storage.setItem(ANONYMOUS_ID, profile.id);
137
141
  _instance.storage.setItem(PROFILE_FALLBACK_CACHE, profile);
142
+ _instance.storage.setItem(EXPERIENCES_FALLBACK_CACHE, experiences);
138
143
  experience_jsShared.logger.debug('Profile from api: ', profile);
144
+ experience_jsShared.logger.debug('Experiences from api: ', experiences);
139
145
  _instance.dispatch({
140
146
  type: PROFILE_CHANGE,
141
- profile
147
+ profile,
148
+ experiences
142
149
  });
143
150
  yield delay(20);
144
151
  return {
@@ -147,27 +154,27 @@ const ninetailedPlugin = ({
147
154
  } catch (error) {
148
155
  experience_jsShared.logger.debug('An error occurred during flushing the events: ', error);
149
156
  const fallbackProfile = _instance.storage.getItem(PROFILE_FALLBACK_CACHE);
157
+ const fallbackExperiences = _instance.storage.getItem(EXPERIENCES_FALLBACK_CACHE) || [];
150
158
  if (fallbackProfile) {
151
159
  experience_jsShared.logger.debug('Found a fallback profile - will use this.');
152
160
  _instance.dispatch({
153
161
  type: PROFILE_CHANGE,
154
- profile: fallbackProfile
162
+ profile: fallbackProfile,
163
+ experiences: fallbackExperiences
155
164
  });
156
- return {
157
- success: false
158
- };
159
165
  } else {
160
166
  experience_jsShared.logger.debug('No fallback profile found - setting profile to null.');
161
167
  _instance.dispatch({
162
168
  type: PROFILE_CHANGE,
163
169
  // TODO is it a good idea to set the profile to null?
164
170
  profile: null,
171
+ experiences: fallbackExperiences,
165
172
  error
166
173
  });
167
- return {
168
- success: false
169
- };
170
174
  }
175
+ return {
176
+ success: false
177
+ };
171
178
  }
172
179
  });
173
180
  const enqueueEvent = event => __awaiter(void 0, void 0, void 0, function* () {
@@ -266,7 +273,7 @@ const ninetailedPlugin = ({
266
273
  abort,
267
274
  payload
268
275
  }) => {
269
- if (![ANONYMOUS_ID, DEBUG_FLAG, PROFILE_FALLBACK_CACHE, CONSENT].includes(payload.key)) {
276
+ if (![ANONYMOUS_ID, DEBUG_FLAG, PROFILE_FALLBACK_CACHE, EXPERIENCES_FALLBACK_CACHE, CONSENT].includes(payload.key)) {
270
277
  return abort();
271
278
  }
272
279
  return payload;
@@ -281,6 +288,7 @@ const ninetailedPlugin = ({
281
288
  });
282
289
  instance.storage.removeItem(ANONYMOUS_ID);
283
290
  instance.storage.removeItem(PROFILE_FALLBACK_CACHE);
291
+ instance.storage.removeItem(EXPERIENCES_FALLBACK_CACHE);
284
292
  experience_jsShared.logger.debug('Removed old profile data from localstorage.');
285
293
  yield ninetailed.track('nt_reset');
286
294
  experience_jsShared.logger.info('Profile reset successful.');
@@ -356,7 +364,6 @@ class Ninetailed {
356
364
  constructor(ninetailedApiClientInstanceOrOptions, {
357
365
  plugins,
358
366
  url,
359
- profile,
360
367
  locale,
361
368
  requestTimeout,
362
369
  onLog,
@@ -474,12 +481,14 @@ class Ninetailed {
474
481
  cb(Object.assign(Object.assign({}, this._profileState), {
475
482
  status: 'error',
476
483
  profile: payload.profile,
484
+ experiences: payload.experiences,
477
485
  error: payload.error
478
486
  }));
479
487
  } else {
480
488
  cb(Object.assign(Object.assign({}, this._profileState), {
481
489
  status: 'success',
482
490
  profile: payload.profile,
491
+ experiences: payload.experiences,
483
492
  error: null
484
493
  }));
485
494
  }
@@ -541,21 +550,13 @@ class Ninetailed {
541
550
  });
542
551
  }
543
552
  });
544
- if (profile) {
545
- this._profileState = {
546
- status: 'success',
547
- profile,
548
- error: null,
549
- from: 'hydrated'
550
- };
551
- } else {
552
- this._profileState = {
553
- status: 'loading',
554
- profile: null,
555
- error: null,
556
- from: 'api'
557
- };
558
- }
553
+ this._profileState = {
554
+ status: 'loading',
555
+ profile: null,
556
+ experiences: null,
557
+ error: null,
558
+ from: 'api'
559
+ };
559
560
  if (typeof onLog === 'function') {
560
561
  experience_jsShared.logger.addSink(new experience_jsShared.OnLogLogSink(onLog));
561
562
  }
@@ -565,7 +566,6 @@ class Ninetailed {
565
566
  this.logger = experience_jsShared.logger;
566
567
  this.eventQueue = ninetailedPlugin({
567
568
  apiClient: this.apiClient,
568
- profile,
569
569
  locale,
570
570
  requestTimeout,
571
571
  buildClientContext,
@@ -587,7 +587,8 @@ class Ninetailed {
587
587
  this._profileState = profileState;
588
588
  if (typeof window !== 'undefined') {
589
589
  window.ninetailed = Object.assign({}, window.ninetailed, {
590
- profile: this.profileState.profile
590
+ profile: this.profileState.profile,
591
+ experiences: this.profileState.experiences
591
592
  });
592
593
  }
593
594
  });
@@ -666,7 +667,7 @@ const selectVariant = (baseline, variants, {
666
667
  return (_a = profile === null || profile === void 0 ? void 0 : profile.audiences) === null || _a === void 0 ? void 0 : _a.includes((_b = variant.audience) === null || _b === void 0 ? void 0 : _b.id);
667
668
  });
668
669
  if (variant) {
669
- if ((options === null || options === void 0 ? void 0 : options.holdout) || -1 > (profile === null || profile === void 0 ? void 0 : profile.random)) {
670
+ if ((options === null || options === void 0 ? void 0 : options.holdout) || -1 > ((profile === null || profile === void 0 ? void 0 : profile.random) || 0)) {
670
671
  return {
671
672
  loading: false,
672
673
  variant: Object.assign(Object.assign({}, baseline), {
@@ -902,6 +903,7 @@ exports.ANONYMOUS_ID = ANONYMOUS_ID;
902
903
  exports.CONSENT = CONSENT;
903
904
  exports.DEBUG_FLAG = DEBUG_FLAG;
904
905
  exports.EMPTY_MERGE_ID = EMPTY_MERGE_ID;
906
+ exports.EXPERIENCES_FALLBACK_CACHE = EXPERIENCES_FALLBACK_CACHE;
905
907
  exports.ElementSeenPayloadSchema = ElementSeenPayloadSchema;
906
908
  exports.HAS_SEEN_COMPONENT = HAS_SEEN_COMPONENT;
907
909
  exports.HAS_SEEN_ELEMENT = HAS_SEEN_ELEMENT;
package/index.js CHANGED
@@ -89,6 +89,7 @@ const LEGACY_ANONYMOUS_ID = '__anon_id';
89
89
  const ANONYMOUS_ID = '__nt_anonymous_id__';
90
90
  const DEBUG_FLAG = '__nt_debug__';
91
91
  const PROFILE_FALLBACK_CACHE = '__nt_profile__';
92
+ const EXPERIENCES_FALLBACK_CACHE = '__nt_experiences__';
92
93
  const PROFILE_CHANGE = 'profile-change';
93
94
  const PROFILE_RESET = 'profile-reset';
94
95
  const CONSENT = '__nt-consent__';
@@ -119,7 +120,10 @@ const ninetailedPlugin = ({
119
120
  }
120
121
  try {
121
122
  const anonymousId = _instance.storage.getItem(ANONYMOUS_ID);
122
- const profile = yield apiClient.upsertProfile({
123
+ const {
124
+ profile,
125
+ experiences
126
+ } = yield apiClient.upsertProfile({
123
127
  profileId: anonymousId,
124
128
  events
125
129
  }, {
@@ -128,10 +132,13 @@ const ninetailedPlugin = ({
128
132
  });
129
133
  _instance.storage.setItem(ANONYMOUS_ID, profile.id);
130
134
  _instance.storage.setItem(PROFILE_FALLBACK_CACHE, profile);
135
+ _instance.storage.setItem(EXPERIENCES_FALLBACK_CACHE, experiences);
131
136
  logger.debug('Profile from api: ', profile);
137
+ logger.debug('Experiences from api: ', experiences);
132
138
  _instance.dispatch({
133
139
  type: PROFILE_CHANGE,
134
- profile
140
+ profile,
141
+ experiences
135
142
  });
136
143
  yield delay(20);
137
144
  return {
@@ -140,27 +147,27 @@ const ninetailedPlugin = ({
140
147
  } catch (error) {
141
148
  logger.debug('An error occurred during flushing the events: ', error);
142
149
  const fallbackProfile = _instance.storage.getItem(PROFILE_FALLBACK_CACHE);
150
+ const fallbackExperiences = _instance.storage.getItem(EXPERIENCES_FALLBACK_CACHE) || [];
143
151
  if (fallbackProfile) {
144
152
  logger.debug('Found a fallback profile - will use this.');
145
153
  _instance.dispatch({
146
154
  type: PROFILE_CHANGE,
147
- profile: fallbackProfile
155
+ profile: fallbackProfile,
156
+ experiences: fallbackExperiences
148
157
  });
149
- return {
150
- success: false
151
- };
152
158
  } else {
153
159
  logger.debug('No fallback profile found - setting profile to null.');
154
160
  _instance.dispatch({
155
161
  type: PROFILE_CHANGE,
156
162
  // TODO is it a good idea to set the profile to null?
157
163
  profile: null,
164
+ experiences: fallbackExperiences,
158
165
  error
159
166
  });
160
- return {
161
- success: false
162
- };
163
167
  }
168
+ return {
169
+ success: false
170
+ };
164
171
  }
165
172
  });
166
173
  const enqueueEvent = event => __awaiter(void 0, void 0, void 0, function* () {
@@ -259,7 +266,7 @@ const ninetailedPlugin = ({
259
266
  abort,
260
267
  payload
261
268
  }) => {
262
- if (![ANONYMOUS_ID, DEBUG_FLAG, PROFILE_FALLBACK_CACHE, CONSENT].includes(payload.key)) {
269
+ if (![ANONYMOUS_ID, DEBUG_FLAG, PROFILE_FALLBACK_CACHE, EXPERIENCES_FALLBACK_CACHE, CONSENT].includes(payload.key)) {
263
270
  return abort();
264
271
  }
265
272
  return payload;
@@ -274,6 +281,7 @@ const ninetailedPlugin = ({
274
281
  });
275
282
  instance.storage.removeItem(ANONYMOUS_ID);
276
283
  instance.storage.removeItem(PROFILE_FALLBACK_CACHE);
284
+ instance.storage.removeItem(EXPERIENCES_FALLBACK_CACHE);
277
285
  logger.debug('Removed old profile data from localstorage.');
278
286
  yield ninetailed.track('nt_reset');
279
287
  logger.info('Profile reset successful.');
@@ -349,7 +357,6 @@ class Ninetailed {
349
357
  constructor(ninetailedApiClientInstanceOrOptions, {
350
358
  plugins,
351
359
  url,
352
- profile,
353
360
  locale,
354
361
  requestTimeout,
355
362
  onLog,
@@ -467,12 +474,14 @@ class Ninetailed {
467
474
  cb(Object.assign(Object.assign({}, this._profileState), {
468
475
  status: 'error',
469
476
  profile: payload.profile,
477
+ experiences: payload.experiences,
470
478
  error: payload.error
471
479
  }));
472
480
  } else {
473
481
  cb(Object.assign(Object.assign({}, this._profileState), {
474
482
  status: 'success',
475
483
  profile: payload.profile,
484
+ experiences: payload.experiences,
476
485
  error: null
477
486
  }));
478
487
  }
@@ -534,21 +543,13 @@ class Ninetailed {
534
543
  });
535
544
  }
536
545
  });
537
- if (profile) {
538
- this._profileState = {
539
- status: 'success',
540
- profile,
541
- error: null,
542
- from: 'hydrated'
543
- };
544
- } else {
545
- this._profileState = {
546
- status: 'loading',
547
- profile: null,
548
- error: null,
549
- from: 'api'
550
- };
551
- }
546
+ this._profileState = {
547
+ status: 'loading',
548
+ profile: null,
549
+ experiences: null,
550
+ error: null,
551
+ from: 'api'
552
+ };
552
553
  if (typeof onLog === 'function') {
553
554
  logger.addSink(new OnLogLogSink(onLog));
554
555
  }
@@ -558,7 +559,6 @@ class Ninetailed {
558
559
  this.logger = logger;
559
560
  this.eventQueue = ninetailedPlugin({
560
561
  apiClient: this.apiClient,
561
- profile,
562
562
  locale,
563
563
  requestTimeout,
564
564
  buildClientContext,
@@ -580,7 +580,8 @@ class Ninetailed {
580
580
  this._profileState = profileState;
581
581
  if (typeof window !== 'undefined') {
582
582
  window.ninetailed = Object.assign({}, window.ninetailed, {
583
- profile: this.profileState.profile
583
+ profile: this.profileState.profile,
584
+ experiences: this.profileState.experiences
584
585
  });
585
586
  }
586
587
  });
@@ -659,7 +660,7 @@ const selectVariant = (baseline, variants, {
659
660
  return (_a = profile === null || profile === void 0 ? void 0 : profile.audiences) === null || _a === void 0 ? void 0 : _a.includes((_b = variant.audience) === null || _b === void 0 ? void 0 : _b.id);
660
661
  });
661
662
  if (variant) {
662
- if ((options === null || options === void 0 ? void 0 : options.holdout) || -1 > (profile === null || profile === void 0 ? void 0 : profile.random)) {
663
+ if ((options === null || options === void 0 ? void 0 : options.holdout) || -1 > ((profile === null || profile === void 0 ? void 0 : profile.random) || 0)) {
663
664
  return {
664
665
  loading: false,
665
666
  variant: Object.assign(Object.assign({}, baseline), {
@@ -855,4 +856,4 @@ const makeExperienceSelectMiddleware = ({
855
856
  };
856
857
  };
857
858
 
858
- export { ANONYMOUS_ID, CONSENT, DEBUG_FLAG, EMPTY_MERGE_ID, ElementSeenPayloadSchema, HAS_SEEN_COMPONENT, HAS_SEEN_ELEMENT, LEGACY_ANONYMOUS_ID, Ninetailed, NinetailedPlugin, OnChangeEmitter, PAGE_HIDDEN, PLUGIN_NAME, PROFILE_CHANGE, PROFILE_FALLBACK_CACHE, PROFILE_RESET, SET_ENABLED_FEATURES, TrackComponentProperties, buildClientNinetailedRequestContext, decodeExperienceVariantsMap, makeExperienceSelectMiddleware, ninetailedPlugin, selectPluginsHavingExperienceSelectionMiddleware, selectPluginsHavingOnChangeEmitter, selectVariant };
859
+ export { ANONYMOUS_ID, CONSENT, DEBUG_FLAG, EMPTY_MERGE_ID, EXPERIENCES_FALLBACK_CACHE, ElementSeenPayloadSchema, HAS_SEEN_COMPONENT, HAS_SEEN_ELEMENT, LEGACY_ANONYMOUS_ID, Ninetailed, NinetailedPlugin, OnChangeEmitter, PAGE_HIDDEN, PLUGIN_NAME, PROFILE_CHANGE, PROFILE_FALLBACK_CACHE, PROFILE_RESET, SET_ENABLED_FEATURES, TrackComponentProperties, buildClientNinetailedRequestContext, decodeExperienceVariantsMap, makeExperienceSelectMiddleware, ninetailedPlugin, selectPluginsHavingExperienceSelectionMiddleware, selectPluginsHavingOnChangeEmitter, selectVariant };
@@ -1,5 +1,5 @@
1
1
  /// <reference types="analytics" />
2
- import { Locale, Traits, Profile, OnLogHandler, OnErrorHandler, Logger, PageviewProperties, Properties, NinetailedApiClient, NinetailedApiClientOptions, NinetailedRequestContext } from '@ninetailed/experience.js-shared';
2
+ import { Locale, Traits, OnLogHandler, OnErrorHandler, Logger, PageviewProperties, Properties, NinetailedApiClient, NinetailedApiClientOptions, NinetailedRequestContext } from '@ninetailed/experience.js-shared';
3
3
  import { EventFunctionOptions, NinetailedInstance, NinetailedPlugin, OnIsInitializedCallback, OnProfileChangeCallback, ProfileState, TrackHasSeenComponent, ElementSeenPayload, TrackComponentView } from './types';
4
4
  import { ObserveOptions } from './ElementSeenObserver';
5
5
  declare global {
@@ -24,7 +24,6 @@ type Options = {
24
24
  url?: string;
25
25
  locale?: Locale;
26
26
  plugins?: (NinetailedPlugin | NinetailedPlugin[])[];
27
- profile?: Profile;
28
27
  requestTimeout?: number;
29
28
  onLog?: OnLogHandler;
30
29
  onError?: OnErrorHandler;
@@ -46,7 +45,7 @@ export declare class Ninetailed implements NinetailedInstance {
46
45
  readonly plugins: NinetailedPlugin[];
47
46
  readonly logger: Logger;
48
47
  private readonly componentViewTrackingThreshold;
49
- constructor(ninetailedApiClientInstanceOrOptions: NinetailedApiClientInstanceOrOptions, { plugins, url, profile, locale, requestTimeout, onLog, onError, buildClientContext, componentViewTrackingThreshold, storageImpl, }?: Options);
48
+ constructor(ninetailedApiClientInstanceOrOptions: NinetailedApiClientInstanceOrOptions, { plugins, url, locale, requestTimeout, onLog, onError, buildClientContext, componentViewTrackingThreshold, storageImpl, }?: Options);
50
49
  page: (data?: Partial<PageviewProperties>, options?: EventFunctionOptions) => Promise<import("./types").FlushResult>;
51
50
  track: (event: string, properties?: Properties, options?: EventFunctionOptions) => Promise<import("./types").FlushResult>;
52
51
  /**
@@ -2,6 +2,7 @@ export declare const LEGACY_ANONYMOUS_ID = "__anon_id";
2
2
  export declare const ANONYMOUS_ID = "__nt_anonymous_id__";
3
3
  export declare const DEBUG_FLAG = "__nt_debug__";
4
4
  export declare const PROFILE_FALLBACK_CACHE = "__nt_profile__";
5
+ export declare const EXPERIENCES_FALLBACK_CACHE = "__nt_experiences__";
5
6
  export declare const PROFILE_CHANGE = "profile-change";
6
7
  export declare const PROFILE_RESET = "profile-reset";
7
8
  export declare const CONSENT = "__nt-consent__";
@@ -33,5 +33,5 @@ type Fail<T> = {
33
33
  type Result<T> = Loading<T> | Success<T> | Fail<T>;
34
34
  export declare const selectVariant: <T extends {
35
35
  id: string;
36
- }>(baseline: T, variants: Variant<T>[], { status, profile, error }: ProfileState, options?: Options) => Result<T>;
36
+ }>(baseline: T, variants: Variant<T>[], { status, profile, error }: Omit<ProfileState, 'experiences'>, options?: Options) => Result<T>;
37
37
  export {};
@@ -1,4 +1,4 @@
1
- import { Logger, PageviewProperties, Profile, Properties, Traits } from '@ninetailed/experience.js-shared';
1
+ import { Logger, PageviewProperties, Profile, Properties, Traits, SelectedVariantInfo } from '@ninetailed/experience.js-shared';
2
2
  import { DetachListeners } from 'analytics';
3
3
  import { TrackComponentProperties } from './TrackingProperties';
4
4
  import { NinetailedPlugin } from './NinetailedPlugin';
@@ -7,16 +7,19 @@ import { ElementSeenPayload } from './ElementSeenPayload';
7
7
  type Loading = {
8
8
  status: 'loading';
9
9
  profile: null;
10
+ experiences: null;
10
11
  error: null;
11
12
  };
12
13
  type Success = {
13
14
  status: 'success';
14
15
  profile: Profile;
16
+ experiences: SelectedVariantInfo[];
15
17
  error: null;
16
18
  };
17
19
  type Fail = {
18
20
  status: 'error';
19
21
  profile: null;
22
+ experiences: null;
20
23
  error: Error;
21
24
  };
22
25
  export type ProfileState = {
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js",
3
- "version": "7.0.1-beta.0",
3
+ "version": "7.1.0-beta.0",
4
4
  "module": "./index.js",
5
5
  "main": "./index.cjs",
6
6
  "type": "module",
7
7
  "types": "./index.d.ts",
8
8
  "dependencies": {
9
- "@ninetailed/experience.js-shared": "7.0.1-beta.0",
9
+ "@ninetailed/experience.js-shared": "7.1.0-beta.0",
10
10
  "analytics": "0.8.1",
11
11
  "zod": "3.21.4"
12
12
  },