@croct/sdk 0.20.1 → 0.21.1

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/apiKey.cjs CHANGED
@@ -26,10 +26,10 @@ const _ApiKey = class _ApiKey {
26
26
  this.privateKey = privateKey;
27
27
  }
28
28
  static from(apiKey) {
29
- if (apiKey instanceof _ApiKey) {
30
- return apiKey;
29
+ if (typeof apiKey === "string") {
30
+ return _ApiKey.parse(apiKey);
31
31
  }
32
- return _ApiKey.parse(apiKey);
32
+ return apiKey;
33
33
  }
34
34
  static parse(apiKey) {
35
35
  const parts = apiKey.split(":");
package/apiKey.js CHANGED
@@ -4,10 +4,10 @@ const _ApiKey = class _ApiKey {
4
4
  this.privateKey = privateKey;
5
5
  }
6
6
  static from(apiKey) {
7
- if (apiKey instanceof _ApiKey) {
8
- return apiKey;
7
+ if (typeof apiKey === "string") {
8
+ return _ApiKey.parse(apiKey);
9
9
  }
10
- return _ApiKey.parse(apiKey);
10
+ return apiKey;
11
11
  }
12
12
  static parse(apiKey) {
13
13
  const parts = apiKey.split(":");
package/constants.cjs CHANGED
@@ -25,7 +25,7 @@ __export(constants_exports, {
25
25
  module.exports = __toCommonJS(constants_exports);
26
26
  const BASE_ENDPOINT_URL = "https://api.croct.io";
27
27
  const MAX_QUERY_LENGTH = parseInt("<@maxQueryLength@>", 10);
28
- const VERSION = "0.20.1";
28
+ const VERSION = "0.21.1";
29
29
  const CLIENT_LIBRARY = `Croct SDK JS v${VERSION}`;
30
30
  // Annotate the CommonJS export names for ESM import in node:
31
31
  0 && (module.exports = {
package/constants.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  declare const BASE_ENDPOINT_URL = "https://api.croct.io";
2
2
  declare const MAX_QUERY_LENGTH: number;
3
- declare const VERSION = "0.20.1";
4
- declare const CLIENT_LIBRARY = "Croct SDK JS v0.20.1";
3
+ declare const VERSION = "0.21.1";
4
+ declare const CLIENT_LIBRARY = "Croct SDK JS v0.21.1";
5
5
 
6
6
  export { BASE_ENDPOINT_URL, CLIENT_LIBRARY, MAX_QUERY_LENGTH, VERSION };
package/constants.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  declare const BASE_ENDPOINT_URL = "https://api.croct.io";
2
2
  declare const MAX_QUERY_LENGTH: number;
3
- declare const VERSION = "0.20.1";
4
- declare const CLIENT_LIBRARY = "Croct SDK JS v0.20.1";
3
+ declare const VERSION = "0.21.1";
4
+ declare const CLIENT_LIBRARY = "Croct SDK JS v0.21.1";
5
5
 
6
6
  export { BASE_ENDPOINT_URL, CLIENT_LIBRARY, MAX_QUERY_LENGTH, VERSION };
package/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const BASE_ENDPOINT_URL = "https://api.croct.io";
2
2
  const MAX_QUERY_LENGTH = parseInt("<@maxQueryLength@>", 10);
3
- const VERSION = "0.20.1";
3
+ const VERSION = "0.21.1";
4
4
  const CLIENT_LIBRARY = `Croct SDK JS v${VERSION}`;
5
5
  export {
6
6
  BASE_ENDPOINT_URL,
package/container.cjs CHANGED
@@ -98,8 +98,7 @@ const _Container = class _Container {
98
98
  inactivityRetryPolicy: new import_retry.ArbitraryPolicy([3e4, 3e4, 12e4, 12e4, 3e5, 3e5, 9e5]),
99
99
  logger: this.getLogger("Tracker"),
100
100
  channel: this.getBeaconChannel(),
101
- eventMetadata: this.configuration.eventMetadata,
102
- processor: this.configuration.eventProcessor === void 0 ? void 0 : this.configuration.eventProcessor(this)
101
+ eventMetadata: this.configuration.eventMetadata
103
102
  });
104
103
  const queue = this.getBeaconQueue();
105
104
  queue.addCallback("halfEmpty", tracker.unsuspend);
package/container.d.cts CHANGED
@@ -2,7 +2,7 @@ import { Logger } from './logging/logger.cjs';
2
2
  import { TokenScope, Context } from './context.cjs';
3
3
  import { MonitoredQueue } from './queue/monitoredQueue.cjs';
4
4
  import { TokenStore } from './token/token.cjs';
5
- import { Tracker, TrackingEventProcessor } from './tracker.cjs';
5
+ import { Tracker } from './tracker.cjs';
6
6
  import { Evaluator } from './evaluator.cjs';
7
7
  import { CidAssigner } from './cid/assigner.cjs';
8
8
  import { EventManager } from './eventManager.cjs';
@@ -22,7 +22,6 @@ import './utilityTypes.cjs';
22
22
  import './sourceLocation.cjs';
23
23
  import '@croct/content-model/definition';
24
24
 
25
- type DependencyResolver<T> = (container: Container) => T;
26
25
  type Configuration = {
27
26
  appId: string;
28
27
  tokenScope: TokenScope;
@@ -45,7 +44,6 @@ type Configuration = {
45
44
  eventMetadata?: {
46
45
  [key: string]: string;
47
46
  };
48
- eventProcessor?: DependencyResolver<TrackingEventProcessor>;
49
47
  defaultFetchTimeout?: number;
50
48
  defaultPreferredLocale?: string;
51
49
  };
@@ -93,4 +91,4 @@ declare class Container {
93
91
  dispose(): Promise<void>;
94
92
  }
95
93
 
96
- export { type Configuration, Container, type DependencyResolver };
94
+ export { type Configuration, Container };
package/container.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Logger } from './logging/logger.js';
2
2
  import { TokenScope, Context } from './context.js';
3
3
  import { MonitoredQueue } from './queue/monitoredQueue.js';
4
4
  import { TokenStore } from './token/token.js';
5
- import { Tracker, TrackingEventProcessor } from './tracker.js';
5
+ import { Tracker } from './tracker.js';
6
6
  import { Evaluator } from './evaluator.js';
7
7
  import { CidAssigner } from './cid/assigner.js';
8
8
  import { EventManager } from './eventManager.js';
@@ -22,7 +22,6 @@ import './utilityTypes.js';
22
22
  import './sourceLocation.js';
23
23
  import '@croct/content-model/definition';
24
24
 
25
- type DependencyResolver<T> = (container: Container) => T;
26
25
  type Configuration = {
27
26
  appId: string;
28
27
  tokenScope: TokenScope;
@@ -45,7 +44,6 @@ type Configuration = {
45
44
  eventMetadata?: {
46
45
  [key: string]: string;
47
46
  };
48
- eventProcessor?: DependencyResolver<TrackingEventProcessor>;
49
47
  defaultFetchTimeout?: number;
50
48
  defaultPreferredLocale?: string;
51
49
  };
@@ -93,4 +91,4 @@ declare class Container {
93
91
  dispose(): Promise<void>;
94
92
  }
95
93
 
96
- export { type Configuration, Container, type DependencyResolver };
94
+ export { type Configuration, Container };
package/container.js CHANGED
@@ -76,8 +76,7 @@ const _Container = class _Container {
76
76
  inactivityRetryPolicy: new ArbitraryPolicy([3e4, 3e4, 12e4, 12e4, 3e5, 3e5, 9e5]),
77
77
  logger: this.getLogger("Tracker"),
78
78
  channel: this.getBeaconChannel(),
79
- eventMetadata: this.configuration.eventMetadata,
80
- processor: this.configuration.eventProcessor === void 0 ? void 0 : this.configuration.eventProcessor(this)
79
+ eventMetadata: this.configuration.eventMetadata
81
80
  });
82
81
  const queue = this.getBeaconQueue();
83
82
  queue.addCallback("halfEmpty", tracker.unsuspend);
@@ -53,6 +53,7 @@ type With<T, K extends keyof T> = T & {
53
53
  type SlotMetadata = {
54
54
  version: string;
55
55
  schema?: ContentDefinitionBundle;
56
+ contentSource: 'slot' | 'experience' | 'experiment';
56
57
  experience?: {
57
58
  experienceId: string;
58
59
  audienceId: string;
@@ -53,6 +53,7 @@ type With<T, K extends keyof T> = T & {
53
53
  type SlotMetadata = {
54
54
  version: string;
55
55
  schema?: ContentDefinitionBundle;
56
+ contentSource: 'slot' | 'experience' | 'experiment';
56
57
  experience?: {
57
58
  experienceId: string;
58
59
  audienceId: string;
@@ -29,7 +29,6 @@ var import_schema = require('../schema/index.cjs');
29
29
  var import_sdk = require('../sdk.cjs');
30
30
  var import_sessionFacade = require('./sessionFacade.cjs');
31
31
  var import_contentFetcherFacade = require('./contentFetcherFacade.cjs');
32
- var import_eventSubjectProcessor = require('../eventSubjectProcessor.cjs');
33
32
  function validateConfiguration(configuration) {
34
33
  try {
35
34
  import_schema.sdkFacadeConfigurationSchema.validate(configuration);
@@ -47,37 +46,57 @@ class SdkFacade {
47
46
  if (userId !== void 0 && token !== void 0) {
48
47
  throw new Error("Either the user ID or token can be specified, but not both.");
49
48
  }
50
- const sdk = new SdkFacade(
51
- import_sdk.Sdk.init({
52
- ...containerConfiguration,
53
- tokenScope: containerConfiguration.tokenScope ?? "global",
54
- debug: containerConfiguration.debug ?? false,
55
- test: containerConfiguration.test ?? false,
56
- disableCidMirroring: containerConfiguration.disableCidMirroring ?? false,
57
- eventProcessor: (container) => new import_eventSubjectProcessor.EventSubjectProcessor(container.getLogger("EventSubjectProcessor"))
58
- })
59
- );
49
+ const sdk = import_sdk.Sdk.init({
50
+ ...containerConfiguration,
51
+ tokenScope: containerConfiguration.tokenScope ?? "global",
52
+ debug: containerConfiguration.debug ?? false,
53
+ test: containerConfiguration.test ?? false,
54
+ disableCidMirroring: containerConfiguration.disableCidMirroring ?? false
55
+ });
56
+ const facade = new SdkFacade(sdk);
57
+ sdk.eventManager.addListener("tokenChanged", ({ oldToken, newToken }) => {
58
+ const oldSubject = oldToken?.getSubject() ?? null;
59
+ const newSubject = newToken?.getSubject() ?? null;
60
+ if (newSubject === oldSubject) {
61
+ return;
62
+ }
63
+ if (oldToken !== null && oldSubject !== null) {
64
+ facade.trackInternalEvent(
65
+ {
66
+ type: "userSignedOut",
67
+ userId: oldSubject
68
+ },
69
+ oldToken
70
+ );
71
+ }
72
+ if (newSubject !== null) {
73
+ facade.trackInternalEvent({
74
+ type: "userSignedIn",
75
+ userId: newSubject
76
+ });
77
+ }
78
+ });
60
79
  if (userId !== void 0) {
61
- const currentToken = sdk.context.getToken();
80
+ const currentToken = facade.context.getToken();
62
81
  const currentSubject = currentToken?.getSubject() ?? null;
63
82
  if (currentSubject !== userId) {
64
83
  if (userId === null) {
65
- sdk.unsetToken();
84
+ facade.unsetToken();
66
85
  } else {
67
- sdk.identify(userId);
86
+ facade.identify(userId);
68
87
  }
69
88
  }
70
89
  } else if (token !== void 0) {
71
90
  if (token === null) {
72
- sdk.unsetToken();
91
+ facade.unsetToken();
73
92
  } else {
74
- sdk.setToken(import_token.Token.parse(token));
93
+ facade.setToken(import_token.Token.parse(token));
75
94
  }
76
95
  }
77
96
  if (track) {
78
- sdk.tracker.enable();
97
+ facade.tracker.enable();
79
98
  }
80
- return sdk;
99
+ return facade;
81
100
  }
82
101
  get context() {
83
102
  return this.sdk.context;
@@ -163,50 +182,17 @@ class SdkFacade {
163
182
  if (currentToken !== null && currentToken.toString() === token.toString()) {
164
183
  return;
165
184
  }
166
- const currentSubject = currentToken?.getSubject() ?? null;
167
- const subject = token.getSubject();
168
- const logger = this.getLogger();
169
- if (subject === currentSubject) {
170
- this.context.setToken(token);
171
- logger.debug("Token refreshed");
172
- return;
173
- }
174
- if (currentSubject !== null) {
175
- this.trackInternalEvent({
176
- type: "userSignedOut",
177
- userId: currentSubject
178
- });
179
- logger.info("User signed out");
180
- }
181
185
  this.context.setToken(token);
182
- if (subject !== null) {
183
- this.trackInternalEvent({
184
- type: "userSignedIn",
185
- userId: subject
186
- });
187
- logger.info(`User signed in as ${subject}`);
188
- }
189
- logger.debug("New token saved, ");
190
186
  }
191
187
  unsetToken() {
192
188
  const token = this.getToken();
193
189
  if (token === null) {
194
190
  return;
195
191
  }
196
- const logger = this.getLogger();
197
- const subject = token.getSubject();
198
- if (subject !== null) {
199
- this.trackInternalEvent({
200
- type: "userSignedOut",
201
- userId: subject
202
- });
203
- logger.info("User signed out");
204
- }
205
192
  this.context.setToken(null);
206
- logger.debug("Token removed");
207
193
  }
208
- trackInternalEvent(event) {
209
- this.sdk.tracker.track(event).catch(() => {
194
+ trackInternalEvent(event, token) {
195
+ this.sdk.tracker.track(event, { token }).catch(() => {
210
196
  });
211
197
  }
212
198
  getLogger(...namespace) {
@@ -7,7 +7,6 @@ import { sdkFacadeConfigurationSchema } from "../schema/index.js";
7
7
  import { Sdk } from "../sdk.js";
8
8
  import { SessionFacade } from "./sessionFacade.js";
9
9
  import { ContentFetcherFacade } from "./contentFetcherFacade.js";
10
- import { EventSubjectProcessor } from "../eventSubjectProcessor.js";
11
10
  function validateConfiguration(configuration) {
12
11
  try {
13
12
  sdkFacadeConfigurationSchema.validate(configuration);
@@ -25,37 +24,57 @@ class SdkFacade {
25
24
  if (userId !== void 0 && token !== void 0) {
26
25
  throw new Error("Either the user ID or token can be specified, but not both.");
27
26
  }
28
- const sdk = new SdkFacade(
29
- Sdk.init({
30
- ...containerConfiguration,
31
- tokenScope: containerConfiguration.tokenScope ?? "global",
32
- debug: containerConfiguration.debug ?? false,
33
- test: containerConfiguration.test ?? false,
34
- disableCidMirroring: containerConfiguration.disableCidMirroring ?? false,
35
- eventProcessor: (container) => new EventSubjectProcessor(container.getLogger("EventSubjectProcessor"))
36
- })
37
- );
27
+ const sdk = Sdk.init({
28
+ ...containerConfiguration,
29
+ tokenScope: containerConfiguration.tokenScope ?? "global",
30
+ debug: containerConfiguration.debug ?? false,
31
+ test: containerConfiguration.test ?? false,
32
+ disableCidMirroring: containerConfiguration.disableCidMirroring ?? false
33
+ });
34
+ const facade = new SdkFacade(sdk);
35
+ sdk.eventManager.addListener("tokenChanged", ({ oldToken, newToken }) => {
36
+ const oldSubject = oldToken?.getSubject() ?? null;
37
+ const newSubject = newToken?.getSubject() ?? null;
38
+ if (newSubject === oldSubject) {
39
+ return;
40
+ }
41
+ if (oldToken !== null && oldSubject !== null) {
42
+ facade.trackInternalEvent(
43
+ {
44
+ type: "userSignedOut",
45
+ userId: oldSubject
46
+ },
47
+ oldToken
48
+ );
49
+ }
50
+ if (newSubject !== null) {
51
+ facade.trackInternalEvent({
52
+ type: "userSignedIn",
53
+ userId: newSubject
54
+ });
55
+ }
56
+ });
38
57
  if (userId !== void 0) {
39
- const currentToken = sdk.context.getToken();
58
+ const currentToken = facade.context.getToken();
40
59
  const currentSubject = currentToken?.getSubject() ?? null;
41
60
  if (currentSubject !== userId) {
42
61
  if (userId === null) {
43
- sdk.unsetToken();
62
+ facade.unsetToken();
44
63
  } else {
45
- sdk.identify(userId);
64
+ facade.identify(userId);
46
65
  }
47
66
  }
48
67
  } else if (token !== void 0) {
49
68
  if (token === null) {
50
- sdk.unsetToken();
69
+ facade.unsetToken();
51
70
  } else {
52
- sdk.setToken(Token.parse(token));
71
+ facade.setToken(Token.parse(token));
53
72
  }
54
73
  }
55
74
  if (track) {
56
- sdk.tracker.enable();
75
+ facade.tracker.enable();
57
76
  }
58
- return sdk;
77
+ return facade;
59
78
  }
60
79
  get context() {
61
80
  return this.sdk.context;
@@ -141,50 +160,17 @@ class SdkFacade {
141
160
  if (currentToken !== null && currentToken.toString() === token.toString()) {
142
161
  return;
143
162
  }
144
- const currentSubject = currentToken?.getSubject() ?? null;
145
- const subject = token.getSubject();
146
- const logger = this.getLogger();
147
- if (subject === currentSubject) {
148
- this.context.setToken(token);
149
- logger.debug("Token refreshed");
150
- return;
151
- }
152
- if (currentSubject !== null) {
153
- this.trackInternalEvent({
154
- type: "userSignedOut",
155
- userId: currentSubject
156
- });
157
- logger.info("User signed out");
158
- }
159
163
  this.context.setToken(token);
160
- if (subject !== null) {
161
- this.trackInternalEvent({
162
- type: "userSignedIn",
163
- userId: subject
164
- });
165
- logger.info(`User signed in as ${subject}`);
166
- }
167
- logger.debug("New token saved, ");
168
164
  }
169
165
  unsetToken() {
170
166
  const token = this.getToken();
171
167
  if (token === null) {
172
168
  return;
173
169
  }
174
- const logger = this.getLogger();
175
- const subject = token.getSubject();
176
- if (subject !== null) {
177
- this.trackInternalEvent({
178
- type: "userSignedOut",
179
- userId: subject
180
- });
181
- logger.info("User signed out");
182
- }
183
170
  this.context.setToken(null);
184
- logger.debug("Token removed");
185
171
  }
186
- trackInternalEvent(event) {
187
- this.sdk.tracker.track(event).catch(() => {
172
+ trackInternalEvent(event, token) {
173
+ this.sdk.tracker.track(event, { token }).catch(() => {
188
174
  });
189
175
  }
190
176
  getLogger(...namespace) {
package/index.d.cts CHANGED
@@ -1,7 +1,5 @@
1
1
  export { VERSION } from './constants.cjs';
2
2
  export { Configuration, Sdk } from './sdk.cjs';
3
- import './container.cjs';
4
- import './logging/logger.cjs';
5
3
  import './context.cjs';
6
4
  import './token/token.cjs';
7
5
  import '@croct/json';
@@ -10,8 +8,7 @@ import './cache/cache.cjs';
10
8
  import './tab.cjs';
11
9
  import './eventManager.cjs';
12
10
  import './sdkEvents.cjs';
13
- import './queue/monitoredQueue.cjs';
14
- import './queue/queue.cjs';
11
+ import './logging/logger.cjs';
15
12
  import './tracker.cjs';
16
13
  import './channel/channel.cjs';
17
14
  import './retry/policy.cjs';
package/index.d.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  export { VERSION } from './constants.js';
2
2
  export { Configuration, Sdk } from './sdk.js';
3
- import './container.js';
4
- import './logging/logger.js';
5
3
  import './context.js';
6
4
  import './token/token.js';
7
5
  import '@croct/json';
@@ -10,8 +8,7 @@ import './cache/cache.js';
10
8
  import './tab.js';
11
9
  import './eventManager.js';
12
10
  import './sdkEvents.js';
13
- import './queue/monitoredQueue.js';
14
- import './queue/queue.js';
11
+ import './logging/logger.js';
15
12
  import './tracker.js';
16
13
  import './channel/channel.js';
17
14
  import './retry/policy.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@croct/sdk",
3
- "version": "0.20.1",
3
+ "version": "0.21.1",
4
4
  "description": "Croct SDK for JavaScript.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -58,6 +58,10 @@ const productDetails = new import_validation.ObjectType({
58
58
  originalPrice: new import_validation.NumberType({
59
59
  minimum: 0
60
60
  }),
61
+ currency: new import_validation.StringType({
62
+ minLength: 1,
63
+ maxLength: 10
64
+ }),
61
65
  url: new import_validation.StringType({
62
66
  format: "url"
63
67
  }),
@@ -32,6 +32,10 @@ const productDetails = new ObjectType({
32
32
  originalPrice: new NumberType({
33
33
  minimum: 0
34
34
  }),
35
+ currency: new StringType({
36
+ minLength: 1,
37
+ maxLength: 10
38
+ }),
35
39
  url: new StringType({
36
40
  format: "url"
37
41
  }),
@@ -91,7 +91,6 @@ const sdkConfigurationSchema = new import_validation.ObjectType({
91
91
  logger: import_loggerSchema.loggerSchema,
92
92
  urlSanitizer: new import_validation.FunctionType(),
93
93
  eventMetadata: eventMetadataSchema,
94
- eventProcessor: new import_validation.FunctionType(),
95
94
  defaultFetchTimeout: new import_validation.NumberType({
96
95
  integer: true,
97
96
  minimum: 1
@@ -67,7 +67,6 @@ const sdkConfigurationSchema = new ObjectType({
67
67
  logger: loggerSchema,
68
68
  urlSanitizer: new FunctionType(),
69
69
  eventMetadata: eventMetadataSchema,
70
- eventProcessor: new FunctionType(),
71
70
  defaultFetchTimeout: new NumberType({
72
71
  integer: true,
73
72
  minimum: 1
package/sdk.d.cts CHANGED
@@ -1,7 +1,6 @@
1
- import { DependencyResolver } from './container.cjs';
2
1
  import { TokenScope, Context } from './context.cjs';
3
2
  import { Logger } from './logging/logger.cjs';
4
- import { TrackingEventProcessor, Tracker } from './tracker.cjs';
3
+ import { Tracker } from './tracker.cjs';
5
4
  import { Evaluator } from './evaluator.cjs';
6
5
  import { SdkEventMap } from './sdkEvents.cjs';
7
6
  import { EventManager } from './eventManager.cjs';
@@ -10,8 +9,6 @@ import { UrlSanitizer } from './tab.cjs';
10
9
  import { ContentFetcher } from './contentFetcher.cjs';
11
10
  import { TokenStore } from './token/token.cjs';
12
11
  import { CookieCacheConfiguration } from './cache/cookieCache.cjs';
13
- import './queue/monitoredQueue.cjs';
14
- import './queue/queue.cjs';
15
12
  import './cache/cache.cjs';
16
13
  import './channel/channel.cjs';
17
14
  import './retry/policy.cjs';
@@ -43,7 +40,6 @@ type Configuration = {
43
40
  userToken?: CookieCacheConfiguration;
44
41
  previewToken?: CookieCacheConfiguration;
45
42
  };
46
- eventProcessor?: DependencyResolver<TrackingEventProcessor>;
47
43
  defaultFetchTimeout?: number;
48
44
  defaultPreferredLocale?: string;
49
45
  };
package/sdk.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { DependencyResolver } from './container.js';
2
1
  import { TokenScope, Context } from './context.js';
3
2
  import { Logger } from './logging/logger.js';
4
- import { TrackingEventProcessor, Tracker } from './tracker.js';
3
+ import { Tracker } from './tracker.js';
5
4
  import { Evaluator } from './evaluator.js';
6
5
  import { SdkEventMap } from './sdkEvents.js';
7
6
  import { EventManager } from './eventManager.js';
@@ -10,8 +9,6 @@ import { UrlSanitizer } from './tab.js';
10
9
  import { ContentFetcher } from './contentFetcher.js';
11
10
  import { TokenStore } from './token/token.js';
12
11
  import { CookieCacheConfiguration } from './cache/cookieCache.js';
13
- import './queue/monitoredQueue.js';
14
- import './queue/queue.js';
15
12
  import './cache/cache.js';
16
13
  import './channel/channel.js';
17
14
  import './retry/policy.js';
@@ -43,7 +40,6 @@ type Configuration = {
43
40
  userToken?: CookieCacheConfiguration;
44
41
  previewToken?: CookieCacheConfiguration;
45
42
  };
46
- eventProcessor?: DependencyResolver<TrackingEventProcessor>;
47
43
  defaultFetchTimeout?: number;
48
44
  defaultPreferredLocale?: string;
49
45
  };
package/tracker.cjs CHANGED
@@ -42,7 +42,6 @@ class Tracker {
42
42
  this.inactivityRetryPolicy = inactivityRetryPolicy;
43
43
  this.channel = channel;
44
44
  this.logger = logger ?? new import_logging.NullLogger();
45
- this.processor = config.processor;
46
45
  this.options = {
47
46
  ...options,
48
47
  eventMetadata: options.eventMetadata ?? {}
@@ -174,8 +173,9 @@ class Tracker {
174
173
  };
175
174
  startTimer();
176
175
  }
177
- track(event, timestamp = Date.now()) {
178
- return this.dispatch(this.enrichEvent(event, timestamp), timestamp).then(() => event);
176
+ track(event, options = {}) {
177
+ const { timestamp = Date.now(), token } = options;
178
+ return this.dispatch(this.enrichEvent(event, timestamp), timestamp, token).then(() => event);
179
179
  }
180
180
  trackPageOpen({ referrer, ...payload }) {
181
181
  this.enqueue({
@@ -225,34 +225,20 @@ class Tracker {
225
225
  notifyEvent(event) {
226
226
  this.listeners.map((listener) => listener(event));
227
227
  }
228
- dispatch(event, timestamp) {
229
- const userToken = this.tokenProvider.getToken();
228
+ dispatch(event, timestamp, token) {
229
+ const userToken = token ?? this.tokenProvider.getToken();
230
230
  const metadata = this.options.eventMetadata;
231
231
  const context = {
232
232
  tabId: this.tab.id,
233
233
  url: this.tab.url,
234
234
  ...Object.keys(metadata).length > 0 ? { metadata } : {}
235
235
  };
236
- const queuedEvent = {
236
+ return this.publish({
237
237
  ...userToken !== null ? { userToken } : {},
238
238
  event,
239
239
  timestamp,
240
240
  context
241
- };
242
- const processedEvents = this.processor !== void 0 ? this.processor.process(queuedEvent) : [queuedEvent];
243
- processedEvents.sort((left, right) => left.timestamp - right.timestamp);
244
- let result = null;
245
- for (const processedEvent of processedEvents) {
246
- if (processedEvent === queuedEvent) {
247
- result = this.publish(queuedEvent);
248
- continue;
249
- }
250
- this.publish(processedEvent);
251
- }
252
- if (result === null) {
253
- return Promise.reject(new Error("Event suppressed."));
254
- }
255
- return result;
241
+ });
256
242
  }
257
243
  publish(queuedEvent) {
258
244
  const { event } = queuedEvent;
package/tracker.d.cts CHANGED
@@ -15,17 +15,17 @@ type Options = {
15
15
  [key: string]: string;
16
16
  };
17
17
  };
18
+ type TrackOptions = {
19
+ timestamp?: number;
20
+ token?: Token;
21
+ };
18
22
  type Configuration = Options & {
19
23
  channel: OutputChannel<Beacon>;
20
24
  logger?: Logger;
21
25
  tab: Tab;
22
26
  tokenProvider: TokenProvider;
23
- processor?: TrackingEventProcessor;
24
27
  inactivityRetryPolicy: RetryPolicy<number>;
25
28
  };
26
- interface TrackingEventProcessor {
27
- process(event: QueuedEventInfo): QueuedEventInfo[];
28
- }
29
29
  type QueuedEventInfo<T extends TrackingEvent = TrackingEvent> = Omit<EventInfo<T>, 'status'>;
30
30
  type EventInfo<T extends TrackingEvent = TrackingEvent> = {
31
31
  context: TrackingEventContext;
@@ -44,7 +44,6 @@ declare class Tracker {
44
44
  private inactivityRetryPolicy;
45
45
  private readonly channel;
46
46
  private readonly logger;
47
- private readonly processor?;
48
47
  private readonly listeners;
49
48
  private readonly pending;
50
49
  private readonly state;
@@ -62,7 +61,7 @@ declare class Tracker {
62
61
  private initialize;
63
62
  private stopInactivityTimer;
64
63
  private startInactivityTimer;
65
- track<T extends PartialTrackingEvent>(event: T, timestamp?: number): Promise<T>;
64
+ track<T extends PartialTrackingEvent>(event: T, options?: TrackOptions): Promise<T>;
66
65
  private trackPageOpen;
67
66
  private trackPageLoad;
68
67
  private trackTabOpen;
@@ -79,4 +78,4 @@ declare class Tracker {
79
78
  private enrichBeaconPayload;
80
79
  }
81
80
 
82
- export { type Configuration, type EventInfo, type EventListener, type QueuedEventInfo, Tracker, type TrackingEventProcessor };
81
+ export { type Configuration, type EventInfo, type EventListener, type QueuedEventInfo, type TrackOptions, Tracker };
package/tracker.d.ts CHANGED
@@ -15,17 +15,17 @@ type Options = {
15
15
  [key: string]: string;
16
16
  };
17
17
  };
18
+ type TrackOptions = {
19
+ timestamp?: number;
20
+ token?: Token;
21
+ };
18
22
  type Configuration = Options & {
19
23
  channel: OutputChannel<Beacon>;
20
24
  logger?: Logger;
21
25
  tab: Tab;
22
26
  tokenProvider: TokenProvider;
23
- processor?: TrackingEventProcessor;
24
27
  inactivityRetryPolicy: RetryPolicy<number>;
25
28
  };
26
- interface TrackingEventProcessor {
27
- process(event: QueuedEventInfo): QueuedEventInfo[];
28
- }
29
29
  type QueuedEventInfo<T extends TrackingEvent = TrackingEvent> = Omit<EventInfo<T>, 'status'>;
30
30
  type EventInfo<T extends TrackingEvent = TrackingEvent> = {
31
31
  context: TrackingEventContext;
@@ -44,7 +44,6 @@ declare class Tracker {
44
44
  private inactivityRetryPolicy;
45
45
  private readonly channel;
46
46
  private readonly logger;
47
- private readonly processor?;
48
47
  private readonly listeners;
49
48
  private readonly pending;
50
49
  private readonly state;
@@ -62,7 +61,7 @@ declare class Tracker {
62
61
  private initialize;
63
62
  private stopInactivityTimer;
64
63
  private startInactivityTimer;
65
- track<T extends PartialTrackingEvent>(event: T, timestamp?: number): Promise<T>;
64
+ track<T extends PartialTrackingEvent>(event: T, options?: TrackOptions): Promise<T>;
66
65
  private trackPageOpen;
67
66
  private trackPageLoad;
68
67
  private trackTabOpen;
@@ -79,4 +78,4 @@ declare class Tracker {
79
78
  private enrichBeaconPayload;
80
79
  }
81
80
 
82
- export { type Configuration, type EventInfo, type EventListener, type QueuedEventInfo, Tracker, type TrackingEventProcessor };
81
+ export { type Configuration, type EventInfo, type EventListener, type QueuedEventInfo, type TrackOptions, Tracker };
package/tracker.js CHANGED
@@ -20,7 +20,6 @@ class Tracker {
20
20
  this.inactivityRetryPolicy = inactivityRetryPolicy;
21
21
  this.channel = channel;
22
22
  this.logger = logger ?? new NullLogger();
23
- this.processor = config.processor;
24
23
  this.options = {
25
24
  ...options,
26
25
  eventMetadata: options.eventMetadata ?? {}
@@ -152,8 +151,9 @@ class Tracker {
152
151
  };
153
152
  startTimer();
154
153
  }
155
- track(event, timestamp = Date.now()) {
156
- return this.dispatch(this.enrichEvent(event, timestamp), timestamp).then(() => event);
154
+ track(event, options = {}) {
155
+ const { timestamp = Date.now(), token } = options;
156
+ return this.dispatch(this.enrichEvent(event, timestamp), timestamp, token).then(() => event);
157
157
  }
158
158
  trackPageOpen({ referrer, ...payload }) {
159
159
  this.enqueue({
@@ -203,34 +203,20 @@ class Tracker {
203
203
  notifyEvent(event) {
204
204
  this.listeners.map((listener) => listener(event));
205
205
  }
206
- dispatch(event, timestamp) {
207
- const userToken = this.tokenProvider.getToken();
206
+ dispatch(event, timestamp, token) {
207
+ const userToken = token ?? this.tokenProvider.getToken();
208
208
  const metadata = this.options.eventMetadata;
209
209
  const context = {
210
210
  tabId: this.tab.id,
211
211
  url: this.tab.url,
212
212
  ...Object.keys(metadata).length > 0 ? { metadata } : {}
213
213
  };
214
- const queuedEvent = {
214
+ return this.publish({
215
215
  ...userToken !== null ? { userToken } : {},
216
216
  event,
217
217
  timestamp,
218
218
  context
219
- };
220
- const processedEvents = this.processor !== void 0 ? this.processor.process(queuedEvent) : [queuedEvent];
221
- processedEvents.sort((left, right) => left.timestamp - right.timestamp);
222
- let result = null;
223
- for (const processedEvent of processedEvents) {
224
- if (processedEvent === queuedEvent) {
225
- result = this.publish(queuedEvent);
226
- continue;
227
- }
228
- this.publish(processedEvent);
229
- }
230
- if (result === null) {
231
- return Promise.reject(new Error("Event suppressed."));
232
- }
233
- return result;
219
+ });
234
220
  }
235
221
  publish(queuedEvent) {
236
222
  const { event } = queuedEvent;
@@ -11,6 +11,7 @@ type ProductDetails = {
11
11
  variant?: string;
12
12
  displayPrice: number;
13
13
  originalPrice?: number;
14
+ currency?: string;
14
15
  url?: string;
15
16
  imageUrl?: string;
16
17
  };
@@ -11,6 +11,7 @@ type ProductDetails = {
11
11
  variant?: string;
12
12
  displayPrice: number;
13
13
  originalPrice?: number;
14
+ currency?: string;
14
15
  url?: string;
15
16
  imageUrl?: string;
16
17
  };
@@ -1,83 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
- var eventSubjectProcessor_exports = {};
19
- __export(eventSubjectProcessor_exports, {
20
- EventSubjectProcessor: () => EventSubjectProcessor
21
- });
22
- module.exports = __toCommonJS(eventSubjectProcessor_exports);
23
- class EventSubjectProcessor {
24
- constructor(logger) {
25
- this.logger = logger;
26
- }
27
- process(event) {
28
- const { currentToken } = this;
29
- this.currentToken = event.userToken ?? null;
30
- if (currentToken === void 0) {
31
- return [event];
32
- }
33
- if (EventSubjectProcessor.isIdentificationEvent(event.event)) {
34
- return [event];
35
- }
36
- const currentSubject = currentToken?.getSubject() ?? null;
37
- const newSubject = event.userToken?.getSubject() ?? null;
38
- if (newSubject === currentSubject) {
39
- return [event];
40
- }
41
- const events = [];
42
- if (currentToken !== null && currentSubject !== null) {
43
- this.logger.info("External user sign out automatically tracked");
44
- events.push({
45
- timestamp: event.timestamp,
46
- context: event.context,
47
- userToken: currentToken,
48
- event: {
49
- type: "userSignedOut",
50
- userId: currentSubject
51
- }
52
- });
53
- }
54
- if (newSubject !== null) {
55
- this.logger.info("External user sign in automatically tracked");
56
- events.push({
57
- timestamp: event.timestamp,
58
- context: event.context,
59
- userToken: event.userToken,
60
- event: {
61
- type: "userSignedIn",
62
- userId: newSubject
63
- }
64
- });
65
- }
66
- events.push(event);
67
- return events;
68
- }
69
- static isIdentificationEvent(event) {
70
- switch (event.type) {
71
- case "userSignedIn":
72
- case "userSignedUp":
73
- case "userSignedOut":
74
- return true;
75
- default:
76
- return false;
77
- }
78
- }
79
- }
80
- // Annotate the CommonJS export names for ESM import in node:
81
- 0 && (module.exports = {
82
- EventSubjectProcessor
83
- });
@@ -1,22 +0,0 @@
1
- import { TrackingEventProcessor, QueuedEventInfo } from './tracker.cjs';
2
- import { Logger } from './logging/logger.cjs';
3
- import './tab.cjs';
4
- import './eventManager.cjs';
5
- import './channel/channel.cjs';
6
- import './retry/policy.cjs';
7
- import './token/token.cjs';
8
- import '@croct/json';
9
- import './apiKey.cjs';
10
- import './trackingEvents.cjs';
11
- import './patch.cjs';
12
- import './utilityTypes.cjs';
13
-
14
- declare class EventSubjectProcessor implements TrackingEventProcessor {
15
- private currentToken?;
16
- private logger;
17
- constructor(logger: Logger);
18
- process(event: QueuedEventInfo): QueuedEventInfo[];
19
- private static isIdentificationEvent;
20
- }
21
-
22
- export { EventSubjectProcessor };
@@ -1,22 +0,0 @@
1
- import { TrackingEventProcessor, QueuedEventInfo } from './tracker.js';
2
- import { Logger } from './logging/logger.js';
3
- import './tab.js';
4
- import './eventManager.js';
5
- import './channel/channel.js';
6
- import './retry/policy.js';
7
- import './token/token.js';
8
- import '@croct/json';
9
- import './apiKey.js';
10
- import './trackingEvents.js';
11
- import './patch.js';
12
- import './utilityTypes.js';
13
-
14
- declare class EventSubjectProcessor implements TrackingEventProcessor {
15
- private currentToken?;
16
- private logger;
17
- constructor(logger: Logger);
18
- process(event: QueuedEventInfo): QueuedEventInfo[];
19
- private static isIdentificationEvent;
20
- }
21
-
22
- export { EventSubjectProcessor };
@@ -1,60 +0,0 @@
1
- class EventSubjectProcessor {
2
- constructor(logger) {
3
- this.logger = logger;
4
- }
5
- process(event) {
6
- const { currentToken } = this;
7
- this.currentToken = event.userToken ?? null;
8
- if (currentToken === void 0) {
9
- return [event];
10
- }
11
- if (EventSubjectProcessor.isIdentificationEvent(event.event)) {
12
- return [event];
13
- }
14
- const currentSubject = currentToken?.getSubject() ?? null;
15
- const newSubject = event.userToken?.getSubject() ?? null;
16
- if (newSubject === currentSubject) {
17
- return [event];
18
- }
19
- const events = [];
20
- if (currentToken !== null && currentSubject !== null) {
21
- this.logger.info("External user sign out automatically tracked");
22
- events.push({
23
- timestamp: event.timestamp,
24
- context: event.context,
25
- userToken: currentToken,
26
- event: {
27
- type: "userSignedOut",
28
- userId: currentSubject
29
- }
30
- });
31
- }
32
- if (newSubject !== null) {
33
- this.logger.info("External user sign in automatically tracked");
34
- events.push({
35
- timestamp: event.timestamp,
36
- context: event.context,
37
- userToken: event.userToken,
38
- event: {
39
- type: "userSignedIn",
40
- userId: newSubject
41
- }
42
- });
43
- }
44
- events.push(event);
45
- return events;
46
- }
47
- static isIdentificationEvent(event) {
48
- switch (event.type) {
49
- case "userSignedIn":
50
- case "userSignedUp":
51
- case "userSignedOut":
52
- return true;
53
- default:
54
- return false;
55
- }
56
- }
57
- }
58
- export {
59
- EventSubjectProcessor
60
- };