rozmova-analytics 1.1.39 → 1.1.41

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/README.md CHANGED
@@ -68,16 +68,25 @@ window.Analytics.trackEvent("button_click", { button_name: "Sign Up" });
68
68
  Initializes the analytics library, setting up Mixpanel, Google Analytics, Customer.io, and Microsoft Clarity integrations.
69
69
 
70
70
  **Parameters:**
71
+ - `config.isProd?: boolean` - Whether the environment is production (default: true)
71
72
  - `config.locale?: string` - The locale for analytics data (default: auto-detected or "uk")
72
73
  - `config.platform?: string` - The platform identifier (default: "web")
73
74
  - `config.isClearly?: boolean` - Whether to enable Clearly-specific features like cookie consent banner
75
+ - `config.config?: object` - Initial configuration object with userId and email
76
+ - `config.config.userId?: string` - Initial user ID
77
+ - `config.config.email?: string` - Initial user email
74
78
 
75
79
  **Example:**
76
80
  ```javascript
77
81
  Analytics.init({
82
+ isProd: true,
78
83
  locale: "en",
79
84
  platform: "web",
80
- isClearly: true
85
+ isClearly: true,
86
+ config: {
87
+ userId: "user-123",
88
+ email: "user@example.com"
89
+ }
81
90
  });
82
91
  ```
83
92
 
@@ -126,6 +135,8 @@ Associates a user with the provided user ID and sets user properties across all
126
135
  - `locale: string` - User's locale
127
136
  - `phone?: string` - User's phone number (optional)
128
137
  - `isB2B?: boolean` - Whether the user is a business user (optional)
138
+ - `CIOParams?: EventParams` - Additional properties for Customer.io (optional)
139
+ - `mixpanelParams?: Dict` - Additional properties for Mixpanel (optional)
129
140
 
130
141
  **Example:**
131
142
  ```javascript
@@ -135,13 +146,19 @@ Analytics.setUser("user-456", {
135
146
  numberOfSessions: 3,
136
147
  locale: "en",
137
148
  phone: "+1234567890",
138
- isB2B: true
149
+ isB2B: true,
150
+ CIOParams: {
151
+ custom_field: "value"
152
+ },
153
+ mixpanelParams: {
154
+ $created: "2024-01-01"
155
+ }
139
156
  });
140
157
  ```
141
158
 
142
159
  ### `resetUser()`
143
160
 
144
- Resets the current user across all analytics providers, clears stored user data, generates a new user ID, and reinitializes the library.
161
+ Resets the current user across all analytics providers, clears stored user data, generates a new user ID, and reinitializes the library. Automatically calls `init()` after reset.
145
162
 
146
163
  **Example:**
147
164
  ```javascript
@@ -164,20 +181,13 @@ Analytics.setUserTag("subscription_plan", "pro");
164
181
 
165
182
  ### `setConfig(params?)`
166
183
 
167
- Updates the analytics configuration with new parameters.
184
+ Updates the analytics configuration with new parameters. This is a private method but can be called indirectly through `setUser()` or `resetUser()`.
168
185
 
169
186
  **Parameters:**
170
- - `params.userId?: string` - User ID to set
171
- - `params.email?: string` - User email to set
172
- - `params.locale?: string` - Locale to set
187
+ - `params.userId?: string` - User ID to set (default: from profile)
188
+ - `params.email?: string` - User email to set (default: from profile)
173
189
 
174
- **Example:**
175
- ```javascript
176
- Analytics.setConfig({
177
- userId: "new-user-id",
178
- locale: "fr"
179
- });
180
- ```
190
+ **Note:** This method is called automatically when setting or resetting users. Direct usage is not recommended.
181
191
 
182
192
  ### `setLocale(newLocale)`
183
193
 
@@ -191,13 +201,20 @@ Updates the locale for analytics data.
191
201
  Analytics.setLocale("es");
192
202
  ```
193
203
 
194
- ### `trackPageView()`
204
+ ### `trackPageView(options?)`
195
205
 
196
206
  Tracks a page view event with current page information including title, location, and referrer.
197
207
 
208
+ **Parameters:**
209
+ - `options.referrer?: string` - Custom referrer URL (optional, defaults to document.referrer)
210
+
198
211
  **Example:**
199
212
  ```javascript
213
+ // Track with default referrer
200
214
  Analytics.trackPageView();
215
+
216
+ // Track with custom referrer
217
+ Analytics.trackPageView({ referrer: "https://example.com" });
201
218
  ```
202
219
 
203
220
  ### `getCommonParams()`
@@ -223,6 +240,77 @@ Retrieves the current user ID from cookies, localStorage, or URL query parameter
223
240
  const userId = Analytics.getUserId();
224
241
  ```
225
242
 
243
+ ### `getGAClientId()`
244
+
245
+ Retrieves the Google Analytics client ID asynchronously.
246
+
247
+ **Returns:** `Promise<string | null>`
248
+
249
+ **Example:**
250
+ ```javascript
251
+ const gaClientId = await Analytics.getGAClientId();
252
+ ```
253
+
254
+ ### `setGAClientId(clientId)`
255
+
256
+ Sets the Google Analytics client ID.
257
+
258
+ **Parameters:**
259
+ - `clientId: string` - The GA client ID to set
260
+
261
+ **Example:**
262
+ ```javascript
263
+ Analytics.setGAClientId("1234567890.1234567890");
264
+ ```
265
+
266
+ ### `getAttributionProperties(forcedAttributionProperties?)`
267
+
268
+ Retrieves comprehensive attribution properties including funnel info, tracking parameters, and external analytics user information.
269
+
270
+ **Parameters:**
271
+ - `forcedAttributionProperties?: ForcedAttributionProperties` - Optional forced attribution properties
272
+ - `forcedAttributionProperties.funnelName?: string` - Override funnel name
273
+
274
+ **Returns:** `Promise<UserAttributionProperties>`
275
+
276
+ **Example:**
277
+ ```javascript
278
+ const attribution = await Analytics.getAttributionProperties({
279
+ funnelName: "custom_funnel"
280
+ });
281
+
282
+ console.log(attribution.funnelInfo);
283
+ console.log(attribution.trackingParams);
284
+ console.log(attribution.externalAnalyticsUserInfo);
285
+ ```
286
+
287
+ ### `trackFirstPartyEvent(eventName, properties?, forcedAttributionProperties?)`
288
+
289
+ Tracks an event to your first-party analytics endpoint with comprehensive attribution data encoded in headers.
290
+
291
+ **Parameters:**
292
+ - `eventName: string` - The name of the event to track
293
+ - `properties?: EventParams` - Additional event properties (optional)
294
+ - `forcedAttributionProperties?: ForcedAttributionProperties` - Optional forced attribution properties
295
+
296
+ **Returns:** `Promise<Response>`
297
+
298
+ **Example:**
299
+ ```javascript
300
+ // Track first-party event
301
+ await Analytics.trackFirstPartyEvent("purchase_completed", {
302
+ product_id: "123",
303
+ price: 29.99
304
+ });
305
+
306
+ // Track with forced attribution
307
+ await Analytics.trackFirstPartyEvent("signup", {
308
+ source: "landing_page"
309
+ }, {
310
+ funnelName: "marketing_campaign"
311
+ });
312
+ ```
313
+
226
314
  ## Analytics Providers
227
315
 
228
316
  The library integrates with multiple analytics providers out of the box:
@@ -309,14 +397,25 @@ Analytics.trackEvent("feature_used", {
309
397
  user_tier: "premium"
310
398
  });
311
399
 
400
+ // Track first-party event with attribution
401
+ await Analytics.trackFirstPartyEvent("purchase", {
402
+ product_id: "123",
403
+ amount: 99.99
404
+ });
405
+
406
+ // Get attribution properties
407
+ const attribution = await Analytics.getAttributionProperties();
408
+ console.log(attribution.funnelInfo, attribution.trackingParams);
409
+
410
+ // Get GA client ID
411
+ const gaClientId = await Analytics.getGAClientId();
412
+
312
413
  // Set user tags for Clarity
313
414
  Analytics.setUserTag("subscription", "premium");
314
415
  Analytics.setUserTag("user_segment", "power_user");
315
416
 
316
- // Update configuration
317
- Analytics.setConfig({
318
- locale: "fr"
319
- });
417
+ // Update locale
418
+ Analytics.setLocale("fr");
320
419
 
321
420
  // Reset user on logout
322
421
  Analytics.resetUser();
@@ -352,7 +451,11 @@ The library includes TypeScript definitions for better development experience:
352
451
 
353
452
  ```typescript
354
453
  import Analytics from "rozmova-analytics";
355
- import type { EventParams } from "rozmova-analytics";
454
+ import type {
455
+ EventParams,
456
+ UserAttributionProperties,
457
+ ForcedAttributionProperties
458
+ } from "rozmova-analytics";
356
459
 
357
460
  // Type-safe event tracking
358
461
  const eventParams: EventParams = {
@@ -361,6 +464,16 @@ const eventParams: EventParams = {
361
464
  };
362
465
 
363
466
  Analytics.trackEvent("product_view", eventParams);
467
+
468
+ // Type-safe attribution properties
469
+ const attribution: UserAttributionProperties = await Analytics.getAttributionProperties();
470
+
471
+ // Type-safe first-party event tracking
472
+ const forcedAttribution: ForcedAttributionProperties = {
473
+ funnelName: "custom_funnel"
474
+ };
475
+
476
+ await Analytics.trackFirstPartyEvent("signup", eventParams, forcedAttribution);
364
477
  ```
365
478
 
366
479
  ## Contributing
package/dist/helpers.d.ts CHANGED
@@ -8,8 +8,8 @@ export declare function detectOS(): "Windows" | "macOS" | "Linux" | "Android" |
8
8
  export declare const getDomain: () => string;
9
9
  export declare const getReferrer: () => string | null;
10
10
  export declare function getSearchQueryFromReferrer(): string | null;
11
- export declare const getClientIdFromCookies: () => string;
12
- export declare const getGAClientId: () => Promise<string>;
11
+ export declare const getClientIdFromCookies: () => string | null;
12
+ export declare const getGAClientId: () => Promise<string | null>;
13
13
  export declare function getGASessionId(): string | null;
14
14
  export declare const setGAClientId: (clientId: string) => void;
15
15
  export declare const getLocaleFromURL: (isClearly?: boolean) => string;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Dict } from "mixpanel-browser";
1
2
  import { AnalyticsCommonParams, EventParams, ForcedAttributionProperties, UserAttributionProperties } from "./types";
2
3
  type GConfig = {
3
4
  userId?: string;
@@ -20,7 +21,7 @@ declare class Analytics {
20
21
  }): Promise<void>;
21
22
  private processDataLayer;
22
23
  setUserTag(key: string, value: string): void;
23
- getGAClientId(): Promise<string>;
24
+ getGAClientId(): Promise<string | null>;
24
25
  setGAClientId: (clientId: string) => void;
25
26
  trackEvent(eventName: string, properties?: EventParams, services?: {
26
27
  ga?: boolean;
@@ -38,6 +39,8 @@ declare class Analytics {
38
39
  locale: string;
39
40
  phone?: string;
40
41
  isB2B?: boolean;
42
+ CIOParams?: EventParams;
43
+ mixpanelParams?: Dict;
41
44
  }): void;
42
45
  getCommonParams(): AnalyticsCommonParams;
43
46
  setLocale(newLocale: string): void;
package/dist/index.esm.js CHANGED
@@ -12129,35 +12129,39 @@ function getSearchQueryFromReferrer() {
12129
12129
  return null;
12130
12130
  }
12131
12131
  const getClientIdFromCookies = () => {
12132
- const clientId = api.get("_ga");
12133
- if (!clientId)
12134
- return "";
12135
- const splitedClientId = clientId.split(".");
12136
- return splitedClientId[2] + "." + splitedClientId[3];
12132
+ const ga = api.get("_ga");
12133
+ if (!ga)
12134
+ return null;
12135
+ const parts = ga.split(".");
12136
+ if (parts.length < 4)
12137
+ return null;
12138
+ return `${parts[2]}.${parts[3]}`;
12137
12139
  };
12138
12140
  const getGAClientId = () => {
12139
12141
  return new Promise((resolve) => {
12140
- if (typeof gtag !== "undefined") {
12142
+ let resolved = false;
12143
+ const fallback = () => {
12144
+ if (resolved)
12145
+ return;
12146
+ resolved = true;
12147
+ resolve(getClientIdFromCookies());
12148
+ };
12149
+ if (typeof window !== "undefined" && typeof gtag === "function") {
12141
12150
  try {
12142
- gtag("get", GOOGLE_ANALYTICS_ID, "client_id", function (clientId) {
12143
- if (clientId) {
12144
- resolve(clientId);
12145
- }
12146
- else {
12147
- resolve(getClientIdFromCookies());
12148
- }
12151
+ gtag("get", GOOGLE_ANALYTICS_ID, "client_id", (clientId) => {
12152
+ if (resolved)
12153
+ return;
12154
+ resolved = true;
12155
+ resolve(clientId || getClientIdFromCookies());
12149
12156
  });
12150
- setTimeout(() => {
12151
- resolve(getClientIdFromCookies());
12152
- }, 1000);
12157
+ setTimeout(fallback, 1000);
12153
12158
  }
12154
- catch (error) {
12155
- console.warn("Error getting client ID from gtag:", error);
12156
- resolve(getClientIdFromCookies());
12159
+ catch {
12160
+ fallback();
12157
12161
  }
12158
12162
  }
12159
12163
  else {
12160
- resolve(getClientIdFromCookies());
12164
+ fallback();
12161
12165
  }
12162
12166
  });
12163
12167
  };
@@ -12167,16 +12171,15 @@ function getGASessionId() {
12167
12171
  return null;
12168
12172
  const parts = cookieValue.split(".");
12169
12173
  const version = parts[0];
12170
- if (version === "GS1" && parts.length === 4) {
12171
- // GS1.1.1696433766.1696433857
12172
- return parts[2]; // session start timestamp
12174
+ // GS1.x.<sid>
12175
+ if (version === "GS1" && parts.length >= 3) {
12176
+ return parts[2];
12173
12177
  }
12174
- if (version === "GS2" && parts.length >= 3) {
12175
- // GS2.1.s1746513327$o1$g1$t1746513329$j58$l0$h0
12176
- const match = parts[2].match(/s(\d+)/);
12177
- if (match) {
12178
- return match[1]; // session ID (session start)
12179
- }
12178
+ // GS2.x.s<sid>$...
12179
+ if (version === "GS2") {
12180
+ const match = cookieValue.match(/s(\d+)/);
12181
+ if (match)
12182
+ return match[1];
12180
12183
  }
12181
12184
  return null;
12182
12185
  }
@@ -12816,12 +12819,18 @@ class Analytics {
12816
12819
  });
12817
12820
  }
12818
12821
  setUser(userId, userParams) {
12819
- const { email, name, phone, isB2B } = userParams;
12822
+ const { email, name, phone, isB2B, CIOParams = {}, mixpanelParams = {}, } = userParams;
12820
12823
  // Update config
12821
12824
  this.setConfig({ userId, email });
12822
12825
  // Mixpanel user
12823
12826
  mixpanel.identify(userId);
12824
- mixpanel.people.set({ $email: email, name, phone, isB2B });
12827
+ mixpanel.people.set({
12828
+ $email: email,
12829
+ name,
12830
+ phone,
12831
+ isB2B,
12832
+ ...mixpanelParams,
12833
+ });
12825
12834
  // GA user
12826
12835
  // gtag("config", GOOGLE_ANALYTICS_ID, {
12827
12836
  // user_properties: this.getCommonParams(),
@@ -12829,7 +12838,7 @@ class Analytics {
12829
12838
  // server_container_url: GA_SERVER_CONTAINER_URL,
12830
12839
  // });
12831
12840
  // Customer IO user
12832
- window.analytics.identify(userId, userParams);
12841
+ window.analytics.identify(userId, { ...userParams, ...CIOParams });
12833
12842
  // store user id in cookies
12834
12843
  setProfileId(userId);
12835
12844
  // store user email in cookies
@@ -12873,10 +12882,10 @@ class Analytics {
12873
12882
  }
12874
12883
  getUserId = getUserId;
12875
12884
  async getAttributionProperties(forcedAttributionProperties) {
12876
- const paramsFromCookie = this.getCommonParams();
12885
+ const gaClientId = await this.getGAClientId();
12877
12886
  const { listName, index } = getLastListNameAndIndex();
12878
12887
  const placement = localStorage.getItem(CREATE_THERAPY_PAGE_PLACEMENT_KEY);
12879
- const gaClientId = await this.getGAClientId();
12888
+ const paramsFromCookie = this.getCommonParams();
12880
12889
  return {
12881
12890
  funnelInfo: {
12882
12891
  funnelName: forcedAttributionProperties?.funnelName ||
package/dist/index.js CHANGED
@@ -12131,35 +12131,39 @@ function getSearchQueryFromReferrer() {
12131
12131
  return null;
12132
12132
  }
12133
12133
  const getClientIdFromCookies = () => {
12134
- const clientId = api.get("_ga");
12135
- if (!clientId)
12136
- return "";
12137
- const splitedClientId = clientId.split(".");
12138
- return splitedClientId[2] + "." + splitedClientId[3];
12134
+ const ga = api.get("_ga");
12135
+ if (!ga)
12136
+ return null;
12137
+ const parts = ga.split(".");
12138
+ if (parts.length < 4)
12139
+ return null;
12140
+ return `${parts[2]}.${parts[3]}`;
12139
12141
  };
12140
12142
  const getGAClientId = () => {
12141
12143
  return new Promise((resolve) => {
12142
- if (typeof gtag !== "undefined") {
12144
+ let resolved = false;
12145
+ const fallback = () => {
12146
+ if (resolved)
12147
+ return;
12148
+ resolved = true;
12149
+ resolve(getClientIdFromCookies());
12150
+ };
12151
+ if (typeof window !== "undefined" && typeof gtag === "function") {
12143
12152
  try {
12144
- gtag("get", GOOGLE_ANALYTICS_ID, "client_id", function (clientId) {
12145
- if (clientId) {
12146
- resolve(clientId);
12147
- }
12148
- else {
12149
- resolve(getClientIdFromCookies());
12150
- }
12153
+ gtag("get", GOOGLE_ANALYTICS_ID, "client_id", (clientId) => {
12154
+ if (resolved)
12155
+ return;
12156
+ resolved = true;
12157
+ resolve(clientId || getClientIdFromCookies());
12151
12158
  });
12152
- setTimeout(() => {
12153
- resolve(getClientIdFromCookies());
12154
- }, 1000);
12159
+ setTimeout(fallback, 1000);
12155
12160
  }
12156
- catch (error) {
12157
- console.warn("Error getting client ID from gtag:", error);
12158
- resolve(getClientIdFromCookies());
12161
+ catch {
12162
+ fallback();
12159
12163
  }
12160
12164
  }
12161
12165
  else {
12162
- resolve(getClientIdFromCookies());
12166
+ fallback();
12163
12167
  }
12164
12168
  });
12165
12169
  };
@@ -12169,16 +12173,15 @@ function getGASessionId() {
12169
12173
  return null;
12170
12174
  const parts = cookieValue.split(".");
12171
12175
  const version = parts[0];
12172
- if (version === "GS1" && parts.length === 4) {
12173
- // GS1.1.1696433766.1696433857
12174
- return parts[2]; // session start timestamp
12176
+ // GS1.x.<sid>
12177
+ if (version === "GS1" && parts.length >= 3) {
12178
+ return parts[2];
12175
12179
  }
12176
- if (version === "GS2" && parts.length >= 3) {
12177
- // GS2.1.s1746513327$o1$g1$t1746513329$j58$l0$h0
12178
- const match = parts[2].match(/s(\d+)/);
12179
- if (match) {
12180
- return match[1]; // session ID (session start)
12181
- }
12180
+ // GS2.x.s<sid>$...
12181
+ if (version === "GS2") {
12182
+ const match = cookieValue.match(/s(\d+)/);
12183
+ if (match)
12184
+ return match[1];
12182
12185
  }
12183
12186
  return null;
12184
12187
  }
@@ -12818,12 +12821,18 @@ class Analytics {
12818
12821
  });
12819
12822
  }
12820
12823
  setUser(userId, userParams) {
12821
- const { email, name, phone, isB2B } = userParams;
12824
+ const { email, name, phone, isB2B, CIOParams = {}, mixpanelParams = {}, } = userParams;
12822
12825
  // Update config
12823
12826
  this.setConfig({ userId, email });
12824
12827
  // Mixpanel user
12825
12828
  mixpanel.identify(userId);
12826
- mixpanel.people.set({ $email: email, name, phone, isB2B });
12829
+ mixpanel.people.set({
12830
+ $email: email,
12831
+ name,
12832
+ phone,
12833
+ isB2B,
12834
+ ...mixpanelParams,
12835
+ });
12827
12836
  // GA user
12828
12837
  // gtag("config", GOOGLE_ANALYTICS_ID, {
12829
12838
  // user_properties: this.getCommonParams(),
@@ -12831,7 +12840,7 @@ class Analytics {
12831
12840
  // server_container_url: GA_SERVER_CONTAINER_URL,
12832
12841
  // });
12833
12842
  // Customer IO user
12834
- window.analytics.identify(userId, userParams);
12843
+ window.analytics.identify(userId, { ...userParams, ...CIOParams });
12835
12844
  // store user id in cookies
12836
12845
  setProfileId(userId);
12837
12846
  // store user email in cookies
@@ -12875,10 +12884,10 @@ class Analytics {
12875
12884
  }
12876
12885
  getUserId = getUserId;
12877
12886
  async getAttributionProperties(forcedAttributionProperties) {
12878
- const paramsFromCookie = this.getCommonParams();
12887
+ const gaClientId = await this.getGAClientId();
12879
12888
  const { listName, index } = getLastListNameAndIndex();
12880
12889
  const placement = localStorage.getItem(CREATE_THERAPY_PAGE_PLACEMENT_KEY);
12881
- const gaClientId = await this.getGAClientId();
12890
+ const paramsFromCookie = this.getCommonParams();
12882
12891
  return {
12883
12892
  funnelInfo: {
12884
12893
  funnelName: forcedAttributionProperties?.funnelName ||