You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+151-5Lines changed: 151 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,7 @@ If a visitor arrives at a website that uses the Nuxt UTM module and a UTM parame
25
25
-**📍 UTM Tracking**: Easily capture UTM parameters to gain insights into traffic sources and campaign performance.
26
26
-**🔍 Intelligent De-duplication**: Smart recognition of page refreshes to avoid data duplication, ensuring each visit is uniquely accounted for.
27
27
-**🔗 Comprehensive Data Collection**: Alongside UTM parameters, gather additional context such as referrer details, user agent, landing page url, browser language, and screen resolution. This enriched data empowers your marketing strategies with a deeper understanding of campaign impact.
28
+
-**🔌 Hooks & Extensibility**: Three runtime hooks (`utm:before-track`, `utm:before-persist`, `utm:tracked`) let you skip tracking, enrich data with custom parameters, or trigger side effects after tracking completes.
28
29
29
30
## Quick Setup
30
31
@@ -86,6 +87,9 @@ const utm = useNuxtUTM()
86
87
// - enableTracking(): Enable UTM tracking
87
88
// - disableTracking(): Disable UTM tracking
88
89
// - clearData(): Clear all stored UTM data
90
+
// - onBeforeTrack(cb): Hook called before data collection
91
+
// - onBeforePersist(cb): Hook called to enrich/modify collected data before saving
92
+
// - onTracked(cb): Hook called after data is saved
@@ -195,6 +195,9 @@ The `data` property contains an array of UTM parameters collected. Each element
195
195
"gclidParams": {
196
196
"gclid": "CjklsefawEFRfeafads",
197
197
"gad_source": "1"
198
+
},
199
+
"customParams": {
200
+
"fbclid": "abc123"
198
201
}
199
202
}
200
203
]
@@ -210,6 +213,149 @@ Each entry provides a `timestamp` indicating when the UTM parameters were collec
210
213
-**Data Clearing**: Ability to completely remove all collected data
211
214
-**Session Management**: Automatically manages sessions to avoid duplicate tracking
212
215
216
+
### Hooks
217
+
218
+
The module provides three runtime hooks that let you extend the tracking pipeline. You can use them to skip tracking, enrich data with custom parameters, or trigger side effects after tracking completes. Hooks can be registered via a Nuxt plugin or through the `useNuxtUTM` composable.
219
+
220
+
This keeps your tracking strategy flexible: enrich once in your app, then forward the same enriched payload wherever you need it.
Use `utm:before-persist` to enrich every tracked event with a normalized `pageCategory`. This pattern is useful when you want one internal taxonomy that can be reused across your app and backend.
Called before any data collection begins. The handler receives a `BeforeTrackContext` object with `route`, `query`, and a `skip` flag. Set `skip = true` to prevent tracking for the current page visit.
324
+
325
+
```typescript
326
+
nuxtApp.hook('utm:before-track', (context) => {
327
+
// context.route - the current route object
328
+
// context.query - the current URL query parameters
329
+
// context.skip - set to true to skip tracking
330
+
})
331
+
```
332
+
333
+
#### Hook: `utm:before-persist`
334
+
335
+
Called after the `DataObject` is built but before it is checked for duplicates and saved. The handler receives the `DataObject` directly and can mutate it to add or modify fields. This is the primary hook for adding `customParams`.
336
+
337
+
```typescript
338
+
nuxtApp.hook('utm:before-persist', (data) => {
339
+
// Add any custom tracking parameters
340
+
data.customParams= {
341
+
...data.customParams,
342
+
myCustomField: 'value',
343
+
}
344
+
})
345
+
```
346
+
347
+
> Note: `customParams` are not included in the de-duplication check. Only UTM parameters, GCLID parameters, and session ID are compared.
348
+
349
+
#### Hook: `utm:tracked`
350
+
351
+
Called after data is saved to localStorage. The handler receives the final `DataObject`. Use this for side effects like sending data to a backend or triggering analytics events.
0 commit comments