@jitsu/js 1.0.0-canary-20230219230449 → 1.1.0-canary.14.20230602211353
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/.turbo/turbo-build.log +95 -65
- package/.turbo/turbo-clean.log +5 -5
- package/__tests__/node/nodejs.test.ts +15 -7
- package/__tests__/playwright/integration.test.ts +1 -2
- package/dist/analytics-plugin.d.ts +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/jitsu.cjs.js +304 -59
- package/dist/jitsu.d.ts +1 -1
- package/dist/jitsu.es.js +304 -60
- package/dist/version.d.ts +2 -0
- package/dist/web/p.js.txt +295 -59
- package/package.json +6 -5
- package/src/analytics-plugin.ts +56 -16
- package/src/destination-plugins/gtm.ts +112 -0
- package/src/destination-plugins/index.ts +47 -0
- package/src/destination-plugins/logrocket.ts +83 -0
- package/src/{destination-plugins.ts → destination-plugins/tag.ts} +3 -28
- package/src/index.ts +21 -3
- package/src/jitsu.ts +1 -1
- package/src/version.ts +3 -0
- package/tsconfig.json +1 -0
- package/dist/destination-plugins.d.ts +0 -13
package/dist/jitsu.es.js
CHANGED
|
@@ -65,7 +65,50 @@ function analyticsLib() {
|
|
|
65
65
|
return ke(_objectSpread2(_objectSpread2({}, defaultSettings), opts));
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
function findScript(src) {
|
|
69
|
+
const scripts = Array.prototype.slice.call(window.document.querySelectorAll("script"));
|
|
70
|
+
return scripts.find(s => s.src === src);
|
|
71
|
+
}
|
|
72
|
+
function loadScript(src, attributes) {
|
|
73
|
+
const found = findScript(src);
|
|
74
|
+
if (found !== undefined) {
|
|
75
|
+
const status = found === null || found === void 0 ? void 0 : found.getAttribute("status");
|
|
76
|
+
if (status === "loaded") {
|
|
77
|
+
return Promise.resolve(found);
|
|
78
|
+
}
|
|
79
|
+
if (status === "loading") {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
found.addEventListener("load", () => resolve(found));
|
|
82
|
+
found.addEventListener("error", err => reject(err));
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return new Promise((resolve, reject) => {
|
|
87
|
+
var _a;
|
|
88
|
+
const script = window.document.createElement("script");
|
|
89
|
+
script.type = "text/javascript";
|
|
90
|
+
script.src = src;
|
|
91
|
+
script.async = true;
|
|
92
|
+
script.setAttribute("status", "loading");
|
|
93
|
+
for (const [k, v] of Object.entries(attributes !== null && attributes !== void 0 ? attributes : {})) {
|
|
94
|
+
script.setAttribute(k, v);
|
|
95
|
+
}
|
|
96
|
+
script.onload = () => {
|
|
97
|
+
script.onerror = script.onload = null;
|
|
98
|
+
script.setAttribute("status", "loaded");
|
|
99
|
+
resolve(script);
|
|
100
|
+
};
|
|
101
|
+
script.onerror = () => {
|
|
102
|
+
script.onerror = script.onload = null;
|
|
103
|
+
script.setAttribute("status", "error");
|
|
104
|
+
reject(new Error(`Failed to load ${src}`));
|
|
105
|
+
};
|
|
106
|
+
const tag = window.document.getElementsByTagName("script")[0];
|
|
107
|
+
(_a = tag.parentElement) === null || _a === void 0 ? void 0 : _a.insertBefore(script, tag);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
69
112
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
70
113
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
71
114
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
@@ -74,18 +117,10 @@ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _argu
|
|
|
74
117
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
75
118
|
});
|
|
76
119
|
};
|
|
77
|
-
function satisfyFilter(filter, subject) {
|
|
78
|
-
return filter === "*" || filter.toLowerCase().trim() === (subject || "").trim().toLowerCase();
|
|
79
|
-
}
|
|
80
|
-
function applyFilters(event, creds) {
|
|
81
|
-
const { hosts = ["*"], events = ["*"] } = creds;
|
|
82
|
-
return (!!hosts.find(hostFilter => { var _a; return satisfyFilter(hostFilter, (_a = event.context) === null || _a === void 0 ? void 0 : _a.host); }) &&
|
|
83
|
-
!!events.find(eventFilter => satisfyFilter(eventFilter, event.type)));
|
|
84
|
-
}
|
|
85
120
|
const tagPlugin = {
|
|
86
121
|
id: "tag",
|
|
87
122
|
handle(config, payload) {
|
|
88
|
-
return __awaiter$
|
|
123
|
+
return __awaiter$3(this, void 0, void 0, function* () {
|
|
89
124
|
if (!applyFilters(payload, config)) {
|
|
90
125
|
return;
|
|
91
126
|
}
|
|
@@ -153,53 +188,221 @@ function execJs(code, event) {
|
|
|
153
188
|
function replaceMacro(code, event) {
|
|
154
189
|
return code.replace(/{{\s*event\s*}}/g, JSON.stringify(event));
|
|
155
190
|
}
|
|
156
|
-
const internalDestinationPlugins = {
|
|
157
|
-
[tagPlugin.id]: tagPlugin,
|
|
158
|
-
};
|
|
159
191
|
|
|
160
|
-
function
|
|
161
|
-
|
|
162
|
-
return
|
|
192
|
+
var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
193
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
194
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
195
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
196
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
197
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
198
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
const logrocketPlugin = {
|
|
202
|
+
id: "logrocket",
|
|
203
|
+
handle(config, payload) {
|
|
204
|
+
return __awaiter$2(this, void 0, void 0, function* () {
|
|
205
|
+
if (!applyFilters(payload, config)) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
initLogrocketIfNeeded(config.appId);
|
|
209
|
+
const action = logRocket => {
|
|
210
|
+
if (payload.type === "identify" && payload.userId) {
|
|
211
|
+
logRocket.identify(payload.userId, payload.traits || {});
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
getLogRocketQueue().push(action);
|
|
215
|
+
if (getLogRocketState() === "loaded") {
|
|
216
|
+
flushLogRocketQueue(window["LogRocket"]);
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
function getLogRocketState() {
|
|
222
|
+
return window["__jitsuLrState"] || "fresh";
|
|
163
223
|
}
|
|
164
|
-
function
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
224
|
+
function setLogRocketState(s) {
|
|
225
|
+
window["__jitsuLrState"] = s;
|
|
226
|
+
}
|
|
227
|
+
function getLogRocketQueue() {
|
|
228
|
+
return window["__jitsuLrQueue"] || (window["__jitsuLrQueue"] = []);
|
|
229
|
+
}
|
|
230
|
+
function flushLogRocketQueue(lr) {
|
|
231
|
+
const queue = getLogRocketQueue();
|
|
232
|
+
while (queue.length > 0) {
|
|
233
|
+
const method = queue.shift();
|
|
234
|
+
try {
|
|
235
|
+
const res = method(lr);
|
|
236
|
+
if (res) {
|
|
237
|
+
res.catch(e => console.warn(`Async LogRocket method failed: ${e.message}`, e));
|
|
238
|
+
}
|
|
170
239
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
found.addEventListener("load", () => resolve(found));
|
|
174
|
-
found.addEventListener("error", err => reject(err));
|
|
175
|
-
});
|
|
240
|
+
catch (e) {
|
|
241
|
+
console.warn(`LogRocket method failed: ${e.message}`, e);
|
|
176
242
|
}
|
|
177
243
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
script.async = true;
|
|
184
|
-
script.setAttribute("status", "loading");
|
|
185
|
-
for (const [k, v] of Object.entries(attributes !== null && attributes !== void 0 ? attributes : {})) {
|
|
186
|
-
script.setAttribute(k, v);
|
|
244
|
+
}
|
|
245
|
+
function initLogrocketIfNeeded(appId) {
|
|
246
|
+
return __awaiter$2(this, void 0, void 0, function* () {
|
|
247
|
+
if (getLogRocketState() !== "fresh") {
|
|
248
|
+
return;
|
|
187
249
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
250
|
+
setLogRocketState("loading");
|
|
251
|
+
loadScript(`https://cdn.lr-ingest.io/LogRocket.min.js`, { crossOrigin: "anonymous" })
|
|
252
|
+
.then(() => {
|
|
253
|
+
if (window["LogRocket"]) {
|
|
254
|
+
try {
|
|
255
|
+
window["LogRocket"].init(appId);
|
|
256
|
+
}
|
|
257
|
+
catch (e) {
|
|
258
|
+
console.warn(`LogRocket (id=${appId}) init failed: ${e.message}`, e);
|
|
259
|
+
setLogRocketState("failed");
|
|
260
|
+
}
|
|
261
|
+
setLogRocketState("loaded");
|
|
262
|
+
flushLogRocketQueue(window["LogRocket"]);
|
|
263
|
+
}
|
|
264
|
+
})
|
|
265
|
+
.catch(e => {
|
|
266
|
+
console.warn(`LogRocket (id=${appId}) init failed: ${e.message}`, e);
|
|
267
|
+
setLogRocketState("failed");
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
273
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
274
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
275
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
276
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
277
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
278
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
279
|
+
});
|
|
280
|
+
};
|
|
281
|
+
const defaultScriptSrc = "https://www.googletagmanager.com/gtag/js";
|
|
282
|
+
const gtmPlugin = {
|
|
283
|
+
id: "gtm",
|
|
284
|
+
handle(config, payload) {
|
|
285
|
+
var _a, _b;
|
|
286
|
+
return __awaiter$1(this, void 0, void 0, function* () {
|
|
287
|
+
if (!applyFilters(payload, config)) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
yield initGtmIfNeeded(config);
|
|
291
|
+
const dataLayer = window[config.dataLayerName || "dataLayer"];
|
|
292
|
+
switch (payload.type) {
|
|
293
|
+
case "page":
|
|
294
|
+
const { properties: pageProperties, context } = payload;
|
|
295
|
+
const pageEvent = {
|
|
296
|
+
event: "page_view",
|
|
297
|
+
url: pageProperties.url,
|
|
298
|
+
title: pageProperties.title,
|
|
299
|
+
referer: (_b = (_a = context === null || context === void 0 ? void 0 : context.page) === null || _a === void 0 ? void 0 : _a.referrer) !== null && _b !== void 0 ? _b : "",
|
|
300
|
+
};
|
|
301
|
+
if (config.debug) {
|
|
302
|
+
console.log("gtag push", pageEvent);
|
|
303
|
+
}
|
|
304
|
+
dataLayer.push(pageEvent);
|
|
305
|
+
break;
|
|
306
|
+
case "track":
|
|
307
|
+
const { properties: trackProperties } = payload;
|
|
308
|
+
const trackEvent = Object.assign({ event: payload.event }, trackProperties);
|
|
309
|
+
if (payload.userId) {
|
|
310
|
+
trackEvent.userId = payload.userId;
|
|
311
|
+
}
|
|
312
|
+
if (payload.anonymousId) {
|
|
313
|
+
trackEvent.anonymousId = payload.anonymousId;
|
|
314
|
+
}
|
|
315
|
+
if (config.debug) {
|
|
316
|
+
console.log("gtag push", trackEvent);
|
|
317
|
+
}
|
|
318
|
+
dataLayer.push(trackEvent);
|
|
319
|
+
break;
|
|
320
|
+
case "identify":
|
|
321
|
+
const { traits } = payload;
|
|
322
|
+
const identifyEvent = Object.assign({ event: "identify" }, traits);
|
|
323
|
+
if (payload.userId) {
|
|
324
|
+
identifyEvent.userId = payload.userId;
|
|
325
|
+
}
|
|
326
|
+
if (payload.anonymousId) {
|
|
327
|
+
identifyEvent.anonymousId = payload.anonymousId;
|
|
328
|
+
}
|
|
329
|
+
if (config.debug) {
|
|
330
|
+
console.log("gtag push", identifyEvent);
|
|
331
|
+
}
|
|
332
|
+
dataLayer.push(identifyEvent);
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
},
|
|
337
|
+
};
|
|
338
|
+
function getGtmState() {
|
|
339
|
+
return window["__jitsuGtmState"] || "fresh";
|
|
340
|
+
}
|
|
341
|
+
function setGtmState(s) {
|
|
342
|
+
window["__jitsuGtmState"] = s;
|
|
343
|
+
}
|
|
344
|
+
function initGtmIfNeeded(config) {
|
|
345
|
+
return __awaiter$1(this, void 0, void 0, function* () {
|
|
346
|
+
if (getGtmState() !== "fresh") {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
setGtmState("loading");
|
|
350
|
+
const dlName = config.dataLayerName || "dataLayer";
|
|
351
|
+
const dlParam = dlName !== "dataLayer" ? "&l=" + dlName : "";
|
|
352
|
+
const previewParams = config.preview
|
|
353
|
+
? `>m_preview=${config.preview}>m_auth=${config.auth}>m_cookies_win=x`
|
|
354
|
+
: "";
|
|
355
|
+
const scriptSrc = `${config.customScriptSrc || defaultScriptSrc}?id=${config.containerId}${dlParam}${previewParams}`;
|
|
356
|
+
window[dlName] = window[dlName] || [];
|
|
357
|
+
const gtag = function () {
|
|
358
|
+
window[dlName].push(arguments);
|
|
197
359
|
};
|
|
198
|
-
|
|
199
|
-
(
|
|
360
|
+
// @ts-ignore
|
|
361
|
+
gtag("js", new Date());
|
|
362
|
+
// @ts-ignore
|
|
363
|
+
gtag("config", config.containerId);
|
|
364
|
+
loadScript(scriptSrc)
|
|
365
|
+
.then(() => {
|
|
366
|
+
setGtmState("loaded");
|
|
367
|
+
})
|
|
368
|
+
.catch(e => {
|
|
369
|
+
console.warn(`GTM (containerId=${config.containerId}) init failed: ${e.message}`, e);
|
|
370
|
+
setGtmState("failed");
|
|
371
|
+
});
|
|
200
372
|
});
|
|
201
373
|
}
|
|
202
374
|
|
|
375
|
+
function satisfyFilter(filter, subject) {
|
|
376
|
+
return filter === "*" || filter.toLowerCase().trim() === (subject || "").trim().toLowerCase();
|
|
377
|
+
}
|
|
378
|
+
function satisfyDomainFilter(filter, subject) {
|
|
379
|
+
if (filter === "*") {
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
subject = subject || "";
|
|
383
|
+
if (filter.startsWith("*.")) {
|
|
384
|
+
return subject.endsWith(filter.substring(1));
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
return filter === subject;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
function applyFilters(event, creds) {
|
|
391
|
+
const { hosts = "*", events = "*" } = creds;
|
|
392
|
+
const eventsArray = events.split("\n");
|
|
393
|
+
return (!!hosts.split("\n").find(hostFilter => { var _a; return satisfyDomainFilter(hostFilter, (_a = event.context) === null || _a === void 0 ? void 0 : _a.host); }) &&
|
|
394
|
+
(!!eventsArray.find(eventFilter => satisfyFilter(eventFilter, event.type)) ||
|
|
395
|
+
!!eventsArray.find(eventFilter => satisfyFilter(eventFilter, event.event))));
|
|
396
|
+
}
|
|
397
|
+
const internalDestinationPlugins = {
|
|
398
|
+
[tagPlugin.id]: tagPlugin,
|
|
399
|
+
[gtmPlugin.id]: gtmPlugin,
|
|
400
|
+
[logrocketPlugin.id]: logrocketPlugin,
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
var name = "@jitsu/js";
|
|
404
|
+
var version = "0.0.0";
|
|
405
|
+
|
|
203
406
|
/* global analytics */
|
|
204
407
|
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
205
408
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
@@ -210,7 +413,7 @@ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _argume
|
|
|
210
413
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
211
414
|
});
|
|
212
415
|
};
|
|
213
|
-
const
|
|
416
|
+
const defaultConfig = {
|
|
214
417
|
/* Your segment writeKey */
|
|
215
418
|
writeKey: null,
|
|
216
419
|
/* Disable anonymous MTU */
|
|
@@ -218,6 +421,8 @@ const config = {
|
|
|
218
421
|
debug: false,
|
|
219
422
|
fetch: null,
|
|
220
423
|
echoEvents: false,
|
|
424
|
+
cookieDomain: undefined,
|
|
425
|
+
runtime: undefined,
|
|
221
426
|
};
|
|
222
427
|
const parseQuery = (qs) => {
|
|
223
428
|
if (!qs) {
|
|
@@ -435,13 +640,13 @@ function adjustPayload(payload, config, storage) {
|
|
|
435
640
|
const referrer = runtime.referrer();
|
|
436
641
|
const context = {
|
|
437
642
|
library: {
|
|
438
|
-
name:
|
|
439
|
-
version:
|
|
643
|
+
name: name,
|
|
644
|
+
version: version,
|
|
440
645
|
},
|
|
441
646
|
userAgent: runtime.userAgent(),
|
|
442
647
|
locale: runtime.language(),
|
|
443
648
|
screen: runtime.screen(),
|
|
444
|
-
traits: payload.type != "identify" ? Object.assign({}, (restoreTraits(storage) || {})) : undefined,
|
|
649
|
+
traits: payload.type != "identify" && payload.type != "group" ? Object.assign({}, (restoreTraits(storage) || {})) : undefined,
|
|
445
650
|
page: {
|
|
446
651
|
path: properties.path || (parsedUrl && parsedUrl.pathname),
|
|
447
652
|
referrer: referrer,
|
|
@@ -450,11 +655,11 @@ function adjustPayload(payload, config, storage) {
|
|
|
450
655
|
search: properties.search || (parsedUrl && parsedUrl.search),
|
|
451
656
|
title: properties.title || runtime.pageTitle(),
|
|
452
657
|
url: properties.url || url,
|
|
453
|
-
|
|
658
|
+
encoding: properties.encoding || runtime.documentEncoding(),
|
|
454
659
|
},
|
|
455
660
|
campaign: parseUtms(query),
|
|
456
661
|
};
|
|
457
|
-
const withContext = Object.assign(Object.assign({}, payload), { timestamp: new Date().toISOString(), sentAt: new Date().toISOString(), messageId: randomId(properties.path || (parsedUrl && parsedUrl.pathname)), writeKey: config.writeKey, context: deepMerge(context, customContext) });
|
|
662
|
+
const withContext = Object.assign(Object.assign({}, payload), { timestamp: new Date().toISOString(), sentAt: new Date().toISOString(), messageId: randomId(properties.path || (parsedUrl && parsedUrl.pathname)), writeKey: validateWriteKey(config.writeKey), context: deepMerge(context, customContext) });
|
|
458
663
|
delete withContext.meta;
|
|
459
664
|
delete withContext.options;
|
|
460
665
|
return withContext;
|
|
@@ -463,11 +668,12 @@ function processDestinations(destinations, method, event, debug, analyticsInstan
|
|
|
463
668
|
return __awaiter(this, void 0, void 0, function* () {
|
|
464
669
|
const promises = [];
|
|
465
670
|
for (const destination of destinations) {
|
|
671
|
+
const credentials = Object.assign(Object.assign({}, destination.credentials), destination.options);
|
|
466
672
|
if (destination.deviceOptions.type === "internal-plugin") {
|
|
467
673
|
const plugin = internalDestinationPlugins[destination.deviceOptions.name];
|
|
468
674
|
if (plugin) {
|
|
469
675
|
try {
|
|
470
|
-
promises.push(plugin.handle(
|
|
676
|
+
promises.push(plugin.handle(credentials, event));
|
|
471
677
|
}
|
|
472
678
|
catch (e) {
|
|
473
679
|
console.warn(`[JITSU] Error processing event with internal plugin '${destination.deviceOptions.name}': ${e === null || e === void 0 ? void 0 : e.message}`, e);
|
|
@@ -486,7 +692,7 @@ function processDestinations(destinations, method, event, debug, analyticsInstan
|
|
|
486
692
|
else {
|
|
487
693
|
let pluginInstance;
|
|
488
694
|
try {
|
|
489
|
-
pluginInstance = (typeof plugin === "function" ? plugin : plugin.init)(
|
|
695
|
+
pluginInstance = (typeof plugin === "function" ? plugin : plugin.init)(credentials);
|
|
490
696
|
}
|
|
491
697
|
catch (e) {
|
|
492
698
|
console.warn(`[JITSU] Error creating plugin '${destination.deviceOptions.moduleVarName}@${destination.deviceOptions.packageCdn}' for destination '${destination.id}': ${e === null || e === void 0 ? void 0 : e.message}`, e);
|
|
@@ -514,6 +720,18 @@ function processDestinations(destinations, method, event, debug, analyticsInstan
|
|
|
514
720
|
}
|
|
515
721
|
});
|
|
516
722
|
}
|
|
723
|
+
function looksLikeCuid(id) {
|
|
724
|
+
return id.length === 25 && id.charAt(0) === "c";
|
|
725
|
+
}
|
|
726
|
+
function validateWriteKey(writeKey) {
|
|
727
|
+
if (writeKey) {
|
|
728
|
+
const [, secret] = writeKey.split(":", 2);
|
|
729
|
+
if (!secret && !looksLikeCuid(writeKey)) {
|
|
730
|
+
throw new Error(`Legacy write key detected - ${writeKey}! This format doesn't work anymore, it should be 'key:secret'. Please download a new key from Jitsu UI`);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
return writeKey;
|
|
734
|
+
}
|
|
517
735
|
function send(method, payload, jitsuConfig, instance, store) {
|
|
518
736
|
if (jitsuConfig.echoEvents) {
|
|
519
737
|
console.log(`[JITSU] sending '${method}' event:`, payload);
|
|
@@ -524,12 +742,12 @@ function send(method, payload, jitsuConfig, instance, store) {
|
|
|
524
742
|
if (!fetch) {
|
|
525
743
|
throw new Error("Please specify fetch function in jitsu plugin initialization, fetch isn't available in global scope");
|
|
526
744
|
}
|
|
527
|
-
const authHeader = {};
|
|
528
745
|
const debugHeader = jitsuConfig.debug ? { "X-Enable-Debug": "true" } : {};
|
|
529
746
|
// if (jitsuConfig.debug) {
|
|
530
747
|
// console.log(`[JITSU] Sending event to ${url}: `, JSON.stringify(payload, null, 2));
|
|
531
748
|
// }
|
|
532
749
|
const adjustedPayload = adjustPayload(payload, jitsuConfig, store);
|
|
750
|
+
const authHeader = jitsuConfig.writeKey ? { "X-Write-Key": validateWriteKey(jitsuConfig.writeKey) } : {};
|
|
533
751
|
return fetch(url, {
|
|
534
752
|
method: "POST",
|
|
535
753
|
headers: Object.assign(Object.assign({ "Content-Type": "application/json" }, authHeader), debugHeader),
|
|
@@ -556,7 +774,7 @@ function send(method, payload, jitsuConfig, instance, store) {
|
|
|
556
774
|
}
|
|
557
775
|
if (response.destinations) {
|
|
558
776
|
if (jitsuConfig.debug) {
|
|
559
|
-
console.log(`[JITSU] Processing device
|
|
777
|
+
console.log(`[JITSU] Processing device destinations: `, JSON.stringify(response.destinations, null, 2));
|
|
560
778
|
}
|
|
561
779
|
return processDestinations(response.destinations, method, adjustedPayload, !!jitsuConfig.debug, instance);
|
|
562
780
|
}
|
|
@@ -585,9 +803,10 @@ const jitsuAnalyticsPlugin = (pluginConfig = {}) => {
|
|
|
585
803
|
persistentStorage.removeItem(key);
|
|
586
804
|
},
|
|
587
805
|
});
|
|
806
|
+
const instanceConfig = Object.assign(Object.assign({}, defaultConfig), pluginConfig);
|
|
588
807
|
return {
|
|
589
808
|
name: "jitsu",
|
|
590
|
-
config:
|
|
809
|
+
config: instanceConfig,
|
|
591
810
|
initialize: args => {
|
|
592
811
|
const { config } = args;
|
|
593
812
|
if (config.debug) {
|
|
@@ -615,6 +834,16 @@ const jitsuAnalyticsPlugin = (pluginConfig = {}) => {
|
|
|
615
834
|
//clear storage cache
|
|
616
835
|
Object.keys(storageCache).forEach(key => delete storageCache[key]);
|
|
617
836
|
},
|
|
837
|
+
methods: {
|
|
838
|
+
//analytics doesn't support group as a base method, so we need to add it manually
|
|
839
|
+
group(groupId, traits, options, callback) {
|
|
840
|
+
const analyticsInstance = this.instance;
|
|
841
|
+
const user = analyticsInstance.user();
|
|
842
|
+
const userId = (options === null || options === void 0 ? void 0 : options.userId) || (user === null || user === void 0 ? void 0 : user.userId);
|
|
843
|
+
const anonymousId = (options === null || options === void 0 ? void 0 : options.anonymousId) || (user === null || user === void 0 ? void 0 : user.anonymousId);
|
|
844
|
+
return send("group", Object.assign(Object.assign({ type: "group", groupId, traits }, (anonymousId ? { anonymousId } : {})), (userId ? { userId } : {})), instanceConfig, analyticsInstance, cachingStorageWrapper(analyticsInstance.storage));
|
|
845
|
+
},
|
|
846
|
+
},
|
|
618
847
|
};
|
|
619
848
|
};
|
|
620
849
|
function getSeed() {
|
|
@@ -662,6 +891,14 @@ function parse(input) {
|
|
|
662
891
|
}
|
|
663
892
|
return value;
|
|
664
893
|
}
|
|
894
|
+
const emptyAnalytics = {
|
|
895
|
+
track: () => Promise.resolve(),
|
|
896
|
+
page: () => Promise.resolve(),
|
|
897
|
+
user: () => ({}),
|
|
898
|
+
identify: () => Promise.resolve({}),
|
|
899
|
+
group: () => Promise.resolve({}),
|
|
900
|
+
reset: () => Promise.resolve({}),
|
|
901
|
+
};
|
|
665
902
|
function createUnderlyingAnalyticsInstance(opts, rt, plugins = []) {
|
|
666
903
|
const analytics = analyticsLib({
|
|
667
904
|
app: "test",
|
|
@@ -678,7 +915,14 @@ function createUnderlyingAnalyticsInstance(opts, rt, plugins = []) {
|
|
|
678
915
|
return originalPage(...args);
|
|
679
916
|
}
|
|
680
917
|
};
|
|
681
|
-
return analytics
|
|
918
|
+
return Object.assign(Object.assign({}, analytics), { group(groupId, traits, options, callback) {
|
|
919
|
+
for (const plugin of Object.values(analytics.plugins)) {
|
|
920
|
+
if (plugin["group"]) {
|
|
921
|
+
plugin["group"](groupId, traits, options, callback);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
return Promise.resolve({});
|
|
925
|
+
} });
|
|
682
926
|
}
|
|
683
927
|
function jitsuAnalytics(opts) {
|
|
684
928
|
const inBrowser = isInBrowser();
|
|
@@ -708,4 +952,4 @@ function jitsuAnalytics(opts) {
|
|
|
708
952
|
// }
|
|
709
953
|
}
|
|
710
954
|
|
|
711
|
-
export { emptyRuntime, getTopLevelDomain, isInBrowser, jitsuAnalytics, parseQuery, randomId, windowRuntime };
|
|
955
|
+
export { emptyAnalytics, emptyRuntime, getTopLevelDomain, isInBrowser, jitsuAnalytics, parseQuery, randomId, windowRuntime };
|