@grainql/analytics-web 2.5.3 → 2.5.4

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/index.js CHANGED
@@ -417,10 +417,46 @@ class GrainAnalytics {
417
417
  }
418
418
  }
419
419
  formatEvent(event) {
420
+ const properties = event.properties || {};
421
+ // Auto-enrich events with session-level attribution properties
422
+ // This ensures UTM parameters and attribution data are available on ALL events, not just page_view
423
+ if (!this.config.disableAutoProperties && typeof window !== 'undefined') {
424
+ const hasConsent = this.consentManager.hasConsent('analytics');
425
+ // Only enrich if not a system event (they handle their own properties)
426
+ const isSystemEvent = event.eventName.startsWith('_grain_');
427
+ if (!isSystemEvent && hasConsent) {
428
+ // Get session UTM parameters
429
+ const sessionUTMs = (0, attribution_1.getSessionUTMParameters)();
430
+ if (sessionUTMs) {
431
+ if (sessionUTMs.utm_source)
432
+ properties.utm_source = sessionUTMs.utm_source;
433
+ if (sessionUTMs.utm_medium)
434
+ properties.utm_medium = sessionUTMs.utm_medium;
435
+ if (sessionUTMs.utm_campaign)
436
+ properties.utm_campaign = sessionUTMs.utm_campaign;
437
+ if (sessionUTMs.utm_term)
438
+ properties.utm_term = sessionUTMs.utm_term;
439
+ if (sessionUTMs.utm_content)
440
+ properties.utm_content = sessionUTMs.utm_content;
441
+ }
442
+ // Get first-touch attribution
443
+ const firstTouch = (0, attribution_1.getFirstTouchAttribution)(this.config.tenantId);
444
+ if (firstTouch) {
445
+ properties.first_touch_source = firstTouch.source;
446
+ properties.first_touch_medium = firstTouch.medium;
447
+ properties.first_touch_campaign = firstTouch.campaign;
448
+ properties.first_touch_referrer_category = firstTouch.referrer_category;
449
+ }
450
+ // Add session ID if not already present
451
+ if (!properties.session_id) {
452
+ properties.session_id = this.getSessionId();
453
+ }
454
+ }
455
+ }
420
456
  return {
421
457
  eventName: event.eventName,
422
458
  userId: event.userId || this.getEffectiveUserIdInternal(),
423
- properties: event.properties || {},
459
+ properties,
424
460
  };
425
461
  }
426
462
  async getAuthHeaders() {
package/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@ import { setCookie, getCookie, areCookiesEnabled } from './cookies.js';
7
7
  import { ActivityDetector } from './activity.js';
8
8
  import { HeartbeatManager } from './heartbeat.js';
9
9
  import { PageTrackingManager } from './page-tracking.js';
10
- import { categorizeReferrer, parseUTMParameters, getOrCreateFirstTouchAttribution, getSessionUTMParameters, } from './attribution.js';
10
+ import { categorizeReferrer, parseUTMParameters, getOrCreateFirstTouchAttribution, getSessionUTMParameters, getFirstTouchAttribution, } from './attribution.js';
11
11
  export { categorizeReferrer, parseUTMParameters };
12
12
  // Re-export timezone-country utilities
13
13
  export { getCountry, getCountryCodeFromTimezone, getState } from './countries.js';
@@ -376,10 +376,46 @@ export class GrainAnalytics {
376
376
  }
377
377
  }
378
378
  formatEvent(event) {
379
+ const properties = event.properties || {};
380
+ // Auto-enrich events with session-level attribution properties
381
+ // This ensures UTM parameters and attribution data are available on ALL events, not just page_view
382
+ if (!this.config.disableAutoProperties && typeof window !== 'undefined') {
383
+ const hasConsent = this.consentManager.hasConsent('analytics');
384
+ // Only enrich if not a system event (they handle their own properties)
385
+ const isSystemEvent = event.eventName.startsWith('_grain_');
386
+ if (!isSystemEvent && hasConsent) {
387
+ // Get session UTM parameters
388
+ const sessionUTMs = getSessionUTMParameters();
389
+ if (sessionUTMs) {
390
+ if (sessionUTMs.utm_source)
391
+ properties.utm_source = sessionUTMs.utm_source;
392
+ if (sessionUTMs.utm_medium)
393
+ properties.utm_medium = sessionUTMs.utm_medium;
394
+ if (sessionUTMs.utm_campaign)
395
+ properties.utm_campaign = sessionUTMs.utm_campaign;
396
+ if (sessionUTMs.utm_term)
397
+ properties.utm_term = sessionUTMs.utm_term;
398
+ if (sessionUTMs.utm_content)
399
+ properties.utm_content = sessionUTMs.utm_content;
400
+ }
401
+ // Get first-touch attribution
402
+ const firstTouch = getFirstTouchAttribution(this.config.tenantId);
403
+ if (firstTouch) {
404
+ properties.first_touch_source = firstTouch.source;
405
+ properties.first_touch_medium = firstTouch.medium;
406
+ properties.first_touch_campaign = firstTouch.campaign;
407
+ properties.first_touch_referrer_category = firstTouch.referrer_category;
408
+ }
409
+ // Add session ID if not already present
410
+ if (!properties.session_id) {
411
+ properties.session_id = this.getSessionId();
412
+ }
413
+ }
414
+ }
379
415
  return {
380
416
  eventName: event.eventName,
381
417
  userId: event.userId || this.getEffectiveUserIdInternal(),
382
- properties: event.properties || {},
418
+ properties,
383
419
  };
384
420
  }
385
421
  async getAuthHeaders() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grainql/analytics-web",
3
- "version": "2.5.3",
3
+ "version": "2.5.4",
4
4
  "description": "Lightweight TypeScript SDK for sending analytics events and managing remote configurations via Grain's REST API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",