@jitsu/js 1.1.0 → 1.1.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/.turbo/turbo-build.log +65 -65
- package/.turbo/turbo-clean.log +5 -5
- package/__tests__/node/nodejs.test.ts +15 -4
- package/__tests__/playwright/integration.test.ts +1 -2
- package/dist/analytics-plugin.d.ts +1 -0
- package/dist/destination-plugins.d.ts +4 -0
- package/dist/index.d.ts +1 -0
- package/dist/jitsu.cjs.js +157 -54
- package/dist/jitsu.es.js +157 -54
- package/dist/version.d.ts +2 -0
- package/dist/web/p.js.txt +156 -54
- package/package.json +3 -3
- package/src/analytics-plugin.ts +50 -17
- 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 +13 -3
- package/src/version.ts +3 -0
- package/tsconfig.json +1 -0
package/dist/jitsu.es.js
CHANGED
|
@@ -65,6 +65,49 @@ function analyticsLib() {
|
|
|
65
65
|
return ke(_objectSpread2(_objectSpread2({}, defaultSettings), opts));
|
|
66
66
|
}
|
|
67
67
|
|
|
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
|
+
|
|
68
111
|
var __awaiter$1 = (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) {
|
|
@@ -93,6 +136,76 @@ const tagPlugin = {
|
|
|
93
136
|
});
|
|
94
137
|
},
|
|
95
138
|
};
|
|
139
|
+
function getLogRocketState() {
|
|
140
|
+
return window["__jitsuLrState"] || "fresh";
|
|
141
|
+
}
|
|
142
|
+
function setLogRocketState(s) {
|
|
143
|
+
window["__jitsuLrState"] = s;
|
|
144
|
+
}
|
|
145
|
+
function getLogRocketQueue() {
|
|
146
|
+
return window["__jitsuLrQueue"] || (window["__jitsuLrQueue"] = []);
|
|
147
|
+
}
|
|
148
|
+
function flushLogRocketQueue(lr) {
|
|
149
|
+
const queue = getLogRocketQueue();
|
|
150
|
+
while (queue.length > 0) {
|
|
151
|
+
const method = queue.shift();
|
|
152
|
+
try {
|
|
153
|
+
const res = method(lr);
|
|
154
|
+
if (res) {
|
|
155
|
+
res.catch(e => console.warn(`Async LogRocket method failed: ${e.message}`, e));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
console.warn(`LogRocket method failed: ${e.message}`, e);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function initLogrocketIfNeeded(appId) {
|
|
164
|
+
return __awaiter$1(this, void 0, void 0, function* () {
|
|
165
|
+
if (getLogRocketState() !== "fresh") {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
setLogRocketState("loading");
|
|
169
|
+
loadScript(`https://cdn.lr-ingest.io/LogRocket.min.js`, { crossOrigin: "anonymous" })
|
|
170
|
+
.then(() => {
|
|
171
|
+
if (window["LogRocket"]) {
|
|
172
|
+
try {
|
|
173
|
+
window["LogRocket"].init(appId);
|
|
174
|
+
}
|
|
175
|
+
catch (e) {
|
|
176
|
+
console.warn(`LogRocket (id=${appId}) init failed: ${e.message}`, e);
|
|
177
|
+
setLogRocketState("failed");
|
|
178
|
+
}
|
|
179
|
+
setLogRocketState("loaded");
|
|
180
|
+
flushLogRocketQueue(window["LogRocket"]);
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
.catch(e => {
|
|
184
|
+
console.warn(`LogRocket (id=${appId}) init failed: ${e.message}`, e);
|
|
185
|
+
setLogRocketState("failed");
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
const logrocketPlugin = {
|
|
190
|
+
id: "logrocket",
|
|
191
|
+
handle(config, payload) {
|
|
192
|
+
return __awaiter$1(this, void 0, void 0, function* () {
|
|
193
|
+
if (!applyFilters(payload, config)) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
initLogrocketIfNeeded(config.appId);
|
|
197
|
+
const action = logRocket => {
|
|
198
|
+
if (payload.type === "identify" && payload.userId) {
|
|
199
|
+
logRocket.identify(payload.userId, payload.traits || {});
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
getLogRocketQueue().push(action);
|
|
203
|
+
if (getLogRocketState() === "loaded") {
|
|
204
|
+
flushLogRocketQueue(window["LogRocket"]);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
},
|
|
208
|
+
};
|
|
96
209
|
function insertTags(code, event, opts = {}) {
|
|
97
210
|
let tag;
|
|
98
211
|
try {
|
|
@@ -155,50 +268,11 @@ function replaceMacro(code, event) {
|
|
|
155
268
|
}
|
|
156
269
|
const internalDestinationPlugins = {
|
|
157
270
|
[tagPlugin.id]: tagPlugin,
|
|
271
|
+
[logrocketPlugin.id]: logrocketPlugin,
|
|
158
272
|
};
|
|
159
273
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return scripts.find(s => s.src === src);
|
|
163
|
-
}
|
|
164
|
-
function loadScript(src, attributes) {
|
|
165
|
-
const found = findScript(src);
|
|
166
|
-
if (found !== undefined) {
|
|
167
|
-
const status = found === null || found === void 0 ? void 0 : found.getAttribute("status");
|
|
168
|
-
if (status === "loaded") {
|
|
169
|
-
return Promise.resolve(found);
|
|
170
|
-
}
|
|
171
|
-
if (status === "loading") {
|
|
172
|
-
return new Promise((resolve, reject) => {
|
|
173
|
-
found.addEventListener("load", () => resolve(found));
|
|
174
|
-
found.addEventListener("error", err => reject(err));
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return new Promise((resolve, reject) => {
|
|
179
|
-
var _a;
|
|
180
|
-
const script = window.document.createElement("script");
|
|
181
|
-
script.type = "text/javascript";
|
|
182
|
-
script.src = src;
|
|
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);
|
|
187
|
-
}
|
|
188
|
-
script.onload = () => {
|
|
189
|
-
script.onerror = script.onload = null;
|
|
190
|
-
script.setAttribute("status", "loaded");
|
|
191
|
-
resolve(script);
|
|
192
|
-
};
|
|
193
|
-
script.onerror = () => {
|
|
194
|
-
script.onerror = script.onload = null;
|
|
195
|
-
script.setAttribute("status", "error");
|
|
196
|
-
reject(new Error(`Failed to load ${src}`));
|
|
197
|
-
};
|
|
198
|
-
const tag = window.document.getElementsByTagName("script")[0];
|
|
199
|
-
(_a = tag.parentElement) === null || _a === void 0 ? void 0 : _a.insertBefore(script, tag);
|
|
200
|
-
});
|
|
201
|
-
}
|
|
274
|
+
var name = "@jitsu/js";
|
|
275
|
+
var version = "0.0.0";
|
|
202
276
|
|
|
203
277
|
/* global analytics */
|
|
204
278
|
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
@@ -218,6 +292,8 @@ const config = {
|
|
|
218
292
|
debug: false,
|
|
219
293
|
fetch: null,
|
|
220
294
|
echoEvents: false,
|
|
295
|
+
cookieDomain: undefined,
|
|
296
|
+
runtime: undefined,
|
|
221
297
|
};
|
|
222
298
|
const parseQuery = (qs) => {
|
|
223
299
|
if (!qs) {
|
|
@@ -435,13 +511,13 @@ function adjustPayload(payload, config, storage) {
|
|
|
435
511
|
const referrer = runtime.referrer();
|
|
436
512
|
const context = {
|
|
437
513
|
library: {
|
|
438
|
-
name:
|
|
439
|
-
version:
|
|
514
|
+
name: name,
|
|
515
|
+
version: version,
|
|
440
516
|
},
|
|
441
517
|
userAgent: runtime.userAgent(),
|
|
442
518
|
locale: runtime.language(),
|
|
443
519
|
screen: runtime.screen(),
|
|
444
|
-
traits: payload.type != "identify" ? Object.assign({}, (restoreTraits(storage) || {})) : undefined,
|
|
520
|
+
traits: payload.type != "identify" && payload.type != "group" ? Object.assign({}, (restoreTraits(storage) || {})) : undefined,
|
|
445
521
|
page: {
|
|
446
522
|
path: properties.path || (parsedUrl && parsedUrl.pathname),
|
|
447
523
|
referrer: referrer,
|
|
@@ -450,11 +526,11 @@ function adjustPayload(payload, config, storage) {
|
|
|
450
526
|
search: properties.search || (parsedUrl && parsedUrl.search),
|
|
451
527
|
title: properties.title || runtime.pageTitle(),
|
|
452
528
|
url: properties.url || url,
|
|
453
|
-
|
|
529
|
+
encoding: properties.encoding || runtime.documentEncoding(),
|
|
454
530
|
},
|
|
455
531
|
campaign: parseUtms(query),
|
|
456
532
|
};
|
|
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) });
|
|
533
|
+
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
534
|
delete withContext.meta;
|
|
459
535
|
delete withContext.options;
|
|
460
536
|
return withContext;
|
|
@@ -463,12 +539,11 @@ function processDestinations(destinations, method, event, debug, analyticsInstan
|
|
|
463
539
|
return __awaiter(this, void 0, void 0, function* () {
|
|
464
540
|
const promises = [];
|
|
465
541
|
for (const destination of destinations) {
|
|
542
|
+
const credentials = Object.assign(Object.assign({}, destination.credentials), destination.options);
|
|
466
543
|
if (destination.deviceOptions.type === "internal-plugin") {
|
|
467
544
|
const plugin = internalDestinationPlugins[destination.deviceOptions.name];
|
|
468
545
|
if (plugin) {
|
|
469
546
|
try {
|
|
470
|
-
//to support old versions, where credentials were stored in root
|
|
471
|
-
const credentials = destination.credentials || destination;
|
|
472
547
|
promises.push(plugin.handle(credentials, event));
|
|
473
548
|
}
|
|
474
549
|
catch (e) {
|
|
@@ -488,7 +563,7 @@ function processDestinations(destinations, method, event, debug, analyticsInstan
|
|
|
488
563
|
else {
|
|
489
564
|
let pluginInstance;
|
|
490
565
|
try {
|
|
491
|
-
pluginInstance = (typeof plugin === "function" ? plugin : plugin.init)(
|
|
566
|
+
pluginInstance = (typeof plugin === "function" ? plugin : plugin.init)(credentials);
|
|
492
567
|
}
|
|
493
568
|
catch (e) {
|
|
494
569
|
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 +591,15 @@ function processDestinations(destinations, method, event, debug, analyticsInstan
|
|
|
516
591
|
}
|
|
517
592
|
});
|
|
518
593
|
}
|
|
594
|
+
function validateWriteKey(writeKey) {
|
|
595
|
+
if (writeKey) {
|
|
596
|
+
const [, secret] = writeKey.split(":", 2);
|
|
597
|
+
if (!secret) {
|
|
598
|
+
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`);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
return writeKey;
|
|
602
|
+
}
|
|
519
603
|
function send(method, payload, jitsuConfig, instance, store) {
|
|
520
604
|
if (jitsuConfig.echoEvents) {
|
|
521
605
|
console.log(`[JITSU] sending '${method}' event:`, payload);
|
|
@@ -531,7 +615,7 @@ function send(method, payload, jitsuConfig, instance, store) {
|
|
|
531
615
|
// console.log(`[JITSU] Sending event to ${url}: `, JSON.stringify(payload, null, 2));
|
|
532
616
|
// }
|
|
533
617
|
const adjustedPayload = adjustPayload(payload, jitsuConfig, store);
|
|
534
|
-
const authHeader =
|
|
618
|
+
const authHeader = config.writeKey ? { "X-Write-Key": validateWriteKey(config.writeKey) } : {};
|
|
535
619
|
return fetch(url, {
|
|
536
620
|
method: "POST",
|
|
537
621
|
headers: Object.assign(Object.assign({ "Content-Type": "application/json" }, authHeader), debugHeader),
|
|
@@ -558,7 +642,7 @@ function send(method, payload, jitsuConfig, instance, store) {
|
|
|
558
642
|
}
|
|
559
643
|
if (response.destinations) {
|
|
560
644
|
if (jitsuConfig.debug) {
|
|
561
|
-
console.log(`[JITSU] Processing device
|
|
645
|
+
console.log(`[JITSU] Processing device destinations: `, JSON.stringify(response.destinations, null, 2));
|
|
562
646
|
}
|
|
563
647
|
return processDestinations(response.destinations, method, adjustedPayload, !!jitsuConfig.debug, instance);
|
|
564
648
|
}
|
|
@@ -587,9 +671,10 @@ const jitsuAnalyticsPlugin = (pluginConfig = {}) => {
|
|
|
587
671
|
persistentStorage.removeItem(key);
|
|
588
672
|
},
|
|
589
673
|
});
|
|
674
|
+
const instanceConfig = Object.assign(Object.assign({}, config), pluginConfig);
|
|
590
675
|
return {
|
|
591
676
|
name: "jitsu",
|
|
592
|
-
config:
|
|
677
|
+
config: instanceConfig,
|
|
593
678
|
initialize: args => {
|
|
594
679
|
const { config } = args;
|
|
595
680
|
if (config.debug) {
|
|
@@ -617,6 +702,16 @@ const jitsuAnalyticsPlugin = (pluginConfig = {}) => {
|
|
|
617
702
|
//clear storage cache
|
|
618
703
|
Object.keys(storageCache).forEach(key => delete storageCache[key]);
|
|
619
704
|
},
|
|
705
|
+
methods: {
|
|
706
|
+
//analytics doesn't support group as a base method, so we need to add it manually
|
|
707
|
+
group(groupId, traits, options, callback) {
|
|
708
|
+
const analyticsInstance = this.instance;
|
|
709
|
+
const user = analyticsInstance.user();
|
|
710
|
+
const userId = (options === null || options === void 0 ? void 0 : options.userId) || (user === null || user === void 0 ? void 0 : user.userId);
|
|
711
|
+
const anonymousId = (options === null || options === void 0 ? void 0 : options.anonymousId) || (user === null || user === void 0 ? void 0 : user.anonymousId);
|
|
712
|
+
return send("group", Object.assign(Object.assign({ type: "group", groupId, traits }, (anonymousId ? { anonymousId } : {})), (userId ? { userId } : {})), instanceConfig, analyticsInstance, cachingStorageWrapper(analyticsInstance.storage));
|
|
713
|
+
},
|
|
714
|
+
},
|
|
620
715
|
};
|
|
621
716
|
};
|
|
622
717
|
function getSeed() {
|
|
@@ -669,6 +764,7 @@ const emptyAnalytics = {
|
|
|
669
764
|
page: () => Promise.resolve(),
|
|
670
765
|
user: () => ({}),
|
|
671
766
|
identify: () => Promise.resolve({}),
|
|
767
|
+
group: () => Promise.resolve({}),
|
|
672
768
|
reset: () => Promise.resolve({}),
|
|
673
769
|
};
|
|
674
770
|
function createUnderlyingAnalyticsInstance(opts, rt, plugins = []) {
|
|
@@ -687,7 +783,14 @@ function createUnderlyingAnalyticsInstance(opts, rt, plugins = []) {
|
|
|
687
783
|
return originalPage(...args);
|
|
688
784
|
}
|
|
689
785
|
};
|
|
690
|
-
return analytics
|
|
786
|
+
return Object.assign(Object.assign({}, analytics), { group(groupId, traits, options, callback) {
|
|
787
|
+
for (const plugin of Object.values(analytics.plugins)) {
|
|
788
|
+
if (plugin["group"]) {
|
|
789
|
+
plugin["group"](groupId, traits, options, callback);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return Promise.resolve({});
|
|
793
|
+
} });
|
|
691
794
|
}
|
|
692
795
|
function jitsuAnalytics(opts) {
|
|
693
796
|
const inBrowser = isInBrowser();
|
package/dist/web/p.js.txt
CHANGED
|
@@ -68,6 +68,49 @@
|
|
|
68
68
|
return ke(_objectSpread2(_objectSpread2({}, defaultSettings), opts));
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
function findScript(src) {
|
|
72
|
+
const scripts = Array.prototype.slice.call(window.document.querySelectorAll("script"));
|
|
73
|
+
return scripts.find(s => s.src === src);
|
|
74
|
+
}
|
|
75
|
+
function loadScript(src, attributes) {
|
|
76
|
+
const found = findScript(src);
|
|
77
|
+
if (found !== undefined) {
|
|
78
|
+
const status = found === null || found === void 0 ? void 0 : found.getAttribute("status");
|
|
79
|
+
if (status === "loaded") {
|
|
80
|
+
return Promise.resolve(found);
|
|
81
|
+
}
|
|
82
|
+
if (status === "loading") {
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
found.addEventListener("load", () => resolve(found));
|
|
85
|
+
found.addEventListener("error", err => reject(err));
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return new Promise((resolve, reject) => {
|
|
90
|
+
var _a;
|
|
91
|
+
const script = window.document.createElement("script");
|
|
92
|
+
script.type = "text/javascript";
|
|
93
|
+
script.src = src;
|
|
94
|
+
script.async = true;
|
|
95
|
+
script.setAttribute("status", "loading");
|
|
96
|
+
for (const [k, v] of Object.entries(attributes !== null && attributes !== void 0 ? attributes : {})) {
|
|
97
|
+
script.setAttribute(k, v);
|
|
98
|
+
}
|
|
99
|
+
script.onload = () => {
|
|
100
|
+
script.onerror = script.onload = null;
|
|
101
|
+
script.setAttribute("status", "loaded");
|
|
102
|
+
resolve(script);
|
|
103
|
+
};
|
|
104
|
+
script.onerror = () => {
|
|
105
|
+
script.onerror = script.onload = null;
|
|
106
|
+
script.setAttribute("status", "error");
|
|
107
|
+
reject(new Error(`Failed to load ${src}`));
|
|
108
|
+
};
|
|
109
|
+
const tag = window.document.getElementsByTagName("script")[0];
|
|
110
|
+
(_a = tag.parentElement) === null || _a === void 0 ? void 0 : _a.insertBefore(script, tag);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
71
114
|
var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
72
115
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
73
116
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -96,6 +139,76 @@
|
|
|
96
139
|
});
|
|
97
140
|
},
|
|
98
141
|
};
|
|
142
|
+
function getLogRocketState() {
|
|
143
|
+
return window["__jitsuLrState"] || "fresh";
|
|
144
|
+
}
|
|
145
|
+
function setLogRocketState(s) {
|
|
146
|
+
window["__jitsuLrState"] = s;
|
|
147
|
+
}
|
|
148
|
+
function getLogRocketQueue() {
|
|
149
|
+
return window["__jitsuLrQueue"] || (window["__jitsuLrQueue"] = []);
|
|
150
|
+
}
|
|
151
|
+
function flushLogRocketQueue(lr) {
|
|
152
|
+
const queue = getLogRocketQueue();
|
|
153
|
+
while (queue.length > 0) {
|
|
154
|
+
const method = queue.shift();
|
|
155
|
+
try {
|
|
156
|
+
const res = method(lr);
|
|
157
|
+
if (res) {
|
|
158
|
+
res.catch(e => console.warn(`Async LogRocket method failed: ${e.message}`, e));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
console.warn(`LogRocket method failed: ${e.message}`, e);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function initLogrocketIfNeeded(appId) {
|
|
167
|
+
return __awaiter$1(this, void 0, void 0, function* () {
|
|
168
|
+
if (getLogRocketState() !== "fresh") {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
setLogRocketState("loading");
|
|
172
|
+
loadScript(`https://cdn.lr-ingest.io/LogRocket.min.js`, { crossOrigin: "anonymous" })
|
|
173
|
+
.then(() => {
|
|
174
|
+
if (window["LogRocket"]) {
|
|
175
|
+
try {
|
|
176
|
+
window["LogRocket"].init(appId);
|
|
177
|
+
}
|
|
178
|
+
catch (e) {
|
|
179
|
+
console.warn(`LogRocket (id=${appId}) init failed: ${e.message}`, e);
|
|
180
|
+
setLogRocketState("failed");
|
|
181
|
+
}
|
|
182
|
+
setLogRocketState("loaded");
|
|
183
|
+
flushLogRocketQueue(window["LogRocket"]);
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
.catch(e => {
|
|
187
|
+
console.warn(`LogRocket (id=${appId}) init failed: ${e.message}`, e);
|
|
188
|
+
setLogRocketState("failed");
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
const logrocketPlugin = {
|
|
193
|
+
id: "logrocket",
|
|
194
|
+
handle(config, payload) {
|
|
195
|
+
return __awaiter$1(this, void 0, void 0, function* () {
|
|
196
|
+
if (!applyFilters(payload, config)) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
initLogrocketIfNeeded(config.appId);
|
|
200
|
+
const action = logRocket => {
|
|
201
|
+
if (payload.type === "identify" && payload.userId) {
|
|
202
|
+
logRocket.identify(payload.userId, payload.traits || {});
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
getLogRocketQueue().push(action);
|
|
206
|
+
if (getLogRocketState() === "loaded") {
|
|
207
|
+
flushLogRocketQueue(window["LogRocket"]);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
};
|
|
99
212
|
function insertTags(code, event, opts = {}) {
|
|
100
213
|
let tag;
|
|
101
214
|
try {
|
|
@@ -158,50 +271,11 @@
|
|
|
158
271
|
}
|
|
159
272
|
const internalDestinationPlugins = {
|
|
160
273
|
[tagPlugin.id]: tagPlugin,
|
|
274
|
+
[logrocketPlugin.id]: logrocketPlugin,
|
|
161
275
|
};
|
|
162
276
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
return scripts.find(s => s.src === src);
|
|
166
|
-
}
|
|
167
|
-
function loadScript(src, attributes) {
|
|
168
|
-
const found = findScript(src);
|
|
169
|
-
if (found !== undefined) {
|
|
170
|
-
const status = found === null || found === void 0 ? void 0 : found.getAttribute("status");
|
|
171
|
-
if (status === "loaded") {
|
|
172
|
-
return Promise.resolve(found);
|
|
173
|
-
}
|
|
174
|
-
if (status === "loading") {
|
|
175
|
-
return new Promise((resolve, reject) => {
|
|
176
|
-
found.addEventListener("load", () => resolve(found));
|
|
177
|
-
found.addEventListener("error", err => reject(err));
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
return new Promise((resolve, reject) => {
|
|
182
|
-
var _a;
|
|
183
|
-
const script = window.document.createElement("script");
|
|
184
|
-
script.type = "text/javascript";
|
|
185
|
-
script.src = src;
|
|
186
|
-
script.async = true;
|
|
187
|
-
script.setAttribute("status", "loading");
|
|
188
|
-
for (const [k, v] of Object.entries(attributes !== null && attributes !== void 0 ? attributes : {})) {
|
|
189
|
-
script.setAttribute(k, v);
|
|
190
|
-
}
|
|
191
|
-
script.onload = () => {
|
|
192
|
-
script.onerror = script.onload = null;
|
|
193
|
-
script.setAttribute("status", "loaded");
|
|
194
|
-
resolve(script);
|
|
195
|
-
};
|
|
196
|
-
script.onerror = () => {
|
|
197
|
-
script.onerror = script.onload = null;
|
|
198
|
-
script.setAttribute("status", "error");
|
|
199
|
-
reject(new Error(`Failed to load ${src}`));
|
|
200
|
-
};
|
|
201
|
-
const tag = window.document.getElementsByTagName("script")[0];
|
|
202
|
-
(_a = tag.parentElement) === null || _a === void 0 ? void 0 : _a.insertBefore(script, tag);
|
|
203
|
-
});
|
|
204
|
-
}
|
|
277
|
+
var name = "@jitsu/js";
|
|
278
|
+
var version = "0.0.0";
|
|
205
279
|
|
|
206
280
|
/* global analytics */
|
|
207
281
|
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
@@ -221,6 +295,8 @@
|
|
|
221
295
|
debug: false,
|
|
222
296
|
fetch: null,
|
|
223
297
|
echoEvents: false,
|
|
298
|
+
cookieDomain: undefined,
|
|
299
|
+
runtime: undefined,
|
|
224
300
|
};
|
|
225
301
|
const parseQuery = (qs) => {
|
|
226
302
|
if (!qs) {
|
|
@@ -438,13 +514,13 @@
|
|
|
438
514
|
const referrer = runtime.referrer();
|
|
439
515
|
const context = {
|
|
440
516
|
library: {
|
|
441
|
-
name:
|
|
442
|
-
version:
|
|
517
|
+
name: name,
|
|
518
|
+
version: version,
|
|
443
519
|
},
|
|
444
520
|
userAgent: runtime.userAgent(),
|
|
445
521
|
locale: runtime.language(),
|
|
446
522
|
screen: runtime.screen(),
|
|
447
|
-
traits: payload.type != "identify" ? Object.assign({}, (restoreTraits(storage) || {})) : undefined,
|
|
523
|
+
traits: payload.type != "identify" && payload.type != "group" ? Object.assign({}, (restoreTraits(storage) || {})) : undefined,
|
|
448
524
|
page: {
|
|
449
525
|
path: properties.path || (parsedUrl && parsedUrl.pathname),
|
|
450
526
|
referrer: referrer,
|
|
@@ -453,11 +529,11 @@
|
|
|
453
529
|
search: properties.search || (parsedUrl && parsedUrl.search),
|
|
454
530
|
title: properties.title || runtime.pageTitle(),
|
|
455
531
|
url: properties.url || url,
|
|
456
|
-
|
|
532
|
+
encoding: properties.encoding || runtime.documentEncoding(),
|
|
457
533
|
},
|
|
458
534
|
campaign: parseUtms(query),
|
|
459
535
|
};
|
|
460
|
-
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) });
|
|
536
|
+
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) });
|
|
461
537
|
delete withContext.meta;
|
|
462
538
|
delete withContext.options;
|
|
463
539
|
return withContext;
|
|
@@ -466,12 +542,11 @@
|
|
|
466
542
|
return __awaiter(this, void 0, void 0, function* () {
|
|
467
543
|
const promises = [];
|
|
468
544
|
for (const destination of destinations) {
|
|
545
|
+
const credentials = Object.assign(Object.assign({}, destination.credentials), destination.options);
|
|
469
546
|
if (destination.deviceOptions.type === "internal-plugin") {
|
|
470
547
|
const plugin = internalDestinationPlugins[destination.deviceOptions.name];
|
|
471
548
|
if (plugin) {
|
|
472
549
|
try {
|
|
473
|
-
//to support old versions, where credentials were stored in root
|
|
474
|
-
const credentials = destination.credentials || destination;
|
|
475
550
|
promises.push(plugin.handle(credentials, event));
|
|
476
551
|
}
|
|
477
552
|
catch (e) {
|
|
@@ -491,7 +566,7 @@
|
|
|
491
566
|
else {
|
|
492
567
|
let pluginInstance;
|
|
493
568
|
try {
|
|
494
|
-
pluginInstance = (typeof plugin === "function" ? plugin : plugin.init)(
|
|
569
|
+
pluginInstance = (typeof plugin === "function" ? plugin : plugin.init)(credentials);
|
|
495
570
|
}
|
|
496
571
|
catch (e) {
|
|
497
572
|
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);
|
|
@@ -519,6 +594,15 @@
|
|
|
519
594
|
}
|
|
520
595
|
});
|
|
521
596
|
}
|
|
597
|
+
function validateWriteKey(writeKey) {
|
|
598
|
+
if (writeKey) {
|
|
599
|
+
const [, secret] = writeKey.split(":", 2);
|
|
600
|
+
if (!secret) {
|
|
601
|
+
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`);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return writeKey;
|
|
605
|
+
}
|
|
522
606
|
function send(method, payload, jitsuConfig, instance, store) {
|
|
523
607
|
if (jitsuConfig.echoEvents) {
|
|
524
608
|
console.log(`[JITSU] sending '${method}' event:`, payload);
|
|
@@ -534,7 +618,7 @@
|
|
|
534
618
|
// console.log(`[JITSU] Sending event to ${url}: `, JSON.stringify(payload, null, 2));
|
|
535
619
|
// }
|
|
536
620
|
const adjustedPayload = adjustPayload(payload, jitsuConfig, store);
|
|
537
|
-
const authHeader =
|
|
621
|
+
const authHeader = config.writeKey ? { "X-Write-Key": validateWriteKey(config.writeKey) } : {};
|
|
538
622
|
return fetch(url, {
|
|
539
623
|
method: "POST",
|
|
540
624
|
headers: Object.assign(Object.assign({ "Content-Type": "application/json" }, authHeader), debugHeader),
|
|
@@ -561,7 +645,7 @@
|
|
|
561
645
|
}
|
|
562
646
|
if (response.destinations) {
|
|
563
647
|
if (jitsuConfig.debug) {
|
|
564
|
-
console.log(`[JITSU] Processing device
|
|
648
|
+
console.log(`[JITSU] Processing device destinations: `, JSON.stringify(response.destinations, null, 2));
|
|
565
649
|
}
|
|
566
650
|
return processDestinations(response.destinations, method, adjustedPayload, !!jitsuConfig.debug, instance);
|
|
567
651
|
}
|
|
@@ -590,9 +674,10 @@
|
|
|
590
674
|
persistentStorage.removeItem(key);
|
|
591
675
|
},
|
|
592
676
|
});
|
|
677
|
+
const instanceConfig = Object.assign(Object.assign({}, config), pluginConfig);
|
|
593
678
|
return {
|
|
594
679
|
name: "jitsu",
|
|
595
|
-
config:
|
|
680
|
+
config: instanceConfig,
|
|
596
681
|
initialize: args => {
|
|
597
682
|
const { config } = args;
|
|
598
683
|
if (config.debug) {
|
|
@@ -620,6 +705,16 @@
|
|
|
620
705
|
//clear storage cache
|
|
621
706
|
Object.keys(storageCache).forEach(key => delete storageCache[key]);
|
|
622
707
|
},
|
|
708
|
+
methods: {
|
|
709
|
+
//analytics doesn't support group as a base method, so we need to add it manually
|
|
710
|
+
group(groupId, traits, options, callback) {
|
|
711
|
+
const analyticsInstance = this.instance;
|
|
712
|
+
const user = analyticsInstance.user();
|
|
713
|
+
const userId = (options === null || options === void 0 ? void 0 : options.userId) || (user === null || user === void 0 ? void 0 : user.userId);
|
|
714
|
+
const anonymousId = (options === null || options === void 0 ? void 0 : options.anonymousId) || (user === null || user === void 0 ? void 0 : user.anonymousId);
|
|
715
|
+
return send("group", Object.assign(Object.assign({ type: "group", groupId, traits }, (anonymousId ? { anonymousId } : {})), (userId ? { userId } : {})), instanceConfig, analyticsInstance, cachingStorageWrapper(analyticsInstance.storage));
|
|
716
|
+
},
|
|
717
|
+
},
|
|
623
718
|
};
|
|
624
719
|
};
|
|
625
720
|
function getSeed() {
|
|
@@ -683,7 +778,14 @@
|
|
|
683
778
|
return originalPage(...args);
|
|
684
779
|
}
|
|
685
780
|
};
|
|
686
|
-
return analytics
|
|
781
|
+
return Object.assign(Object.assign({}, analytics), { group(groupId, traits, options, callback) {
|
|
782
|
+
for (const plugin of Object.values(analytics.plugins)) {
|
|
783
|
+
if (plugin["group"]) {
|
|
784
|
+
plugin["group"](groupId, traits, options, callback);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
return Promise.resolve({});
|
|
788
|
+
} });
|
|
687
789
|
}
|
|
688
790
|
function jitsuAnalytics(opts) {
|
|
689
791
|
const inBrowser = isInBrowser();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jitsu/js",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "Jitsu Dev Team <dev@jitsu.com>",
|
|
6
6
|
"main": "dist/jitsu.cjs.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"private": false,
|
|
14
14
|
"devDependencies": {
|
|
15
|
-
"@playwright/test": "1.
|
|
15
|
+
"@playwright/test": "1.30.0",
|
|
16
16
|
"@rollup/plugin-commonjs": "^23.0.2",
|
|
17
17
|
"@rollup/plugin-json": "^5.0.1",
|
|
18
18
|
"@rollup/plugin-multi-entry": "^6.0.0",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"rollup": "^3.2.5",
|
|
35
35
|
"ts-jest": "29.0.5",
|
|
36
36
|
"typescript": "^4.9.5",
|
|
37
|
-
"@jitsu/protocols": "1.1.
|
|
37
|
+
"@jitsu/protocols": "1.1.1"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"analytics": "^0.8.1"
|