@reservamos/browser-analytics 1.0.1 → 1.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reservamos/browser-analytics",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/reservamos/reservamos-browser-analytics.git"
package/src/track.ts CHANGED
@@ -12,6 +12,13 @@ import validator from '@/services/validator';
12
12
  */
13
13
  const FP_TRIGGER_EVENTS = ['Search', 'View Results'];
14
14
 
15
+ const EXTRA_UTM_FIELDS = {
16
+ utm_brand: 'UTM Brand',
17
+ utm_kxconfig: 'UTM KXConfig',
18
+ gad_source: 'GAD Source',
19
+ gclid: 'GCLID',
20
+ };
21
+
15
22
  /**
16
23
  * Simplifies the structure of event data by flattening nested objects and arrays.
17
24
  * @param {object} data - The event data to simplify.
@@ -81,11 +88,13 @@ export async function trackEvent(
81
88
  const defaultProperties = {
82
89
  'User Fingerprint': fingerprint,
83
90
  };
91
+ const trackingParams = extractTrackingParams();
84
92
 
85
93
  const simplifiedData = flattenEventData(eventProperties);
86
94
  const properties = {
87
95
  ...defaultProperties,
88
96
  ...simplifiedData,
97
+ ...trackingParams,
89
98
  ...meta,
90
99
  };
91
100
 
@@ -94,3 +103,26 @@ export async function trackEvent(
94
103
  console.error(`Error tracking event '${eventName}':`, error);
95
104
  }
96
105
  }
106
+
107
+ /**
108
+ * Extracts UTM and other tracking parameters from the current URL that match predefined fields.
109
+ * @returns {Record<string, string>} An object containing matched tracking parameters.
110
+ */
111
+ function extractTrackingParams(): Record<string, string> {
112
+ try {
113
+ const urlParams = new URLSearchParams(window.location.search);
114
+ const trackingData: Record<string, string> = {};
115
+
116
+ Object.entries(EXTRA_UTM_FIELDS).forEach(([queryParam, displayName]) => {
117
+ const value = urlParams.get(queryParam);
118
+ if (value) {
119
+ trackingData[displayName] = value;
120
+ }
121
+ });
122
+
123
+ return trackingData;
124
+ } catch (error) {
125
+ console.error('Error extracting tracking parameters:', error);
126
+ return {};
127
+ }
128
+ }