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