@jitsu/js 1.6.1 → 1.7.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.
@@ -77,23 +77,25 @@ function getCookie(name: string) {
77
77
  return parts.length === 2 ? parts.pop().split(";").shift() : undefined;
78
78
  }
79
79
 
80
- function getGa4Sessions(): Record<string, string> | undefined {
81
- const value = `; ${document.cookie}`;
82
- const matches = value.matchAll(/_ga_(\w+)=([^;]+)/g);
83
- const sessions: Record<string, string> = {};
84
- let matchesCount = 0;
85
- for (const match of matches) {
86
- const parts = match[2].split(".");
87
- if (parts.length < 3) {
88
- continue;
89
- }
90
- sessions[match[1]] = parts[2];
91
- matchesCount++;
92
- }
93
- if (matchesCount === 0) {
80
+ function getGa4Sessions(allCookies: Record<string, string>): Record<string, string> | undefined {
81
+ const gaCookies = Object.entries(allCookies).filter(([key]) => key.startsWith("_ga_"));
82
+ if (gaCookies.length === 0) {
94
83
  return undefined;
95
84
  }
96
- return sessions;
85
+ return Object.fromEntries(
86
+ gaCookies
87
+ .map(([key, value]) => {
88
+ if (typeof value !== "string") {
89
+ return null;
90
+ }
91
+ const parts = value.split(".");
92
+ if (parts.length < 3) {
93
+ return null;
94
+ }
95
+ return [key.substring("_ga_".length), parts[2]];
96
+ })
97
+ .filter(v => v !== null)
98
+ );
97
99
  }
98
100
 
99
101
  function removeCookie(name: string) {
@@ -149,6 +151,20 @@ const cookieStorage: StorageFactory = (cookieDomain, key2cookie) => {
149
151
 
150
152
  export function windowRuntime(opts: JitsuOptions): RuntimeFacade {
151
153
  return {
154
+ getCookie(name: string): string | undefined {
155
+ const value = `; ${document.cookie}`;
156
+ const parts = value.split(`; ${name}=`);
157
+ return parts.length === 2 ? parts.pop().split(";").shift() : undefined;
158
+ },
159
+ getCookies(): Record<string, string> {
160
+ const value = `; ${document.cookie}`;
161
+ const cookies: Record<string, string> = {};
162
+ const matches = value.matchAll(/(\w+)=([^;]+)/g);
163
+ for (const match of matches) {
164
+ cookies[match[1]] = match[2];
165
+ }
166
+ return cookies;
167
+ },
152
168
  documentEncoding(): string | undefined {
153
169
  return window.document.characterSet;
154
170
  },
@@ -193,6 +209,12 @@ export const emptyRuntime = (config: JitsuOptions): RuntimeFacade => ({
193
209
  timezoneOffset(): number | undefined {
194
210
  return undefined;
195
211
  },
212
+ getCookie(name: string): string | undefined {
213
+ return undefined;
214
+ },
215
+ getCookies(): Record<string, string> {
216
+ return {};
217
+ },
196
218
 
197
219
  store(): PersistentStorage {
198
220
  const storage = {};
@@ -284,11 +306,11 @@ function adjustPayload(payload: any, config: JitsuOptions, storage: PersistentSt
284
306
  encoding: properties.encoding || runtime.documentEncoding(),
285
307
  },
286
308
  clientIds: {
287
- fbc: getCookie("_fbc"),
288
- fbp: getCookie("_fbp"),
309
+ fbc: runtime.getCookie("_fbc"),
310
+ fbp: runtime.getCookie("_fbp"),
289
311
  ga4: {
290
- clientId: getCookie("_ga")?.split(".").slice(-2).join("."), //last 2 parts of GA cookie
291
- sessions: getGa4Sessions(),
312
+ clientId: runtime.getCookie("_ga")?.split(".").slice(-2).join("."), //last 2 parts of GA cookie
313
+ sessions: getGa4Sessions(runtime.getCookies()),
292
314
  },
293
315
  },
294
316
  campaign: parseUtms(query),
package/src/browser.ts CHANGED
@@ -53,14 +53,35 @@ function getScriptAttributes(scriptElement: HTMLScriptElement) {
53
53
 
54
54
  const options = readJitsuOptions();
55
55
  const JITSU_V2_ID: string = options.namespace || "jitsu";
56
-
56
+ const queue = [];
57
57
  if (window[JITSU_V2_ID]) {
58
- console.log("Jitsu is already initialized");
58
+ if (Array.isArray(window[JITSU_V2_ID])) {
59
+ //processing queue of events
60
+ if (options.debug) {
61
+ console.log(
62
+ `Initializing Jitsu with prior events queue size of ${window[JITSU_V2_ID].length}`,
63
+ window[JITSU_V2_ID]
64
+ );
65
+ }
66
+ queue.push(...window[JITSU_V2_ID]);
67
+ } else {
68
+ console.warn("Attempted to initialize Jitsu twice. Returning the existing instance");
69
+ }
59
70
  }
60
71
  if (options.debug) {
61
72
  console.log(`Jitsu options: `, JSON.stringify(options));
62
73
  }
63
74
  const jitsu = jitsuAnalytics(options);
75
+ for (const [method, args] of queue) {
76
+ if (options.debug) {
77
+ console.log(`Processing event ${method} from Jitsu queue on`, args);
78
+ }
79
+ try {
80
+ jitsu[method](...args);
81
+ } catch (e: any) {
82
+ console.warn(`Error processing event ${method} from Jitsu queue on`, args, e);
83
+ }
84
+ }
64
85
 
65
86
  if (options.onload) {
66
87
  const onloadFunction = window[options.onload] as any;
@@ -75,6 +96,18 @@ function getScriptAttributes(scriptElement: HTMLScriptElement) {
75
96
  }
76
97
  window[JITSU_V2_ID] = jitsu;
77
98
 
99
+ //new callback based queue
100
+ const callbackQueue = window[JITSU_V2_ID + "Q"] || [];
101
+ if (options.debug) {
102
+ console.log(`Jitsu callback queue size: ${callbackQueue.length}`, callbackQueue);
103
+ }
104
+ callbackQueue.forEach((callback: any) => {
105
+ try {
106
+ callback(jitsu);
107
+ } catch (e: any) {
108
+ console.warn(`Error processing callback from Jitsu queue`, e);
109
+ }
110
+ });
78
111
  if (!options.initOnly) {
79
112
  jitsu.page();
80
113
  }
package/src/index.ts CHANGED
@@ -98,5 +98,3 @@ export function jitsuAnalytics(opts: JitsuOptions): AnalyticsInterface {
98
98
 
99
99
  export * from "./jitsu";
100
100
  export * from "./analytics-plugin";
101
- export { applyFilters } from "./destination-plugins";
102
- export { getTopLevelDomain } from "./tlds";
package/src/jitsu.ts CHANGED
@@ -52,6 +52,9 @@ type RuntimeFacade = {
52
52
  language(): string | undefined;
53
53
  pageUrl(): string | undefined;
54
54
  documentEncoding(): string | undefined;
55
+ getCookie(name: string): string | undefined;
56
+ getCookies(): Record<string, string>;
57
+
55
58
  timezoneOffset(): number | undefined;
56
59
  screen():
57
60
  | {