@jitsu/js 1.1.0-canary.391.20230523080206 → 1.1.0-canary.404.20230526224724

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.
@@ -1,95 +1,65 @@
1
- @jitsu/js:build: cache hit, replaying output 70799201810b0c5a
2
- @jitsu/js:build: 
3
- @jitsu/js:build: > @jitsu/js@0.0.0 build /Users/ildarnurislamov/Projects/onetag/libs/jitsu-js
4
- @jitsu/js:build: > tsc -p . && rollup -c && cp compiled/src/*.d.ts dist
5
- @jitsu/js:build: 
6
- @jitsu/js:build: 
7
- @jitsu/js:build: ./compiled/src/browser.js → dist/web/p.js.txt...
8
- @jitsu/js:build: (!) "this" has been rewritten to "undefined"
9
- @jitsu/js:build: https://rollupjs.org/troubleshooting/#error-this-is-undefined
10
- @jitsu/js:build: compiled/src/analytics-plugin.js
11
- @jitsu/js:build: 1: /* global analytics */
12
- @jitsu/js:build: 2: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
- @jitsu/js:build:  ^
14
- @jitsu/js:build: 3: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- @jitsu/js:build: 4: return new (P || (P = Promise))(function (resolve, reject) {
16
- @jitsu/js:build: ...and 1 other occurrence
17
- @jitsu/js:build: compiled/src/destination-plugins/tag.js
18
- @jitsu/js:build: 1: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
19
- @jitsu/js:build:  ^
20
- @jitsu/js:build: 2: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
21
- @jitsu/js:build: 3: return new (P || (P = Promise))(function (resolve, reject) {
22
- @jitsu/js:build: ...and 1 other occurrence
23
- @jitsu/js:build: compiled/src/destination-plugins/gtm.js
24
- @jitsu/js:build: 1: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
25
- @jitsu/js:build:  ^
26
- @jitsu/js:build: 2: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- @jitsu/js:build: 3: return new (P || (P = Promise))(function (resolve, reject) {
28
- @jitsu/js:build: ...and 1 other occurrence
29
- @jitsu/js:build: compiled/src/destination-plugins/logrocket.js
30
- @jitsu/js:build: 1: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
31
- @jitsu/js:build:  ^
32
- @jitsu/js:build: 2: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
33
- @jitsu/js:build: 3: return new (P || (P = Promise))(function (resolve, reject) {
34
- @jitsu/js:build: ...and 1 other occurrence
35
- @jitsu/js:build: (!) Circular dependencies
36
- @jitsu/js:build: compiled/src/index.js -> compiled/src/analytics-plugin.js -> compiled/src/index.js
37
- @jitsu/js:build: compiled/src/destination-plugins/index.js -> compiled/src/destination-plugins/tag.js -> compiled/src/destination-plugins/index.js
38
- @jitsu/js:build: compiled/src/analytics-plugin.js -> compiled/src/destination-plugins/index.js -> compiled/src/destination-plugins/tag.js -> compiled/src/analytics-plugin.js
39
- @jitsu/js:build: compiled/src/destination-plugins/index.js -> compiled/src/destination-plugins/logrocket.js -> compiled/src/destination-plugins/index.js
40
- @jitsu/js:build: compiled/src/destination-plugins/index.js -> compiled/src/destination-plugins/gtm.js -> compiled/src/destination-plugins/index.js
41
- @jitsu/js:build: (!) Use of eval is strongly discouraged
42
- @jitsu/js:build: https://rollupjs.org/troubleshooting/#avoiding-eval
43
- @jitsu/js:build: compiled/src/destination-plugins/tag.js
44
- @jitsu/js:build: 68: })()`;
45
- @jitsu/js:build: 69: try {
46
- @jitsu/js:build: 70: eval(iif);
47
- @jitsu/js:build:  ^
48
- @jitsu/js:build: 71: }
49
- @jitsu/js:build: 72: catch (e) {
50
- @jitsu/js:build: created dist/web/p.js.txt in 512ms
51
- @jitsu/js:build: 
52
- @jitsu/js:build: ./compiled/src/index.js, ./compiled/src/jitsu.js, ./compiled/src/analytics-plugin.js → dist/jitsu.es.js, dist/jitsu.cjs.js...
53
- @jitsu/js:build: (!) "this" has been rewritten to "undefined"
54
- @jitsu/js:build: https://rollupjs.org/troubleshooting/#error-this-is-undefined
55
- @jitsu/js:build: compiled/src/analytics-plugin.js
56
- @jitsu/js:build: 1: /* global analytics */
57
- @jitsu/js:build: 2: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
58
- @jitsu/js:build:  ^
59
- @jitsu/js:build: 3: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
60
- @jitsu/js:build: 4: return new (P || (P = Promise))(function (resolve, reject) {
61
- @jitsu/js:build: ...and 1 other occurrence
62
- @jitsu/js:build: compiled/src/destination-plugins/tag.js
63
- @jitsu/js:build: 1: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
64
- @jitsu/js:build:  ^
65
- @jitsu/js:build: 2: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
66
- @jitsu/js:build: 3: return new (P || (P = Promise))(function (resolve, reject) {
67
- @jitsu/js:build: ...and 1 other occurrence
68
- @jitsu/js:build: compiled/src/destination-plugins/logrocket.js
69
- @jitsu/js:build: 1: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
70
- @jitsu/js:build:  ^
71
- @jitsu/js:build: 2: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
72
- @jitsu/js:build: 3: return new (P || (P = Promise))(function (resolve, reject) {
73
- @jitsu/js:build: ...and 1 other occurrence
74
- @jitsu/js:build: compiled/src/destination-plugins/gtm.js
75
- @jitsu/js:build: 1: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
76
- @jitsu/js:build:  ^
77
- @jitsu/js:build: 2: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
78
- @jitsu/js:build: 3: return new (P || (P = Promise))(function (resolve, reject) {
79
- @jitsu/js:build: ...and 1 other occurrence
80
- @jitsu/js:build: (!) Circular dependencies
81
- @jitsu/js:build: compiled/src/index.js -> compiled/src/analytics-plugin.js -> compiled/src/index.js
82
- @jitsu/js:build: compiled/src/destination-plugins/index.js -> compiled/src/destination-plugins/tag.js -> compiled/src/destination-plugins/index.js
83
- @jitsu/js:build: compiled/src/analytics-plugin.js -> compiled/src/destination-plugins/index.js -> compiled/src/destination-plugins/tag.js -> compiled/src/analytics-plugin.js
84
- @jitsu/js:build: compiled/src/destination-plugins/index.js -> compiled/src/destination-plugins/logrocket.js -> compiled/src/destination-plugins/index.js
85
- @jitsu/js:build: compiled/src/destination-plugins/index.js -> compiled/src/destination-plugins/gtm.js -> compiled/src/destination-plugins/index.js
86
- @jitsu/js:build: (!) Use of eval is strongly discouraged
87
- @jitsu/js:build: https://rollupjs.org/troubleshooting/#avoiding-eval
88
- @jitsu/js:build: compiled/src/destination-plugins/tag.js
89
- @jitsu/js:build: 68: })()`;
90
- @jitsu/js:build: 69: try {
91
- @jitsu/js:build: 70: eval(iif);
92
- @jitsu/js:build:  ^
93
- @jitsu/js:build: 71: }
94
- @jitsu/js:build: 72: catch (e) {
95
- @jitsu/js:build: created dist/jitsu.es.js, dist/jitsu.cjs.js in 353ms
1
+ @jitsu/js:build: cache hit, replaying output 03414e3b139b4a1c
2
+ @jitsu/js:build: 
3
+ @jitsu/js:build: > @jitsu/js@0.0.0 build /Users/vklmn/dev2/onetag/libs/jitsu-js
4
+ @jitsu/js:build: > tsc -p . && rollup -c && cp compiled/src/*.d.ts dist
5
+ @jitsu/js:build: 
6
+ @jitsu/js:build: 
7
+ @jitsu/js:build: ./compiled/src/browser.js → dist/web/p.js.txt...
8
+ @jitsu/js:build: (!) "this" has been rewritten to "undefined"
9
+ @jitsu/js:build: https://rollupjs.org/troubleshooting/#error-this-is-undefined
10
+ @jitsu/js:build: compiled/src/analytics-plugin.js
11
+ @jitsu/js:build: 1: /* global analytics */
12
+ @jitsu/js:build: 2: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ @jitsu/js:build:  ^
14
+ @jitsu/js:build: 3: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ @jitsu/js:build: 4: return new (P || (P = Promise))(function (resolve, reject) {
16
+ @jitsu/js:build: ...and 1 other occurrence
17
+ @jitsu/js:build: compiled/src/destination-plugins.js
18
+ @jitsu/js:build: 1: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
19
+ @jitsu/js:build:  ^
20
+ @jitsu/js:build: 2: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
21
+ @jitsu/js:build: 3: return new (P || (P = Promise))(function (resolve, reject) {
22
+ @jitsu/js:build: ...and 1 other occurrence
23
+ @jitsu/js:build: (!) Circular dependencies
24
+ @jitsu/js:build: compiled/src/index.js -> compiled/src/analytics-plugin.js -> compiled/src/index.js
25
+ @jitsu/js:build: compiled/src/analytics-plugin.js -> compiled/src/destination-plugins.js -> compiled/src/analytics-plugin.js
26
+ @jitsu/js:build: (!) Use of eval is strongly discouraged
27
+ @jitsu/js:build: https://rollupjs.org/troubleshooting/#avoiding-eval
28
+ @jitsu/js:build: compiled/src/destination-plugins.js
29
+ @jitsu/js:build: 146: })()`;
30
+ @jitsu/js:build: 147: try {
31
+ @jitsu/js:build: 148: eval(iif);
32
+ @jitsu/js:build:  ^
33
+ @jitsu/js:build: 149: }
34
+ @jitsu/js:build: 150: catch (e) {
35
+ @jitsu/js:build: created dist/web/p.js.txt in 195ms
36
+ @jitsu/js:build: 
37
+ @jitsu/js:build: ./compiled/src/index.js, ./compiled/src/jitsu.js, ./compiled/src/analytics-plugin.js → dist/jitsu.es.js, dist/jitsu.cjs.js...
38
+ @jitsu/js:build: (!) "this" has been rewritten to "undefined"
39
+ @jitsu/js:build: https://rollupjs.org/troubleshooting/#error-this-is-undefined
40
+ @jitsu/js:build: compiled/src/analytics-plugin.js
41
+ @jitsu/js:build: 1: /* global analytics */
42
+ @jitsu/js:build: 2: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
43
+ @jitsu/js:build:  ^
44
+ @jitsu/js:build: 3: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
45
+ @jitsu/js:build: 4: return new (P || (P = Promise))(function (resolve, reject) {
46
+ @jitsu/js:build: ...and 1 other occurrence
47
+ @jitsu/js:build: compiled/src/destination-plugins.js
48
+ @jitsu/js:build: 1: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
49
+ @jitsu/js:build:  ^
50
+ @jitsu/js:build: 2: function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
51
+ @jitsu/js:build: 3: return new (P || (P = Promise))(function (resolve, reject) {
52
+ @jitsu/js:build: ...and 1 other occurrence
53
+ @jitsu/js:build: (!) Circular dependencies
54
+ @jitsu/js:build: compiled/src/index.js -> compiled/src/analytics-plugin.js -> compiled/src/index.js
55
+ @jitsu/js:build: compiled/src/analytics-plugin.js -> compiled/src/destination-plugins.js -> compiled/src/analytics-plugin.js
56
+ @jitsu/js:build: (!) Use of eval is strongly discouraged
57
+ @jitsu/js:build: https://rollupjs.org/troubleshooting/#avoiding-eval
58
+ @jitsu/js:build: compiled/src/destination-plugins.js
59
+ @jitsu/js:build: 146: })()`;
60
+ @jitsu/js:build: 147: try {
61
+ @jitsu/js:build: 148: eval(iif);
62
+ @jitsu/js:build:  ^
63
+ @jitsu/js:build: 149: }
64
+ @jitsu/js:build: 150: catch (e) {
65
+ @jitsu/js:build: created dist/jitsu.es.js, dist/jitsu.cjs.js in 133ms
@@ -1,5 +1,5 @@
1
- @jitsu/js:clean: cache hit, replaying output 96c25c4b05e1319b
2
- @jitsu/js:clean: 
3
- @jitsu/js:clean: > @jitsu/js@0.0.0 clean /Users/ildarnurislamov/Projects/onetag/libs/jitsu-js
4
- @jitsu/js:clean: > rm -rf ./dist
5
- @jitsu/js:clean: 
1
+ @jitsu/js:clean: cache hit, replaying output 02a6b0bcaf2c7148
2
+ @jitsu/js:clean: 
3
+ @jitsu/js:clean: > @jitsu/js@0.0.0 clean /Users/vklmn/dev2/onetag/libs/jitsu-js
4
+ @jitsu/js:clean: > rm -rf ./dist
5
+ @jitsu/js:clean: 
@@ -35,7 +35,7 @@ describe("Test Jitsu NodeJS client", () => {
35
35
  await startServer();
36
36
  try {
37
37
  const jitsu: AnalyticsInterface = jitsuAnalytics({
38
- writeKey: "test",
38
+ writeKey: "key:secret",
39
39
  host: server.baseUrl,
40
40
  debug: true,
41
41
  fetch: fetchImpl,
@@ -0,0 +1,17 @@
1
+ import { AnalyticsClientEvent } from "@jitsu/protocols/analytics";
2
+ export type InternalPlugin<T> = {
3
+ id: string;
4
+ handle(config: T, payload: AnalyticsClientEvent): Promise<void>;
5
+ };
6
+ export type CommonDestinationCredentials = {
7
+ hosts?: string[];
8
+ events?: string[];
9
+ };
10
+ export type TagDestinationCredentials = {
11
+ code: string;
12
+ } & CommonDestinationCredentials;
13
+ export type LogRocketDestinationCredentials = {
14
+ appId: string;
15
+ } & CommonDestinationCredentials;
16
+ export type LogRocket = any;
17
+ export declare const internalDestinationPlugins: Record<string, InternalPlugin<any>>;
package/dist/jitsu.cjs.js CHANGED
@@ -110,7 +110,7 @@ function loadScript(src, attributes) {
110
110
  });
111
111
  }
112
112
 
113
- var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
113
+ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
114
114
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
115
115
  return new (P || (P = Promise))(function (resolve, reject) {
116
116
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -119,10 +119,18 @@ var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _argu
119
119
  step((generator = generator.apply(thisArg, _arguments || [])).next());
120
120
  });
121
121
  };
122
+ function satisfyFilter(filter, subject) {
123
+ return filter === "*" || filter.toLowerCase().trim() === (subject || "").trim().toLowerCase();
124
+ }
125
+ function applyFilters(event, creds) {
126
+ const { hosts = ["*"], events = ["*"] } = creds;
127
+ return (!!hosts.find(hostFilter => { var _a; return satisfyFilter(hostFilter, (_a = event.context) === null || _a === void 0 ? void 0 : _a.host); }) &&
128
+ !!events.find(eventFilter => satisfyFilter(eventFilter, event.type)));
129
+ }
122
130
  const tagPlugin = {
123
131
  id: "tag",
124
132
  handle(config, payload) {
125
- return __awaiter$3(this, void 0, void 0, function* () {
133
+ return __awaiter$1(this, void 0, void 0, function* () {
126
134
  if (!applyFilters(payload, config)) {
127
135
  return;
128
136
  }
@@ -130,96 +138,6 @@ const tagPlugin = {
130
138
  });
131
139
  },
132
140
  };
133
- function insertTags(code, event, opts = {}) {
134
- let tag;
135
- try {
136
- tag = JSON.parse(code);
137
- }
138
- catch (e) {
139
- tag = { code, lang: "javascript" };
140
- }
141
- const debug = opts.debug || false;
142
- if (isInBrowser()) {
143
- if (tag.lang === "javascript") {
144
- execJs(tag.code, event);
145
- }
146
- else {
147
- const codeHolder = document.createElement("span");
148
- codeHolder.innerHTML = replaceMacro(tag.code, event);
149
- document.body.insertAdjacentElement("beforeend", codeHolder);
150
- const scripts = codeHolder.querySelectorAll("script");
151
- scripts.forEach(script => {
152
- const scriptClone = document.createElement("script");
153
- scriptClone.type = scriptClone.type || "text/javascript";
154
- if (script.hasAttribute("src")) {
155
- scriptClone.src = script.src;
156
- }
157
- scriptClone.text = script.text;
158
- if (debug) {
159
- console.log(`[JITSU] Executing script${script.hasAttribute("src") ? ` ${script.src}` : ""}`, scriptClone.text);
160
- }
161
- document.head.appendChild(scriptClone);
162
- document.head.removeChild(scriptClone);
163
- });
164
- }
165
- }
166
- else {
167
- if (debug) {
168
- console.log(`[JITSU] insertTags(): cannot insert tags in non-browser environment`);
169
- }
170
- }
171
- }
172
- function execJs(code, event) {
173
- const varName = `jitsu_event_${randomId()}`;
174
- window[varName] = event;
175
- const iif = `(function(){
176
- const event = ${varName};
177
- ${code}
178
- })()`;
179
- try {
180
- eval(iif);
181
- }
182
- catch (e) {
183
- console.error(`[JITSU] Error executing JS code: ${e.message}. Code: `, iif);
184
- }
185
- finally {
186
- delete window[varName];
187
- }
188
- return iif;
189
- }
190
- function replaceMacro(code, event) {
191
- return code.replace(/{{\s*event\s*}}/g, JSON.stringify(event));
192
- }
193
-
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
141
  function getLogRocketState() {
224
142
  return window["__jitsuLrState"] || "fresh";
225
143
  }
@@ -245,7 +163,7 @@ function flushLogRocketQueue(lr) {
245
163
  }
246
164
  }
247
165
  function initLogrocketIfNeeded(appId) {
248
- return __awaiter$2(this, void 0, void 0, function* () {
166
+ return __awaiter$1(this, void 0, void 0, function* () {
249
167
  if (getLogRocketState() !== "fresh") {
250
168
  return;
251
169
  }
@@ -270,126 +188,94 @@ function initLogrocketIfNeeded(appId) {
270
188
  });
271
189
  });
272
190
  }
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",
191
+ const logrocketPlugin = {
192
+ id: "logrocket",
286
193
  handle(config, payload) {
287
- var _a, _b;
288
194
  return __awaiter$1(this, void 0, void 0, function* () {
289
195
  if (!applyFilters(payload, config)) {
290
196
  return;
291
197
  }
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;
198
+ initLogrocketIfNeeded(config.appId);
199
+ const action = logRocket => {
200
+ if (payload.type === "identify" && payload.userId) {
201
+ logRocket.identify(payload.userId, payload.traits || {});
202
+ }
203
+ };
204
+ getLogRocketQueue().push(action);
205
+ if (getLogRocketState() === "loaded") {
206
+ flushLogRocketQueue(window["LogRocket"]);
336
207
  }
337
208
  });
338
209
  },
339
210
  };
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;
211
+ function insertTags(code, event, opts = {}) {
212
+ let tag;
213
+ try {
214
+ tag = JSON.parse(code);
215
+ }
216
+ catch (e) {
217
+ tag = { code, lang: "javascript" };
218
+ }
219
+ const debug = opts.debug || false;
220
+ if (isInBrowser()) {
221
+ if (tag.lang === "javascript") {
222
+ execJs(tag.code, event);
350
223
  }
351
- setGtmState("loading");
352
- const dlName = config.dataLayerName || "dataLayer";
353
- const dlParam = dlName !== "dataLayer" ? "&l=" + dlName : "";
354
- const previewParams = config.preview
355
- ? `&gtm_preview=${config.preview}&gtm_auth=${config.auth}&gtm_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);
361
- };
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
- });
374
- });
224
+ else {
225
+ const codeHolder = document.createElement("span");
226
+ codeHolder.innerHTML = replaceMacro(tag.code, event);
227
+ document.body.insertAdjacentElement("beforeend", codeHolder);
228
+ const scripts = codeHolder.querySelectorAll("script");
229
+ scripts.forEach(script => {
230
+ const scriptClone = document.createElement("script");
231
+ scriptClone.type = scriptClone.type || "text/javascript";
232
+ if (script.hasAttribute("src")) {
233
+ scriptClone.src = script.src;
234
+ }
235
+ scriptClone.text = script.text;
236
+ if (debug) {
237
+ console.log(`[JITSU] Executing script${script.hasAttribute("src") ? ` ${script.src}` : ""}`, scriptClone.text);
238
+ }
239
+ document.head.appendChild(scriptClone);
240
+ document.head.removeChild(scriptClone);
241
+ });
242
+ }
243
+ }
244
+ else {
245
+ if (debug) {
246
+ console.log(`[JITSU] insertTags(): cannot insert tags in non-browser environment`);
247
+ }
248
+ }
375
249
  }
376
-
377
- function satisfyFilter(filter, subject) {
378
- return filter === "*" || filter.toLowerCase().trim() === (subject || "").trim().toLowerCase();
250
+ function execJs(code, event) {
251
+ const varName = `jitsu_event_${randomId()}`;
252
+ window[varName] = event;
253
+ const iif = `(function(){
254
+ const event = ${varName};
255
+ ${code}
256
+ })()`;
257
+ try {
258
+ eval(iif);
259
+ }
260
+ catch (e) {
261
+ console.error(`[JITSU] Error executing JS code: ${e.message}. Code: `, iif);
262
+ }
263
+ finally {
264
+ delete window[varName];
265
+ }
266
+ return iif;
379
267
  }
380
- function applyFilters(event, creds) {
381
- const { hosts = "*", events = "*" } = creds;
382
- const eventsArray = events.split("\n");
383
- return (!!hosts.split("\n").find(hostFilter => { var _a; return satisfyFilter(hostFilter, (_a = event.context) === null || _a === void 0 ? void 0 : _a.host); }) &&
384
- (!!eventsArray.find(eventFilter => satisfyFilter(eventFilter, event.type)) ||
385
- !!eventsArray.find(eventFilter => satisfyFilter(eventFilter, event.event))));
268
+ function replaceMacro(code, event) {
269
+ return code.replace(/{{\s*event\s*}}/g, JSON.stringify(event));
386
270
  }
387
271
  const internalDestinationPlugins = {
388
272
  [tagPlugin.id]: tagPlugin,
389
- [gtmPlugin.id]: gtmPlugin,
390
273
  [logrocketPlugin.id]: logrocketPlugin,
391
274
  };
392
275
 
276
+ var name = "@jitsu/js";
277
+ var version = "0.0.0";
278
+
393
279
  /* global analytics */
394
280
  var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
395
281
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
@@ -627,8 +513,8 @@ function adjustPayload(payload, config, storage) {
627
513
  const referrer = runtime.referrer();
628
514
  const context = {
629
515
  library: {
630
- name: "jitsu-js",
631
- version: "1.0.0",
516
+ name: name,
517
+ version: version,
632
518
  },
633
519
  userAgent: runtime.userAgent(),
634
520
  locale: runtime.language(),
@@ -646,7 +532,7 @@ function adjustPayload(payload, config, storage) {
646
532
  },
647
533
  campaign: parseUtms(query),
648
534
  };
649
- 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) });
535
+ 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) });
650
536
  delete withContext.meta;
651
537
  delete withContext.options;
652
538
  return withContext;
@@ -660,7 +546,6 @@ function processDestinations(destinations, method, event, debug, analyticsInstan
660
546
  const plugin = internalDestinationPlugins[destination.deviceOptions.name];
661
547
  if (plugin) {
662
548
  try {
663
- //to support old versions, where credentials were stored in root
664
549
  promises.push(plugin.handle(credentials, event));
665
550
  }
666
551
  catch (e) {
@@ -708,6 +593,15 @@ function processDestinations(destinations, method, event, debug, analyticsInstan
708
593
  }
709
594
  });
710
595
  }
596
+ function validateWriteKey(writeKey) {
597
+ if (writeKey) {
598
+ const [, secret] = writeKey.split(":", 2);
599
+ if (!secret) {
600
+ 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`);
601
+ }
602
+ }
603
+ return writeKey;
604
+ }
711
605
  function send(method, payload, jitsuConfig, instance, store) {
712
606
  if (jitsuConfig.echoEvents) {
713
607
  console.log(`[JITSU] sending '${method}' event:`, payload);
@@ -723,7 +617,7 @@ function send(method, payload, jitsuConfig, instance, store) {
723
617
  // console.log(`[JITSU] Sending event to ${url}: `, JSON.stringify(payload, null, 2));
724
618
  // }
725
619
  const adjustedPayload = adjustPayload(payload, jitsuConfig, store);
726
- const authHeader = adjustedPayload.writeKey ? { "X-Write-Key": adjustedPayload.writeKey } : {};
620
+ const authHeader = config.writeKey ? { "X-Write-Key": validateWriteKey(config.writeKey) } : {};
727
621
  return fetch(url, {
728
622
  method: "POST",
729
623
  headers: Object.assign(Object.assign({ "Content-Type": "application/json" }, authHeader), debugHeader),