@jitsu/js 1.9.0-canary.581.20240115194116 → 1.9.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jitsu/js",
3
- "version": "1.9.0-canary.581.20240115194116",
3
+ "version": "1.9.0",
4
4
  "description": "",
5
5
  "author": "Jitsu Dev Team <dev@jitsu.com>",
6
6
  "main": "dist/jitsu.cjs.js",
@@ -34,7 +34,8 @@
34
34
  "rollup": "^3.2.5",
35
35
  "ts-jest": "29.0.5",
36
36
  "typescript": "^5.3.3",
37
- "@jitsu/protocols": "1.9.0-canary.581.20240115194116"
37
+ "jsondiffpatch": "1.9.0",
38
+ "@jitsu/protocols": "1.9.0"
38
39
  },
39
40
  "dependencies": {
40
41
  "analytics": "0.8.9"
@@ -9,6 +9,9 @@ import { loadScript } from "./script-loader";
9
9
  import { internalDestinationPlugins } from "./destination-plugins";
10
10
  import { jitsuLibraryName, jitsuVersion } from "./version";
11
11
  import { getTopLevelDomain } from "./tlds";
12
+ import * as jsondiffpatch from "jsondiffpatch";
13
+
14
+ const diff = jsondiffpatch.create();
12
15
 
13
16
  const defaultConfig: Required<JitsuOptions> = {
14
17
  /* Your segment writeKey */
@@ -20,6 +23,7 @@ const defaultConfig: Required<JitsuOptions> = {
20
23
  echoEvents: false,
21
24
  cookieDomain: undefined,
22
25
  runtime: undefined,
26
+ fetchTimeoutMs: undefined,
23
27
  s2s: undefined,
24
28
  };
25
29
 
@@ -390,6 +394,11 @@ export type InternalPluginDescriptor = {
390
394
 
391
395
  export type DeviceOptions = AnalyticsPluginDescriptor | InternalPluginDescriptor;
392
396
 
397
+ function isDiff(obj: any) {
398
+ const keys = Object.keys(obj);
399
+ return keys.length === 1 && keys[0] === "__diff";
400
+ }
401
+
393
402
  async function processDestinations(
394
403
  destinations: DestinationDescriptor[],
395
404
  method: string,
@@ -402,7 +411,13 @@ async function processDestinations(
402
411
  for (const destination of destinations) {
403
412
  let newEvents = [originalEvent];
404
413
  if (destination.newEvents) {
405
- newEvents = destination.newEvents.map(e => (e === "same" ? originalEvent : e));
414
+ try {
415
+ newEvents = destination.newEvents.map(e =>
416
+ e === "same" ? originalEvent : isDiff(e) ? diff.patch(originalEvent, e.__diff) : e
417
+ );
418
+ } catch (e) {
419
+ console.error(`[JITSU] Error applying '${destination.id}' changes to event: ${e?.message}`, e);
420
+ }
406
421
  }
407
422
  const credentials = { ...destination.credentials, ...destination.options };
408
423
 
@@ -533,6 +548,12 @@ async function send(
533
548
  // console.log(`[JITSU] Sending event to ${url}: `, JSON.stringify(payload, null, 2));
534
549
  // }
535
550
  const adjustedPayload = adjustPayload(payload, jitsuConfig, store, s2s);
551
+ const abortController = jitsuConfig.fetchTimeoutMs ? new AbortController() : undefined;
552
+ const abortTimeout = jitsuConfig.fetchTimeoutMs
553
+ ? setTimeout(() => {
554
+ abortController.abort();
555
+ }, jitsuConfig.fetchTimeoutMs)
556
+ : undefined;
536
557
 
537
558
  const authHeader = jitsuConfig.writeKey ? { "X-Write-Key": jitsuConfig.writeKey } : {};
538
559
  let fetchResult;
@@ -546,7 +567,11 @@ async function send(
546
567
  ...debugHeader,
547
568
  },
548
569
  body: JSON.stringify(adjustedPayload),
570
+ signal: abortController?.signal,
549
571
  });
572
+ if (abortTimeout) {
573
+ clearTimeout(abortTimeout);
574
+ }
550
575
  } catch (e: any) {
551
576
  throw new Error(`Calling ${url} failed: ${e.message}`);
552
577
  }
package/src/browser.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JitsuOptions } from "./jitsu";
1
+ import type { AnalyticsInterface, JitsuOptions } from "./jitsu";
2
2
  import { jitsuAnalytics } from "./index";
3
3
 
4
4
  export type JitsuBrowserOptions = {
@@ -40,6 +40,21 @@ function getScriptAttributes(scriptElement: HTMLScriptElement) {
40
40
  );
41
41
  }
42
42
 
43
+ function runCallback(callback: any, jitsu: AnalyticsInterface) {
44
+ if (typeof callback === "function") {
45
+ callback(jitsu);
46
+ } else if (Array.isArray(callback) && typeof callback[0] === "string") {
47
+ const [method, ...args] = callback;
48
+ if (typeof jitsu[method] === "function") {
49
+ jitsu[method](...args);
50
+ } else {
51
+ console.warn(`Method ${method} is not supported, ignoring callback`);
52
+ }
53
+ } else {
54
+ console.warn(`Invalid jitsu queue callback`, callback);
55
+ }
56
+ }
57
+
43
58
  (function () {
44
59
  function readJitsuOptions(): JitsuBrowserOptions {
45
60
  const scriptElement = window.document.currentScript as HTMLScriptElement;
@@ -93,7 +108,7 @@ function getScriptAttributes(scriptElement: HTMLScriptElement) {
93
108
  }
94
109
  callbackQueue.forEach((callback: any) => {
95
110
  try {
96
- callback(jitsu);
111
+ runCallback(callback, jitsu);
97
112
  } catch (e: any) {
98
113
  console.warn(`Error processing callback from Jitsu queue`, e);
99
114
  }
@@ -2,15 +2,9 @@ import { loadScript } from "../script-loader";
2
2
  import { AnalyticsClientEvent } from "@jitsu/protocols/analytics";
3
3
  import { applyFilters, CommonDestinationCredentials, InternalPlugin } from "./index";
4
4
 
5
- const defaultScriptSrc = "https://www.googletagmanager.com/gtag/js";
6
-
7
5
  export type GtmDestinationCredentials = {
8
- debug?: boolean;
9
6
  containerId?: string;
10
7
  dataLayerName?: string;
11
- preview?: string;
12
- auth?: string;
13
- customScriptSrc?: string;
14
8
  } & CommonDestinationCredentials;
15
9
 
16
10
  export const gtmPlugin: InternalPlugin<GtmDestinationCredentials> = {
@@ -83,31 +77,26 @@ async function initGtmIfNeeded(config: GtmDestinationCredentials, payload: Analy
83
77
  setGtmState("loading");
84
78
 
85
79
  const dlName = config.dataLayerName || "dataLayer";
86
- const dlParam = dlName !== "dataLayer" ? "&l=" + dlName : "";
87
- const previewParams = config.preview
88
- ? `&gtm_preview=${config.preview}&gtm_auth=${config.auth}&gtm_cookies_win=x`
89
- : "";
90
80
  const tagId = config.containerId;
91
- const scriptSrc = `${config.customScriptSrc || defaultScriptSrc}?id=${tagId}${dlParam}${previewParams}`;
92
-
93
- window[dlName] = window[dlName] || [];
94
- const gtag = function () {
95
- window[dlName].push(arguments);
96
- };
97
- window[dlName].push({
98
- user_id: payload.userId,
99
- });
100
- // @ts-ignore
101
- gtag("js", new Date());
102
- // @ts-ignore
103
- gtag("config", tagId);
104
81
 
105
- loadScript(scriptSrc)
106
- .then(() => {
107
- setGtmState("loaded");
108
- })
109
- .catch(e => {
110
- console.warn(`GTM (containerId=${tagId}) init failed: ${e.message}`, e);
111
- setGtmState("failed");
82
+ (function (w, l, i) {
83
+ w[l] = w[l] || [];
84
+ w[l].push({
85
+ user_id: payload.userId,
86
+ });
87
+ w[l].push({
88
+ "gtm.start": new Date().getTime(),
89
+ event: "gtm.js",
112
90
  });
91
+ const dl = l != "dataLayer" ? "&l=" + l : "";
92
+ const scriptSrc = "https://www.googletagmanager.com/gtm.js?id=" + i + dl;
93
+ loadScript(scriptSrc)
94
+ .then(() => {
95
+ setGtmState("loaded");
96
+ })
97
+ .catch(e => {
98
+ console.warn(`GTM (containerId=${tagId}) init failed: ${e.message}`, e);
99
+ setGtmState("failed");
100
+ });
101
+ })(window, dlName, tagId);
113
102
  }
package/src/index.ts CHANGED
@@ -16,7 +16,7 @@ export default function parse(input) {
16
16
  if (parseFloat(value) === value) {
17
17
  value = parseFloat(value);
18
18
  }
19
- } catch (e) {}
19
+ } catch (e) { }
20
20
  if (value === null || value === "") {
21
21
  return;
22
22
  }
@@ -24,7 +24,7 @@ export default function parse(input) {
24
24
  }
25
25
 
26
26
  export const emptyAnalytics: AnalyticsInterface = {
27
- setAnonymousId: () => {},
27
+ setAnonymousId: () => { },
28
28
  track: () => Promise.resolve(),
29
29
  page: () => Promise.resolve(),
30
30
  user: () => ({}),
@@ -123,15 +123,15 @@ function createUnderlyingAnalyticsInstance(
123
123
  setAnonymousId: (id: string) => {
124
124
  if (opts.debug) {
125
125
  console.log("[JITSU DEBUG] Setting anonymous id to " + id);
126
- //Workaround for analytics.js bug. Underlying setAnonymousId doesn't work set the id immediately,
127
- //so we got to it manually here. See https://github.com/jitsucom/jitsu/issues/1060
128
- storage.setItem("__anon_id", id);
129
- const userState = analytics.user();
130
- if (userState) {
131
- userState.anonymousId = id;
132
- }
133
- (analytics as any).setAnonymousId(id);
134
126
  }
127
+ //Workaround for analytics.js bug. Underlying setAnonymousId doesn't work set the id immediately,
128
+ //so we got to it manually here. See https://github.com/jitsucom/jitsu/issues/1060
129
+ storage.setItem("__anon_id", id);
130
+ const userState = analytics.user();
131
+ if (userState) {
132
+ userState.anonymousId = id;
133
+ }
134
+ (analytics as any).setAnonymousId(id);
135
135
  },
136
136
  async group(
137
137
  groupId?: ID,
package/src/jitsu.ts CHANGED
@@ -45,6 +45,11 @@ type JitsuOptions = {
45
45
  * If not set at all, it will be detected automatically by presence of `window` object
46
46
  */
47
47
  s2s?: boolean;
48
+
49
+ /**
50
+ * Timeout for fetch requests. Default value: 5000
51
+ */
52
+ fetchTimeoutMs?: number;
48
53
  };
49
54
 
50
55
  type PersistentStorage = {
package/src/version.ts CHANGED
@@ -1,6 +1,6 @@
1
- //import pkg from "../package.json";
1
+ import pkg from "../package.json";
2
2
 
3
- const jitsuVersion = "0.0.0";
3
+ const jitsuVersion = pkg.version !== "0.0.0" ? pkg.version : "2.0.0";
4
4
  const jitsuLibraryName = "@jitsu/js";
5
5
 
6
6
  export { jitsuVersion, jitsuLibraryName };