@croct/sdk 0.9.1 → 0.11.0-alpha
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/activeRecord.js +32 -36
- package/cache/fallbackCache.js +15 -32
- package/cache/inMemoryCache.js +9 -10
- package/cache/index.js +1 -1
- package/cache/localStorageCache.js +24 -25
- package/channel/beaconSocketChannel.d.ts +1 -1
- package/channel/beaconSocketChannel.js +49 -79
- package/channel/channel.d.ts +1 -1
- package/channel/encodedChannel.js +8 -10
- package/channel/guaranteedChannel.d.ts +4 -4
- package/channel/guaranteedChannel.js +41 -43
- package/channel/index.js +1 -1
- package/channel/queuedChannel.js +35 -64
- package/channel/retryChannel.d.ts +1 -1
- package/channel/retryChannel.js +44 -77
- package/channel/sandboxChannel.js +17 -18
- package/channel/socketChannel.d.ts +4 -4
- package/channel/socketChannel.js +77 -79
- package/cid/cachedAssigner.js +15 -27
- package/cid/fixedAssigner.js +5 -6
- package/cid/index.js +1 -1
- package/cid/remoteAssigner.js +23 -36
- package/constants.d.ts +3 -2
- package/constants.js +6 -5
- package/container.d.ts +13 -6
- package/container.js +152 -168
- package/contentFetcher.d.ts +59 -0
- package/contentFetcher.js +129 -0
- package/context.d.ts +3 -3
- package/context.js +36 -38
- package/error.js +2 -2
- package/evaluator.d.ts +33 -24
- package/evaluator.js +126 -117
- package/eventManager.d.ts +1 -1
- package/eventManager.js +14 -15
- package/facade/contentFetcherFacade.d.ts +27 -0
- package/facade/contentFetcherFacade.js +40 -0
- package/facade/evaluatorFacade.d.ts +13 -3
- package/facade/evaluatorFacade.js +57 -72
- package/facade/sdkFacade.d.ts +10 -3
- package/facade/sdkFacade.js +129 -141
- package/facade/sessionFacade.js +6 -7
- package/facade/sessionPatch.js +9 -13
- package/facade/trackerFacade.js +32 -38
- package/facade/userFacade.js +10 -11
- package/facade/userPatch.js +9 -13
- package/index.js +2 -2
- package/logging/consoleLogger.js +18 -35
- package/logging/index.js +1 -1
- package/logging/namespacedLogger.js +14 -15
- package/logging/nullLogger.js +10 -13
- package/namespacedStorage.js +30 -47
- package/package.json +13 -16
- package/patch.d.ts +1 -1
- package/queue/capacityRestrictedQueue.js +17 -18
- package/queue/inMemoryQueue.js +22 -28
- package/queue/index.js +1 -1
- package/queue/monitoredQueue.d.ts +2 -2
- package/queue/monitoredQueue.js +39 -40
- package/queue/persistentQueue.js +33 -38
- package/retry/arbitraryPolicy.js +8 -10
- package/retry/backoffPolicy.d.ts +1 -1
- package/retry/backoffPolicy.js +11 -13
- package/retry/index.js +1 -1
- package/retry/maxAttemptsPolicy.js +7 -8
- package/retry/neverPolicy.js +6 -9
- package/schema/attributeSchema.js +1 -1
- package/schema/contentFetcherSchemas.d.ts +2 -0
- package/schema/contentFetcherSchemas.js +22 -0
- package/schema/contentSchemas.js +1 -1
- package/schema/contextSchemas.js +1 -1
- package/schema/ecommerceSchemas.js +1 -1
- package/schema/evaluatorSchemas.d.ts +2 -0
- package/schema/{evaluationSchemas.js → evaluatorSchemas.js} +3 -3
- package/schema/eventSchemas.js +8 -9
- package/schema/index.d.ts +2 -1
- package/schema/index.js +3 -2
- package/schema/loggerSchema.js +1 -1
- package/schema/operationSchemas.js +8 -8
- package/schema/sdkFacadeSchemas.js +9 -6
- package/schema/sdkSchemas.js +8 -5
- package/schema/tokenSchema.js +5 -4
- package/schema/userSchema.js +2 -2
- package/sdk.d.ts +9 -3
- package/sdk.js +81 -127
- package/sdkEvents.d.ts +3 -3
- package/sourceLocation.d.ts +3 -3
- package/sourceLocation.js +13 -14
- package/tab.d.ts +5 -5
- package/tab.js +50 -80
- package/token/cachedTokenStore.js +9 -10
- package/token/inMemoryTokenStore.js +7 -8
- package/token/index.js +1 -1
- package/token/replicatedTokenStore.js +7 -8
- package/token/token.d.ts +9 -5
- package/token/token.js +63 -57
- package/tracker.d.ts +4 -4
- package/tracker.js +145 -122
- package/trackingEvents.d.ts +36 -36
- package/trackingEvents.js +12 -6
- package/transformer.js +1 -1
- package/utilityTypes.d.ts +2 -2
- package/uuid.js +9 -7
- package/validation/arrayType.d.ts +2 -2
- package/validation/arrayType.js +29 -27
- package/validation/booleanType.js +11 -15
- package/validation/functionType.js +11 -15
- package/validation/index.js +1 -1
- package/validation/jsonType.d.ts +2 -2
- package/validation/jsonType.js +61 -80
- package/validation/mixedSchema.js +4 -7
- package/validation/nullType.js +11 -15
- package/validation/numberType.d.ts +1 -1
- package/validation/numberType.js +23 -22
- package/validation/objectType.d.ts +1 -1
- package/validation/objectType.js +61 -73
- package/validation/schema.js +6 -10
- package/validation/stringType.d.ts +1 -1
- package/validation/stringType.js +36 -47
- package/validation/unionType.js +27 -77
- package/validation/violation.js +1 -2
- package/schema/evaluationSchemas.d.ts +0 -2
package/tracker.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Tracker = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var _b;
|
|
12
|
-
var tab = _a.tab, tokenProvider = _a.tokenProvider, channel = _a.channel, logger = _a.logger, inactivityRetryPolicy = _a.inactivityRetryPolicy, options = tslib_1.__rest(_a, ["tab", "tokenProvider", "channel", "logger", "inactivityRetryPolicy"]);
|
|
4
|
+
const logging_1 = require("./logging");
|
|
5
|
+
const error_1 = require("./error");
|
|
6
|
+
const trackingEvents_1 = require("./trackingEvents");
|
|
7
|
+
const trackedEvents = {};
|
|
8
|
+
class Tracker {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
var _a;
|
|
13
11
|
this.listeners = [];
|
|
14
12
|
this.pending = [];
|
|
15
13
|
this.state = {
|
|
@@ -20,12 +18,16 @@ var Tracker = /** @class */ (function () {
|
|
|
20
18
|
this.inactivityTimer = {
|
|
21
19
|
since: 0,
|
|
22
20
|
};
|
|
21
|
+
const { tab, tokenProvider, channel, logger, inactivityRetryPolicy, ...options } = config;
|
|
23
22
|
this.tab = tab;
|
|
24
23
|
this.tokenProvider = tokenProvider;
|
|
25
24
|
this.inactivityRetryPolicy = inactivityRetryPolicy;
|
|
26
25
|
this.channel = channel;
|
|
27
26
|
this.logger = logger !== null && logger !== void 0 ? logger : new logging_1.NullLogger();
|
|
28
|
-
this.options =
|
|
27
|
+
this.options = {
|
|
28
|
+
...options,
|
|
29
|
+
eventMetadata: (_a = options.eventMetadata) !== null && _a !== void 0 ? _a : {},
|
|
30
|
+
};
|
|
29
31
|
this.enable = this.enable.bind(this);
|
|
30
32
|
this.disable = this.disable.bind(this);
|
|
31
33
|
this.suspend = this.suspend.bind(this);
|
|
@@ -35,33 +37,29 @@ var Tracker = /** @class */ (function () {
|
|
|
35
37
|
this.trackTabUrlChange = this.trackTabUrlChange.bind(this);
|
|
36
38
|
this.trackInactivity = this.trackInactivity.bind(this);
|
|
37
39
|
}
|
|
38
|
-
|
|
40
|
+
addListener(listener) {
|
|
39
41
|
this.listeners.push(listener);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
}
|
|
43
|
+
removeListener(listener) {
|
|
44
|
+
let index = this.listeners.indexOf(listener);
|
|
43
45
|
while (index >= 0) {
|
|
44
46
|
this.listeners.splice(index, 1);
|
|
45
47
|
index = this.listeners.indexOf(listener);
|
|
46
48
|
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
enumerable: false,
|
|
56
|
-
configurable: true
|
|
57
|
-
});
|
|
58
|
-
Tracker.prototype.isEnabled = function () {
|
|
49
|
+
}
|
|
50
|
+
get flushed() {
|
|
51
|
+
const suppress = () => {
|
|
52
|
+
// suppress errors
|
|
53
|
+
};
|
|
54
|
+
return Promise.all(this.pending).then(suppress, suppress);
|
|
55
|
+
}
|
|
56
|
+
isEnabled() {
|
|
59
57
|
return this.state.enabled;
|
|
60
|
-
}
|
|
61
|
-
|
|
58
|
+
}
|
|
59
|
+
isSuspended() {
|
|
62
60
|
return this.state.suspended;
|
|
63
|
-
}
|
|
64
|
-
|
|
61
|
+
}
|
|
62
|
+
enable() {
|
|
65
63
|
if (this.state.enabled) {
|
|
66
64
|
return;
|
|
67
65
|
}
|
|
@@ -78,8 +76,8 @@ var Tracker = /** @class */ (function () {
|
|
|
78
76
|
this.tab.addListener('load', this.trackPageLoad);
|
|
79
77
|
this.tab.addListener('urlChange', this.trackTabUrlChange);
|
|
80
78
|
this.tab.addListener('visibilityChange', this.trackTabVisibilityChange);
|
|
81
|
-
}
|
|
82
|
-
|
|
79
|
+
}
|
|
80
|
+
disable() {
|
|
83
81
|
if (!this.state.enabled) {
|
|
84
82
|
return;
|
|
85
83
|
}
|
|
@@ -92,8 +90,8 @@ var Tracker = /** @class */ (function () {
|
|
|
92
90
|
this.tab.removeListener('urlChange', this.trackTabUrlChange);
|
|
93
91
|
this.tab.removeListener('visibilityChange', this.trackTabVisibilityChange);
|
|
94
92
|
this.stopInactivityTimer();
|
|
95
|
-
}
|
|
96
|
-
|
|
93
|
+
}
|
|
94
|
+
suspend() {
|
|
97
95
|
if (this.state.suspended) {
|
|
98
96
|
return;
|
|
99
97
|
}
|
|
@@ -103,8 +101,8 @@ var Tracker = /** @class */ (function () {
|
|
|
103
101
|
this.state.enabled = true;
|
|
104
102
|
}
|
|
105
103
|
this.state.suspended = true;
|
|
106
|
-
}
|
|
107
|
-
|
|
104
|
+
}
|
|
105
|
+
unsuspend() {
|
|
108
106
|
if (!this.state.suspended) {
|
|
109
107
|
return;
|
|
110
108
|
}
|
|
@@ -114,12 +112,12 @@ var Tracker = /** @class */ (function () {
|
|
|
114
112
|
this.state.enabled = false;
|
|
115
113
|
this.enable();
|
|
116
114
|
}
|
|
117
|
-
}
|
|
118
|
-
|
|
115
|
+
}
|
|
116
|
+
initialize() {
|
|
119
117
|
if (trackedEvents[this.tab.id] === undefined) {
|
|
120
118
|
trackedEvents[this.tab.id] = {};
|
|
121
119
|
}
|
|
122
|
-
|
|
120
|
+
const initEvents = trackedEvents[this.tab.id];
|
|
123
121
|
if (this.tab.isNew && !initEvents.tabOpened) {
|
|
124
122
|
initEvents.tabOpened = true;
|
|
125
123
|
this.trackTabOpen({ tabId: this.tab.id });
|
|
@@ -131,139 +129,158 @@ var Tracker = /** @class */ (function () {
|
|
|
131
129
|
referrer: this.tab.referrer,
|
|
132
130
|
});
|
|
133
131
|
}
|
|
134
|
-
}
|
|
135
|
-
|
|
132
|
+
}
|
|
133
|
+
stopInactivityTimer() {
|
|
136
134
|
if (this.inactivityTimer.id !== undefined) {
|
|
137
135
|
window.clearTimeout(this.inactivityTimer.id);
|
|
138
136
|
delete this.inactivityTimer.id;
|
|
139
137
|
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
var _this = this;
|
|
138
|
+
}
|
|
139
|
+
startInactivityTimer() {
|
|
143
140
|
this.stopInactivityTimer();
|
|
144
141
|
this.inactivityTimer.since = Date.now();
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (!
|
|
148
|
-
window.clearTimeout(
|
|
142
|
+
let iteration = -1;
|
|
143
|
+
const startTimer = () => {
|
|
144
|
+
if (!this.inactivityRetryPolicy.shouldRetry(iteration + 1, this.inactivityTimer.since)) {
|
|
145
|
+
window.clearTimeout(this.inactivityTimer.id);
|
|
149
146
|
return;
|
|
150
147
|
}
|
|
151
148
|
iteration += 1;
|
|
152
|
-
|
|
153
|
-
|
|
149
|
+
this.inactivityTimer.id = window.setTimeout(() => {
|
|
150
|
+
this.trackInactivity();
|
|
154
151
|
startTimer();
|
|
155
|
-
},
|
|
152
|
+
}, this.inactivityRetryPolicy.getDelay(iteration));
|
|
156
153
|
};
|
|
157
154
|
startTimer();
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
155
|
+
}
|
|
156
|
+
track(event, timestamp = Date.now()) {
|
|
157
|
+
return this.publish(this.enrichEvent(event, timestamp), timestamp).then(() => event);
|
|
158
|
+
}
|
|
159
|
+
trackPageOpen({ referrer, ...payload }) {
|
|
160
|
+
this.enqueue({
|
|
161
|
+
type: 'pageOpened',
|
|
162
|
+
...payload,
|
|
163
|
+
...(referrer.length > 0 ? { referrer: referrer } : {}),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
trackPageLoad({ detail: { tab } }) {
|
|
169
167
|
this.enqueue({
|
|
170
168
|
type: 'pageLoaded',
|
|
171
169
|
url: tab.url,
|
|
172
170
|
title: tab.title,
|
|
173
171
|
lastModifiedTime: Date.parse(tab.document.lastModified),
|
|
174
172
|
});
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
this.enqueue(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
173
|
+
}
|
|
174
|
+
trackTabOpen(payload) {
|
|
175
|
+
this.enqueue({
|
|
176
|
+
type: 'tabOpened',
|
|
177
|
+
...payload,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
trackTabUrlChange({ detail }) {
|
|
181
181
|
this.enqueue({
|
|
182
182
|
type: 'tabUrlChanged',
|
|
183
183
|
tabId: detail.tab.id,
|
|
184
184
|
url: detail.url,
|
|
185
185
|
});
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
var detail = _a.detail;
|
|
186
|
+
}
|
|
187
|
+
trackTabVisibilityChange({ detail }) {
|
|
189
188
|
this.enqueue({
|
|
190
189
|
type: 'tabVisibilityChanged',
|
|
191
190
|
tabId: detail.tab.id,
|
|
192
191
|
visibility: detail.visible ? 'visible' : 'hidden',
|
|
193
192
|
});
|
|
194
|
-
}
|
|
195
|
-
|
|
193
|
+
}
|
|
194
|
+
trackInactivity() {
|
|
196
195
|
this.enqueue({
|
|
197
196
|
type: 'nothingChanged',
|
|
198
197
|
sinceTime: this.inactivityTimer.since,
|
|
199
198
|
});
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
this.publish(event, timestamp).catch(function () {
|
|
199
|
+
}
|
|
200
|
+
enqueue(event, timestamp = Date.now()) {
|
|
201
|
+
this.publish(event, timestamp).catch(() => {
|
|
204
202
|
// suppress error
|
|
205
203
|
});
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
this.listeners.map(
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
var _this = this;
|
|
204
|
+
}
|
|
205
|
+
notifyEvent(event) {
|
|
206
|
+
this.listeners.map(listener => listener(event));
|
|
207
|
+
}
|
|
208
|
+
publish(event, timestamp) {
|
|
212
209
|
if (event.type !== 'nothingChanged') {
|
|
213
210
|
this.stopInactivityTimer();
|
|
214
211
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
212
|
+
const metadata = this.options.eventMetadata;
|
|
213
|
+
const context = {
|
|
214
|
+
tabId: this.tab.id,
|
|
215
|
+
url: this.tab.url,
|
|
216
|
+
...(Object.keys(metadata).length > 0 ? { metadata: metadata } : {}),
|
|
217
|
+
};
|
|
218
|
+
const eventInfo = {
|
|
218
219
|
event: event,
|
|
219
220
|
context: context,
|
|
220
221
|
timestamp: timestamp,
|
|
221
222
|
status: 'pending',
|
|
222
223
|
};
|
|
223
224
|
if (this.state.suspended) {
|
|
224
|
-
this.logger.warn(
|
|
225
|
-
this.notifyEvent(
|
|
225
|
+
this.logger.warn(`Tracker is suspended, ignoring event "${event.type}"`);
|
|
226
|
+
this.notifyEvent({ ...eventInfo, status: 'ignored' });
|
|
226
227
|
return Promise.reject(new Error('The tracker is suspended.'));
|
|
227
228
|
}
|
|
228
|
-
this.logger.info(
|
|
229
|
+
this.logger.info(`Tracked event "${event.type}"`);
|
|
229
230
|
this.notifyEvent(eventInfo);
|
|
230
|
-
return new Promise(
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
231
|
+
return new Promise((resolve, reject) => {
|
|
232
|
+
const promise = this.channel
|
|
233
|
+
.publish(this.createBeacon(event, timestamp, context))
|
|
234
|
+
.then(() => {
|
|
235
|
+
this.logger.debug(`Successfully published event "${event.type}"`);
|
|
236
|
+
this.notifyEvent({ ...eventInfo, status: 'confirmed' });
|
|
234
237
|
resolve(event);
|
|
235
|
-
},
|
|
236
|
-
|
|
237
|
-
|
|
238
|
+
}, cause => {
|
|
239
|
+
this.logger.error(`Failed to publish event "${event.type}", reason: ${(0, error_1.formatCause)(cause)}`);
|
|
240
|
+
this.notifyEvent({ ...eventInfo, status: 'failed' });
|
|
238
241
|
reject(cause);
|
|
239
242
|
});
|
|
240
|
-
|
|
241
|
-
promise.finally(
|
|
242
|
-
|
|
243
|
+
this.pending.push(promise);
|
|
244
|
+
promise.finally(() => {
|
|
245
|
+
this.pending.splice(this.pending.indexOf(promise), 1);
|
|
243
246
|
});
|
|
244
|
-
if (
|
|
245
|
-
|
|
247
|
+
if (this.state.enabled && event.type !== 'nothingChanged') {
|
|
248
|
+
this.startInactivityTimer();
|
|
246
249
|
}
|
|
247
250
|
});
|
|
248
|
-
}
|
|
249
|
-
|
|
251
|
+
}
|
|
252
|
+
enrichEvent(event, timestamp) {
|
|
250
253
|
if ((0, trackingEvents_1.isCartPartialEvent)(event)) {
|
|
251
|
-
|
|
252
|
-
return
|
|
254
|
+
const { cart: { lastUpdateTime = timestamp, ...cart }, ...payload } = event;
|
|
255
|
+
return {
|
|
256
|
+
...payload,
|
|
257
|
+
cart: {
|
|
258
|
+
...cart,
|
|
259
|
+
lastUpdateTime: lastUpdateTime,
|
|
260
|
+
},
|
|
261
|
+
};
|
|
253
262
|
}
|
|
254
263
|
return event;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
return
|
|
259
|
-
|
|
260
|
-
|
|
264
|
+
}
|
|
265
|
+
createBeacon(event, timestamp, context) {
|
|
266
|
+
const token = this.tokenProvider.getToken();
|
|
267
|
+
return {
|
|
268
|
+
timestamp: timestamp,
|
|
269
|
+
...(token !== null ? { token: token.toString() } : {}),
|
|
270
|
+
context: context,
|
|
271
|
+
payload: this.enrichBeaconPayload(this.createBeaconPayload(event)),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
createBeaconPayload(event) {
|
|
261
275
|
if (!(0, trackingEvents_1.isIdentifiedUserEvent)(event)) {
|
|
262
276
|
return event;
|
|
263
277
|
}
|
|
264
278
|
if (event.type === 'userSignedUp' && event.profile !== undefined) {
|
|
265
|
-
|
|
266
|
-
return
|
|
279
|
+
const { userId, profile, ...payload } = event;
|
|
280
|
+
return {
|
|
281
|
+
...payload,
|
|
282
|
+
externalUserId: userId,
|
|
283
|
+
patch: {
|
|
267
284
|
operations: [
|
|
268
285
|
{
|
|
269
286
|
type: 'set',
|
|
@@ -271,19 +288,25 @@ var Tracker = /** @class */ (function () {
|
|
|
271
288
|
value: profile,
|
|
272
289
|
},
|
|
273
290
|
],
|
|
274
|
-
}
|
|
291
|
+
},
|
|
292
|
+
};
|
|
275
293
|
}
|
|
276
|
-
|
|
277
|
-
return
|
|
278
|
-
|
|
279
|
-
|
|
294
|
+
const { userId, ...payload } = event;
|
|
295
|
+
return {
|
|
296
|
+
...payload,
|
|
297
|
+
externalUserId: userId,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
enrichBeaconPayload(event) {
|
|
280
301
|
switch (event.type) {
|
|
281
302
|
case 'linkOpened':
|
|
282
|
-
return
|
|
303
|
+
return {
|
|
304
|
+
...event,
|
|
305
|
+
link: new URL(event.link, this.tab.url).toString(),
|
|
306
|
+
};
|
|
283
307
|
default:
|
|
284
308
|
return event;
|
|
285
309
|
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
}());
|
|
310
|
+
}
|
|
311
|
+
}
|
|
289
312
|
exports.Tracker = Tracker;
|
package/trackingEvents.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Patch } from './patch';
|
|
2
2
|
import { DistributiveOmit, Optional } from './utilityTypes';
|
|
3
|
-
export
|
|
3
|
+
export type ProductDetails = {
|
|
4
4
|
productId: string;
|
|
5
5
|
productSku?: string;
|
|
6
6
|
name: string;
|
|
@@ -12,7 +12,7 @@ export declare type ProductDetails = {
|
|
|
12
12
|
url?: string;
|
|
13
13
|
imageUrl?: string;
|
|
14
14
|
};
|
|
15
|
-
export
|
|
15
|
+
export type CartItem = {
|
|
16
16
|
index: number;
|
|
17
17
|
product: ProductDetails;
|
|
18
18
|
quantity: number;
|
|
@@ -20,7 +20,7 @@ export declare type CartItem = {
|
|
|
20
20
|
discount?: number;
|
|
21
21
|
coupon?: string;
|
|
22
22
|
};
|
|
23
|
-
export
|
|
23
|
+
export type Cart = {
|
|
24
24
|
currency: string;
|
|
25
25
|
items: CartItem[];
|
|
26
26
|
subtotal?: number;
|
|
@@ -36,7 +36,7 @@ export declare type Cart = {
|
|
|
36
36
|
coupon?: string;
|
|
37
37
|
lastUpdateTime: number;
|
|
38
38
|
};
|
|
39
|
-
export
|
|
39
|
+
export type OrderItem = {
|
|
40
40
|
index: number;
|
|
41
41
|
product: ProductDetails;
|
|
42
42
|
quantity?: number;
|
|
@@ -44,8 +44,8 @@ export declare type OrderItem = {
|
|
|
44
44
|
discount?: number;
|
|
45
45
|
coupon?: string;
|
|
46
46
|
};
|
|
47
|
-
export
|
|
48
|
-
export
|
|
47
|
+
export type OrderStatus = 'placed' | 'paid' | 'completed';
|
|
48
|
+
export type Order = {
|
|
49
49
|
orderId: string;
|
|
50
50
|
currency: string;
|
|
51
51
|
items: OrderItem[];
|
|
@@ -64,7 +64,7 @@ export declare type Order = {
|
|
|
64
64
|
installments?: number;
|
|
65
65
|
status?: OrderStatus;
|
|
66
66
|
};
|
|
67
|
-
export
|
|
67
|
+
export type Gender = 'male' | 'female' | 'neutral' | 'unknown';
|
|
68
68
|
export declare const pageEventTypes: readonly ["pageLoaded", "pageOpened"];
|
|
69
69
|
export declare const tabEventTypes: readonly ["tabOpened", "tabUrlChanged", "tabVisibilityChanged"];
|
|
70
70
|
export declare const cartEventTypes: readonly ["cartModified", "cartViewed", "checkoutStarted"];
|
|
@@ -80,17 +80,17 @@ export interface UserProfileChanged extends BaseEvent {
|
|
|
80
80
|
type: 'userProfileChanged';
|
|
81
81
|
patch: Patch;
|
|
82
82
|
}
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
type Primitive = null | string | number | boolean;
|
|
84
|
+
type PrimitiveMap = {
|
|
85
85
|
[member: string]: Primitive;
|
|
86
86
|
};
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
type PrimitiveArray = Primitive[];
|
|
88
|
+
type TwoLevelMap = {
|
|
89
89
|
[member: string]: Primitive | PrimitiveMap | PrimitiveArray;
|
|
90
90
|
};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
type TwoLevelArray = PrimitiveArray | PrimitiveMap[] | PrimitiveArray[];
|
|
92
|
+
type CustomAttribute = Primitive | TwoLevelMap | TwoLevelArray;
|
|
93
|
+
type UserProfile = {
|
|
94
94
|
firstName?: string;
|
|
95
95
|
lastName?: string;
|
|
96
96
|
birthDate?: string;
|
|
@@ -130,9 +130,9 @@ export interface UserSignedOut extends BaseEvent {
|
|
|
130
130
|
type: 'userSignedOut';
|
|
131
131
|
userId: string;
|
|
132
132
|
}
|
|
133
|
-
export
|
|
134
|
-
export
|
|
135
|
-
export
|
|
133
|
+
export type IdentifiedUserEvent = UserSignedIn | UserSignedOut | UserSignedUp;
|
|
134
|
+
export type UserEvent = UserProfileChanged | IdentifiedUserEvent;
|
|
135
|
+
export type CartEventType = typeof cartEventTypes[number];
|
|
136
136
|
interface BaseCartEvent extends BaseEvent {
|
|
137
137
|
type: CartEventType;
|
|
138
138
|
cart: Cart;
|
|
@@ -147,7 +147,7 @@ export interface CheckoutStarted extends BaseCartEvent {
|
|
|
147
147
|
type: 'checkoutStarted';
|
|
148
148
|
orderId?: string;
|
|
149
149
|
}
|
|
150
|
-
export
|
|
150
|
+
export type CartEvent = CartModified | CartViewed | CheckoutStarted;
|
|
151
151
|
export interface OrderPlaced extends BaseEvent {
|
|
152
152
|
type: 'orderPlaced';
|
|
153
153
|
order: Order;
|
|
@@ -156,11 +156,11 @@ export interface ProductViewed extends BaseEvent {
|
|
|
156
156
|
type: 'productViewed';
|
|
157
157
|
product: ProductDetails;
|
|
158
158
|
}
|
|
159
|
-
export
|
|
159
|
+
export type EcommerceEvent = OrderPlaced | ProductViewed | CartEvent;
|
|
160
160
|
/**
|
|
161
161
|
* Tab event
|
|
162
162
|
*/
|
|
163
|
-
export
|
|
163
|
+
export type TabEventType = typeof tabEventTypes[number];
|
|
164
164
|
interface BaseTabEvent extends BaseEvent {
|
|
165
165
|
type: TabEventType;
|
|
166
166
|
tabId: string;
|
|
@@ -176,8 +176,8 @@ export interface TabVisibilityChanged extends BaseTabEvent {
|
|
|
176
176
|
type: 'tabVisibilityChanged';
|
|
177
177
|
visibility: 'visible' | 'hidden';
|
|
178
178
|
}
|
|
179
|
-
export
|
|
180
|
-
export
|
|
179
|
+
export type TabEvent = TabVisibilityChanged | TabUrlChanged | TabOpened;
|
|
180
|
+
export type PageEventType = typeof pageEventTypes[number];
|
|
181
181
|
interface BasePageEvent extends BaseEvent {
|
|
182
182
|
type: PageEventType;
|
|
183
183
|
url: string;
|
|
@@ -191,7 +191,7 @@ export interface PageLoaded extends BasePageEvent {
|
|
|
191
191
|
title: string;
|
|
192
192
|
lastModifiedTime: number;
|
|
193
193
|
}
|
|
194
|
-
export
|
|
194
|
+
export type PageEvent = PageLoaded | PageOpened;
|
|
195
195
|
export interface NothingChanged extends BaseEvent {
|
|
196
196
|
type: 'nothingChanged';
|
|
197
197
|
sinceTime: number;
|
|
@@ -239,8 +239,8 @@ export interface LinkOpened extends BaseEvent {
|
|
|
239
239
|
type: 'linkOpened';
|
|
240
240
|
link: string;
|
|
241
241
|
}
|
|
242
|
-
export
|
|
243
|
-
|
|
242
|
+
export type MiscEvent = NothingChanged | SessionAttributesChanged | EventOccurred | GoalCompleted | InterestShown | PostViewed | LinkOpened;
|
|
243
|
+
type EventMap = {
|
|
244
244
|
tabVisibilityChanged: TabVisibilityChanged;
|
|
245
245
|
tabUrlChanged: TabUrlChanged;
|
|
246
246
|
tabOpened: TabOpened;
|
|
@@ -263,17 +263,17 @@ declare type EventMap = {
|
|
|
263
263
|
eventOccurred: EventOccurred;
|
|
264
264
|
linkOpened: LinkOpened;
|
|
265
265
|
};
|
|
266
|
-
export
|
|
267
|
-
export
|
|
266
|
+
export type TrackingEventType = keyof EventMap;
|
|
267
|
+
export type TrackingEvent<T extends TrackingEventType = TrackingEventType> = T extends TrackingEventType ? EventMap[T] : EventMap[TrackingEventType];
|
|
268
268
|
/**
|
|
269
269
|
* Partial Events
|
|
270
270
|
*/
|
|
271
|
-
|
|
272
|
-
export
|
|
271
|
+
type CartPartialEvent<T extends CartEvent = CartEvent> = DistributiveOmit<T, 'cart'> & Record<'cart', Optional<Cart, 'lastUpdateTime'>>;
|
|
272
|
+
export type PartialTrackingEvent = Exclude<TrackingEvent, PageEvent | TabEvent | CartEvent> | CartPartialEvent;
|
|
273
273
|
/**
|
|
274
274
|
* External Events
|
|
275
275
|
*/
|
|
276
|
-
|
|
276
|
+
type ExternalEventMap = {
|
|
277
277
|
cartModified: CartPartialEvent<CartModified>;
|
|
278
278
|
cartViewed: CartPartialEvent<CartViewed>;
|
|
279
279
|
checkoutStarted: CartPartialEvent<CheckoutStarted>;
|
|
@@ -286,22 +286,22 @@ declare type ExternalEventMap = {
|
|
|
286
286
|
linkOpened: LinkOpened;
|
|
287
287
|
eventOccurred: EventOccurred;
|
|
288
288
|
};
|
|
289
|
-
export
|
|
290
|
-
export
|
|
291
|
-
export
|
|
289
|
+
export type ExternalTrackingEventType = keyof ExternalEventMap;
|
|
290
|
+
export type ExternalTrackingEvent<T extends ExternalTrackingEventType = ExternalTrackingEventType> = T extends ExternalTrackingEventType ? ExternalEventMap[T] : ExternalEventMap[ExternalTrackingEventType];
|
|
291
|
+
export type ExternalTrackingEventPayload<T extends ExternalTrackingEventType> = Omit<ExternalEventMap[T], 'type'>;
|
|
292
292
|
export declare function isIdentifiedUserEvent(event: TrackingEvent): event is IdentifiedUserEvent;
|
|
293
293
|
export declare function isCartPartialEvent(event: PartialTrackingEvent): event is CartPartialEvent;
|
|
294
|
-
export
|
|
294
|
+
export type TrackingEventContext = {
|
|
295
295
|
tabId: string;
|
|
296
296
|
url: string;
|
|
297
297
|
metadata?: {
|
|
298
298
|
[key: string]: string;
|
|
299
299
|
};
|
|
300
300
|
};
|
|
301
|
-
export
|
|
301
|
+
export type BeaconPayload = Exclude<TrackingEvent, IdentifiedUserEvent> | DistributiveOmit<Exclude<IdentifiedUserEvent, UserSignedUp>, 'userId'> & Record<'externalUserId', IdentifiedUserEvent['userId']> | Omit<UserSignedUp, 'userId' | 'profile'> & Record<'externalUserId', IdentifiedUserEvent['userId']> & {
|
|
302
302
|
patch?: Patch;
|
|
303
303
|
};
|
|
304
|
-
export
|
|
304
|
+
export type Beacon = {
|
|
305
305
|
timestamp: number;
|
|
306
306
|
token?: string;
|
|
307
307
|
context: TrackingEventContext;
|
package/trackingEvents.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isCartPartialEvent = exports.isIdentifiedUserEvent = exports.eventTypes = exports.miscEventTypes = exports.userEventTypes = exports.identifiedUserEventTypes = exports.ecommerceEventTypes = exports.cartEventTypes = exports.tabEventTypes = exports.pageEventTypes = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
4
|
/*
|
|
6
5
|
* Events
|
|
7
6
|
*/
|
|
@@ -19,18 +18,20 @@ exports.cartEventTypes = [
|
|
|
19
18
|
'cartViewed',
|
|
20
19
|
'checkoutStarted',
|
|
21
20
|
];
|
|
22
|
-
exports.ecommerceEventTypes =
|
|
21
|
+
exports.ecommerceEventTypes = [
|
|
22
|
+
...exports.cartEventTypes,
|
|
23
23
|
'orderPlaced',
|
|
24
24
|
'productViewed',
|
|
25
|
-
]
|
|
25
|
+
];
|
|
26
26
|
exports.identifiedUserEventTypes = [
|
|
27
27
|
'userSignedIn',
|
|
28
28
|
'userSignedOut',
|
|
29
29
|
'userSignedUp',
|
|
30
30
|
];
|
|
31
|
-
exports.userEventTypes =
|
|
31
|
+
exports.userEventTypes = [
|
|
32
|
+
...exports.identifiedUserEventTypes,
|
|
32
33
|
'userProfileChanged',
|
|
33
|
-
]
|
|
34
|
+
];
|
|
34
35
|
exports.miscEventTypes = [
|
|
35
36
|
'nothingChanged',
|
|
36
37
|
'sessionAttributesChanged',
|
|
@@ -40,7 +41,12 @@ exports.miscEventTypes = [
|
|
|
40
41
|
'eventOccurred',
|
|
41
42
|
'linkOpened',
|
|
42
43
|
];
|
|
43
|
-
exports.eventTypes =
|
|
44
|
+
exports.eventTypes = [
|
|
45
|
+
...exports.pageEventTypes,
|
|
46
|
+
...exports.ecommerceEventTypes,
|
|
47
|
+
...exports.userEventTypes,
|
|
48
|
+
...exports.miscEventTypes,
|
|
49
|
+
];
|
|
44
50
|
/*
|
|
45
51
|
* Type guards
|
|
46
52
|
*/
|
package/transformer.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.encodeJson = void 0;
|
|
4
|
-
|
|
4
|
+
const encodeJson = function encodeJson(input) {
|
|
5
5
|
return Promise.resolve(JSON.stringify(input));
|
|
6
6
|
};
|
|
7
7
|
exports.encodeJson = encodeJson;
|
package/utilityTypes.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export type DistributiveOmit<T, K extends keyof T> = T extends unknown ? Omit<T, K> : never;
|
|
2
|
+
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|