|
1 | 1 | # Nuxt UTM |
2 | 2 |
|
3 | | -[](https://github.com/stackbuilders/nuxt-utm/actions/workflows/main.yml) |
4 | 3 | [![npm version][npm-version-src]][npm-version-href] |
5 | 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] |
6 | 5 | [![License][license-src]][license-href] |
7 | 6 | [![Nuxt][nuxt-src]][nuxt-href] |
| 7 | +[](https://github.com/stackbuilders/nuxt-utm/actions/workflows/main.yml) |
8 | 8 |
|
9 | | -**Built in collaboration with The Durst Organization** |
10 | | - |
11 | | ---- |
12 | | - |
13 | | -A Nuxt 3 module for tracking UTM parameters. |
| 9 | +A [Nuxt 3](https://nuxt.com) module for tracking UTM parameters. |
14 | 10 |
|
15 | 11 | - [✨ Release Notes](/CHANGELOG.md) |
16 | | - <!-- - [🏀 Online playground](https://stackblitz.com/github/stackbuilders/nuxt-utm?file=playground%2Fapp.vue) --> |
17 | | - <!-- - [📖 Documentation](https://example.com) --> |
18 | | - |
19 | | -## How it works / motivation / purpose |
20 | | - |
21 | | -If a visitor arrives at a website that uses the Nuxt UTM module and a UTM parameter is present in the URL, the module will collect the UTM parameters along with additional information. This information is saved in the device's local storage within the user's browser. This is especially useful for static generated websites that can later integrate with the backend to save this data. For example, when a visitor or lead submits a form, you can send this data alongside the form data. Later, this information can be especially useful for evaluating the effectiveness of ad campaigns and assessing their impact. |
| 12 | +<!-- - [🏀 Online playground](https://stackblitz.com/github/your-org/my-module?file=playground%2Fapp.vue) --> |
| 13 | +<!-- - [📖 Documentation](https://example.com) --> |
22 | 14 |
|
23 | 15 | ## Features |
24 | 16 |
|
25 | | -- **📍 UTM Tracking**: Easily capture UTM parameters to gain insights into traffic sources and campaign performance. |
26 | | -- **🔍 Intelligent De-duplication**: Smart recognition of page refreshes to avoid data duplication, ensuring each visit is uniquely accounted for. |
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. |
| 17 | +- ✅ Automatically capture UTM parameters from URL query strings |
| 18 | +- ✅ Store UTM data persistently in localStorage with session tracking |
| 19 | +- ✅ Support for Google Ads parameters (gclid, gad_source) |
| 20 | +- ✅ Additional context (referrer, user agent, screen size, landing page) |
| 21 | +- ✅ TypeScript support |
| 22 | +- ✅ **Enable/disable tracking via configuration or runtime** |
| 23 | +- ✅ Composable API for easy access |
28 | 24 |
|
29 | 25 | ## Quick Setup |
30 | 26 |
|
31 | 27 | 1. Add `nuxt-utm` dependency to your project |
32 | 28 |
|
33 | 29 | ```bash |
34 | | -# Using pnpm |
35 | | -pnpm add -D nuxt-utm |
| 30 | +# Using npm |
| 31 | +npm install --save-dev nuxt-utm |
36 | 32 |
|
37 | 33 | # Using yarn |
38 | 34 | yarn add --dev nuxt-utm |
39 | 35 |
|
40 | | -# Using npm |
41 | | -npm install --save-dev nuxt-utm |
| 36 | +# Using pnpm |
| 37 | +pnpm add -D nuxt-utm |
42 | 38 | ``` |
43 | 39 |
|
44 | 40 | 2. Add `nuxt-utm` to the `modules` section of `nuxt.config.ts` |
45 | 41 |
|
46 | 42 | ```js |
47 | 43 | export default defineNuxtConfig({ |
48 | 44 | modules: ['nuxt-utm'], |
| 45 | + utm: { |
| 46 | + enabled: true, // Set to false to disable capturing UTM parameters |
| 47 | + }, |
49 | 48 | }) |
50 | 49 | ``` |
51 | 50 |
|
52 | | -That's it! You can now use Nuxt UTM in your Nuxt app ✨ |
| 51 | +## Configuration |
| 52 | + |
| 53 | +### Module Options |
| 54 | + |
| 55 | +| Option | Type | Default | Description | |
| 56 | +|--------|------|---------|-------------| |
| 57 | +| `enabled` | `boolean` | `true` | Enable or disable UTM parameter tracking | |
| 58 | + |
| 59 | +Example configurations: |
| 60 | + |
| 61 | +```js |
| 62 | +// Disable UTM tracking completely |
| 63 | +export default defineNuxtConfig({ |
| 64 | + modules: ['nuxt-utm'], |
| 65 | + utm: { |
| 66 | + enabled: false, |
| 67 | + }, |
| 68 | +}) |
| 69 | + |
| 70 | +// Enable UTM tracking (default behavior) |
| 71 | +export default defineNuxtConfig({ |
| 72 | + modules: ['nuxt-utm'], |
| 73 | + utm: { |
| 74 | + enabled: true, |
| 75 | + }, |
| 76 | +}) |
| 77 | +``` |
53 | 78 |
|
54 | 79 | ## Usage |
55 | 80 |
|
| 81 | +### Basic Usage |
| 82 | + |
56 | 83 | You can use `useNuxtUTM` composable to access the UTM object: |
57 | 84 |
|
58 | 85 | ```vue |
59 | 86 | <script setup> |
60 | | -const utm = useNuxtUTM() |
| 87 | +const { data, enabled, clear } = useNuxtUTM() |
| 88 | +
|
| 89 | +// Access UTM data |
| 90 | +console.log(data.value) // Array of UTM data objects |
| 91 | +
|
| 92 | +// Check if tracking is enabled |
| 93 | +console.log(enabled.value) // boolean |
| 94 | +
|
| 95 | +// Disable tracking at runtime (e.g., when user opts out) |
| 96 | +enabled.value = false |
| 97 | +
|
| 98 | +// Re-enable tracking |
| 99 | +enabled.value = true |
| 100 | +
|
| 101 | +// Clear all UTM data |
| 102 | +clear() |
61 | 103 | </script> |
62 | 104 | ``` |
63 | 105 |
|
64 | | -> Remember: You don't need to import the composable because nuxt imports it automatically. |
| 106 | +### Alternative Access via Nuxt App |
65 | 107 |
|
66 | | -Alternatively, you can get the UTM information through the Nuxt App with the following instructions: |
| 108 | +Alternatively, you can get the UTM information through the Nuxt App: |
67 | 109 |
|
68 | 110 | ```vue |
69 | 111 | <script setup> |
70 | 112 | import { useNuxtApp } from 'nuxt/app' |
71 | 113 | const { $utm } = useNuxtApp() |
| 114 | +const { data, enabled, clear } = $utm |
| 115 | +
|
| 116 | +// Disable tracking |
| 117 | +$utm.enabled.value = false |
| 118 | +
|
| 119 | +// Clear all data |
| 120 | +$utm.clear() |
72 | 121 | </script> |
73 | 122 | ``` |
74 | 123 |
|
75 | | -Regardless of the option you choose to use the module, the `utm' object will contain an array of UTM parameters collected for use. Each element in the array represents a set of UTM parameters collected from a URL visit, and is structured as follows |
| 124 | +### Runtime Tracking Control |
| 125 | + |
| 126 | +The module provides runtime control over UTM tracking, which is useful for implementing user consent flows: |
| 127 | + |
| 128 | +```vue |
| 129 | +<template> |
| 130 | + <div> |
| 131 | + <h1>Privacy Settings</h1> |
| 132 | + <label> |
| 133 | + <input |
| 134 | + type="checkbox" |
| 135 | + v-model="trackingEnabled" |
| 136 | + @change="handleTrackingToggle" |
| 137 | + > |
| 138 | + Allow UTM tracking |
| 139 | + </label> |
| 140 | + |
| 141 | + <div v-if="trackingEnabled"> |
| 142 | + <h2>Current UTM Data</h2> |
| 143 | + <pre>{{ utmData }}</pre> |
| 144 | + </div> |
| 145 | + </div> |
| 146 | +</template> |
| 147 | +
|
| 148 | +<script setup> |
| 149 | +const { data: utmData, enabled, clear } = useNuxtUTM() |
| 150 | +
|
| 151 | +const trackingEnabled = ref(enabled.value) |
| 152 | +
|
| 153 | +const handleTrackingToggle = () => { |
| 154 | + enabled.value = trackingEnabled.value |
| 155 | + |
| 156 | + // Optionally clear existing data when disabled |
| 157 | + if (!trackingEnabled.value) { |
| 158 | + clear() |
| 159 | + } |
| 160 | +} |
| 161 | +</script> |
| 162 | +``` |
| 163 | + |
| 164 | +## Data Structure |
| 165 | + |
| 166 | +Regardless of the option you choose to use the module, the `data` will contain an array of UTM parameters collected for use. Each element in the array represents a set of UTM parameters collected from a URL visit, and is structured as follows: |
76 | 167 |
|
77 | 168 | ```json |
78 | 169 | [ |
@@ -104,7 +195,60 @@ Regardless of the option you choose to use the module, the `utm' object will con |
104 | 195 | ] |
105 | 196 | ``` |
106 | 197 |
|
107 | | -In the `$utm` array, each entry provides a `timestamp` indicating when the UTM parameters were collected, the `utmParams` object containing the UTM parameters, `additionalInfo` object with more context about the visit, and a `sessionId` to differentiate visits in different sessions. |
| 198 | +In the `data` array, each entry provides: |
| 199 | +- **timestamp**: When the UTM parameters were collected |
| 200 | +- **utmParams**: The UTM parameters from the URL |
| 201 | +- **additionalInfo**: Context about the visit (referrer, user agent, etc.) |
| 202 | +- **sessionId**: Unique identifier for the user session |
| 203 | +- **gclidParams**: Google Ads click identifier and source (if present) |
| 204 | + |
| 205 | +## Common Use Cases |
| 206 | + |
| 207 | +### GDPR/Privacy Compliance |
| 208 | + |
| 209 | +```vue |
| 210 | +<script setup> |
| 211 | +const { enabled, clear } = useNuxtUTM() |
| 212 | +
|
| 213 | +// Check user's consent from your privacy management system |
| 214 | +const hasConsent = await checkUserConsent() |
| 215 | +enabled.value = hasConsent |
| 216 | +
|
| 217 | +// Listen for consent changes |
| 218 | +onConsentChange((newConsent) => { |
| 219 | + enabled.value = newConsent |
| 220 | + if (!newConsent) { |
| 221 | + // Clear existing UTM data using the built-in clear function |
| 222 | + clear() |
| 223 | + } |
| 224 | +}) |
| 225 | +</script> |
| 226 | +``` |
| 227 | + |
| 228 | +### Conditional Tracking by Environment |
| 229 | + |
| 230 | +```js |
| 231 | +// nuxt.config.ts |
| 232 | +export default defineNuxtConfig({ |
| 233 | + modules: ['nuxt-utm'], |
| 234 | + utm: { |
| 235 | + // Only enable in production |
| 236 | + enabled: process.env.NODE_ENV === 'production', |
| 237 | + }, |
| 238 | +}) |
| 239 | +``` |
| 240 | + |
| 241 | +### A/B Testing with Tracking Control |
| 242 | + |
| 243 | +```vue |
| 244 | +<script setup> |
| 245 | +const { enabled } = useNuxtUTM() |
| 246 | +
|
| 247 | +// Enable tracking only for certain user segments |
| 248 | +const userSegment = await getUserSegment() |
| 249 | +enabled.value = userSegment === 'tracking-enabled' |
| 250 | +</script> |
| 251 | +``` |
108 | 252 |
|
109 | 253 | ## Development |
110 | 254 |
|
@@ -160,5 +304,5 @@ Do you want to contribute to this project? Please take a look at our [contributi |
160 | 304 |
|
161 | 305 | --- |
162 | 306 |
|
163 | | -<img src="https://www.stackbuilders.com/media/images/Sb-supports.original.png" alt="Stack Builders" width="50%"></img> |
| 307 | +<img src="https://www.stackbuilders.com/media/images/Sb-supports.original.png" alt="Stack Builders" width="50%"></img> |
164 | 308 | [Check out our libraries](https://github.com/stackbuilders/) | [Join our team](https://www.stackbuilders.com/join-us/) |
0 commit comments