@jitsu/js 1.1.1 → 1.1.2

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/dist/jitsu.es.js CHANGED
@@ -108,7 +108,7 @@ function loadScript(src, attributes) {
108
108
  });
109
109
  }
110
110
 
111
- var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
111
+ var __awaiter$3 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
112
112
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
113
113
  return new (P || (P = Promise))(function (resolve, reject) {
114
114
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -117,18 +117,10 @@ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _argu
117
117
  step((generator = generator.apply(thisArg, _arguments || [])).next());
118
118
  });
119
119
  };
120
- function satisfyFilter(filter, subject) {
121
- return filter === "*" || filter.toLowerCase().trim() === (subject || "").trim().toLowerCase();
122
- }
123
- function applyFilters(event, creds) {
124
- const { hosts = ["*"], events = ["*"] } = creds;
125
- return (!!hosts.find(hostFilter => { var _a; return satisfyFilter(hostFilter, (_a = event.context) === null || _a === void 0 ? void 0 : _a.host); }) &&
126
- !!events.find(eventFilter => satisfyFilter(eventFilter, event.type)));
127
- }
128
120
  const tagPlugin = {
129
121
  id: "tag",
130
122
  handle(config, payload) {
131
- return __awaiter$1(this, void 0, void 0, function* () {
123
+ return __awaiter$3(this, void 0, void 0, function* () {
132
124
  if (!applyFilters(payload, config)) {
133
125
  return;
134
126
  }
@@ -136,6 +128,96 @@ const tagPlugin = {
136
128
  });
137
129
  },
138
130
  };
131
+ function insertTags(code, event, opts = {}) {
132
+ let tag;
133
+ try {
134
+ tag = JSON.parse(code);
135
+ }
136
+ catch (e) {
137
+ tag = { code, lang: "javascript" };
138
+ }
139
+ const debug = opts.debug || false;
140
+ if (isInBrowser()) {
141
+ if (tag.lang === "javascript") {
142
+ execJs(tag.code, event);
143
+ }
144
+ else {
145
+ const codeHolder = document.createElement("span");
146
+ codeHolder.innerHTML = replaceMacro(tag.code, event);
147
+ document.body.insertAdjacentElement("beforeend", codeHolder);
148
+ const scripts = codeHolder.querySelectorAll("script");
149
+ scripts.forEach(script => {
150
+ const scriptClone = document.createElement("script");
151
+ scriptClone.type = scriptClone.type || "text/javascript";
152
+ if (script.hasAttribute("src")) {
153
+ scriptClone.src = script.src;
154
+ }
155
+ scriptClone.text = script.text;
156
+ if (debug) {
157
+ console.log(`[JITSU] Executing script${script.hasAttribute("src") ? ` ${script.src}` : ""}`, scriptClone.text);
158
+ }
159
+ document.head.appendChild(scriptClone);
160
+ document.head.removeChild(scriptClone);
161
+ });
162
+ }
163
+ }
164
+ else {
165
+ if (debug) {
166
+ console.log(`[JITSU] insertTags(): cannot insert tags in non-browser environment`);
167
+ }
168
+ }
169
+ }
170
+ function execJs(code, event) {
171
+ const varName = `jitsu_event_${randomId()}`;
172
+ window[varName] = event;
173
+ const iif = `(function(){
174
+ const event = ${varName};
175
+ ${code}
176
+ })()`;
177
+ try {
178
+ eval(iif);
179
+ }
180
+ catch (e) {
181
+ console.error(`[JITSU] Error executing JS code: ${e.message}. Code: `, iif);
182
+ }
183
+ finally {
184
+ delete window[varName];
185
+ }
186
+ return iif;
187
+ }
188
+ function replaceMacro(code, event) {
189
+ return code.replace(/{{\s*event\s*}}/g, JSON.stringify(event));
190
+ }
191
+
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
+ };
139
221
  function getLogRocketState() {
140
222
  return window["__jitsuLrState"] || "fresh";
141
223
  }
@@ -161,7 +243,7 @@ function flushLogRocketQueue(lr) {
161
243
  }
162
244
  }
163
245
  function initLogrocketIfNeeded(appId) {
164
- return __awaiter$1(this, void 0, void 0, function* () {
246
+ return __awaiter$2(this, void 0, void 0, function* () {
165
247
  if (getLogRocketState() !== "fresh") {
166
248
  return;
167
249
  }
@@ -186,88 +268,135 @@ function initLogrocketIfNeeded(appId) {
186
268
  });
187
269
  });
188
270
  }
189
- const logrocketPlugin = {
190
- id: "logrocket",
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",
191
284
  handle(config, payload) {
285
+ var _a, _b;
192
286
  return __awaiter$1(this, void 0, void 0, function* () {
193
287
  if (!applyFilters(payload, config)) {
194
288
  return;
195
289
  }
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"]);
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;
205
334
  }
206
335
  });
207
336
  },
208
337
  };
209
- function insertTags(code, event, opts = {}) {
210
- let tag;
211
- try {
212
- tag = JSON.parse(code);
213
- }
214
- catch (e) {
215
- tag = { code, lang: "javascript" };
216
- }
217
- const debug = opts.debug || false;
218
- if (isInBrowser()) {
219
- if (tag.lang === "javascript") {
220
- execJs(tag.code, event);
221
- }
222
- else {
223
- const codeHolder = document.createElement("span");
224
- codeHolder.innerHTML = replaceMacro(tag.code, event);
225
- document.body.insertAdjacentElement("beforeend", codeHolder);
226
- const scripts = codeHolder.querySelectorAll("script");
227
- scripts.forEach(script => {
228
- const scriptClone = document.createElement("script");
229
- scriptClone.type = scriptClone.type || "text/javascript";
230
- if (script.hasAttribute("src")) {
231
- scriptClone.src = script.src;
232
- }
233
- scriptClone.text = script.text;
234
- if (debug) {
235
- console.log(`[JITSU] Executing script${script.hasAttribute("src") ? ` ${script.src}` : ""}`, scriptClone.text);
236
- }
237
- document.head.appendChild(scriptClone);
238
- document.head.removeChild(scriptClone);
239
- });
240
- }
241
- }
242
- else {
243
- if (debug) {
244
- console.log(`[JITSU] insertTags(): cannot insert tags in non-browser environment`);
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;
245
348
  }
246
- }
349
+ setGtmState("loading");
350
+ const dlName = config.dataLayerName || "dataLayer";
351
+ const dlParam = dlName !== "dataLayer" ? "&l=" + dlName : "";
352
+ const previewParams = config.preview
353
+ ? `&gtm_preview=${config.preview}&gtm_auth=${config.auth}&gtm_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);
359
+ };
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
+ });
372
+ });
247
373
  }
248
- function execJs(code, event) {
249
- const varName = `jitsu_event_${randomId()}`;
250
- window[varName] = event;
251
- const iif = `(function(){
252
- const event = ${varName};
253
- ${code}
254
- })()`;
255
- try {
256
- eval(iif);
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;
257
381
  }
258
- catch (e) {
259
- console.error(`[JITSU] Error executing JS code: ${e.message}. Code: `, iif);
382
+ subject = subject || "";
383
+ if (filter.startsWith("*.")) {
384
+ return subject.endsWith(filter.substring(1));
260
385
  }
261
- finally {
262
- delete window[varName];
386
+ else {
387
+ return filter === subject;
263
388
  }
264
- return iif;
265
389
  }
266
- function replaceMacro(code, event) {
267
- return code.replace(/{{\s*event\s*}}/g, JSON.stringify(event));
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))));
268
396
  }
269
397
  const internalDestinationPlugins = {
270
398
  [tagPlugin.id]: tagPlugin,
399
+ [gtmPlugin.id]: gtmPlugin,
271
400
  [logrocketPlugin.id]: logrocketPlugin,
272
401
  };
273
402
 
@@ -284,7 +413,7 @@ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _argume
284
413
  step((generator = generator.apply(thisArg, _arguments || [])).next());
285
414
  });
286
415
  };
287
- const config = {
416
+ const defaultConfig = {
288
417
  /* Your segment writeKey */
289
418
  writeKey: null,
290
419
  /* Disable anonymous MTU */
@@ -591,10 +720,13 @@ function processDestinations(destinations, method, event, debug, analyticsInstan
591
720
  }
592
721
  });
593
722
  }
723
+ function looksLikeCuid(id) {
724
+ return id.length === 25 && id.charAt(0) === "c";
725
+ }
594
726
  function validateWriteKey(writeKey) {
595
727
  if (writeKey) {
596
728
  const [, secret] = writeKey.split(":", 2);
597
- if (!secret) {
729
+ if (!secret && !looksLikeCuid(writeKey)) {
598
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`);
599
731
  }
600
732
  }
@@ -615,7 +747,7 @@ function send(method, payload, jitsuConfig, instance, store) {
615
747
  // console.log(`[JITSU] Sending event to ${url}: `, JSON.stringify(payload, null, 2));
616
748
  // }
617
749
  const adjustedPayload = adjustPayload(payload, jitsuConfig, store);
618
- const authHeader = config.writeKey ? { "X-Write-Key": validateWriteKey(config.writeKey) } : {};
750
+ const authHeader = jitsuConfig.writeKey ? { "X-Write-Key": validateWriteKey(jitsuConfig.writeKey) } : {};
619
751
  return fetch(url, {
620
752
  method: "POST",
621
753
  headers: Object.assign(Object.assign({ "Content-Type": "application/json" }, authHeader), debugHeader),
@@ -671,7 +803,7 @@ const jitsuAnalyticsPlugin = (pluginConfig = {}) => {
671
803
  persistentStorage.removeItem(key);
672
804
  },
673
805
  });
674
- const instanceConfig = Object.assign(Object.assign({}, config), pluginConfig);
806
+ const instanceConfig = Object.assign(Object.assign({}, defaultConfig), pluginConfig);
675
807
  return {
676
808
  name: "jitsu",
677
809
  config: instanceConfig,