|
10 | 10 |
|
11 | 11 | --- |
12 | 12 |
|
13 | | -A Nuxt 3 module for tracking UTM parameters. |
| 13 | +A Nuxt 3/4 module for tracking UTM parameters. |
14 | 14 |
|
15 | 15 | - [✨ Release Notes](/CHANGELOG.md) |
16 | 16 | <!-- - [🏀 Online playground](https://stackblitz.com/github/stackbuilders/nuxt-utm?file=playground%2Fapp.vue) --> |
@@ -53,26 +53,122 @@ That's it! You can now use Nuxt UTM in your Nuxt app ✨ |
53 | 53 |
|
54 | 54 | ## Usage |
55 | 55 |
|
56 | | -You can use `useNuxtUTM` composable to access the UTM object: |
| 56 | +### Configuration |
| 57 | + |
| 58 | +You can configure the module by passing options in your `nuxt.config.ts`: |
| 59 | + |
| 60 | +```js |
| 61 | +export default defineNuxtConfig({ |
| 62 | + modules: ['nuxt-utm'], |
| 63 | + utm: { |
| 64 | + trackingEnabled: true, // defaults to true - initial tracking state |
| 65 | + }, |
| 66 | +}) |
| 67 | +``` |
| 68 | + |
| 69 | +#### Options |
| 70 | + |
| 71 | +- `trackingEnabled`: Boolean (default: `true`) - Sets the initial state for UTM tracking. This can be changed at runtime. |
| 72 | + |
| 73 | +### Runtime Tracking Control |
| 74 | + |
| 75 | +The module provides runtime control over tracking, perfect for implementing cookie consent banners or user privacy preferences. |
| 76 | + |
| 77 | +#### Using the Composable |
57 | 78 |
|
58 | 79 | ```vue |
59 | 80 | <script setup> |
60 | 81 | const utm = useNuxtUTM() |
| 82 | +
|
| 83 | +// The composable returns: |
| 84 | +// - data: Reactive array of collected UTM data |
| 85 | +// - trackingEnabled: Reactive boolean indicating if tracking is active |
| 86 | +// - enableTracking(): Enable UTM tracking |
| 87 | +// - disableTracking(): Disable UTM tracking |
| 88 | +// - clearData(): Clear all stored UTM data |
61 | 89 | </script> |
62 | 90 | ``` |
63 | 91 |
|
64 | | -> Remember: You don't need to import the composable because nuxt imports it automatically. |
| 92 | +#### Example: Cookie Banner Integration |
| 93 | + |
| 94 | +```vue |
| 95 | +<template> |
| 96 | + <div v-if="showBanner" class="cookie-banner"> |
| 97 | + <p>We use tracking to improve your experience.</p> |
| 98 | + <button @click="acceptTracking">Accept</button> |
| 99 | + <button @click="rejectTracking">Reject</button> |
| 100 | + </div> |
| 101 | +</template> |
65 | 102 |
|
66 | | -Alternatively, you can get the UTM information through the Nuxt App with the following instructions: |
| 103 | +<script setup> |
| 104 | +import { ref } from 'vue' |
| 105 | +const utm = useNuxtUTM() |
| 106 | +const showBanner = ref(!utm.trackingEnabled.value) |
| 107 | +
|
| 108 | +const acceptTracking = () => { |
| 109 | + utm.enableTracking() |
| 110 | + showBanner.value = false |
| 111 | +} |
| 112 | +
|
| 113 | +const rejectTracking = () => { |
| 114 | + utm.disableTracking() |
| 115 | + utm.clearData() // Optional: clear any existing data |
| 116 | + showBanner.value = false |
| 117 | +} |
| 118 | +</script> |
| 119 | +``` |
| 120 | + |
| 121 | +#### Privacy Controls |
| 122 | + |
| 123 | +```vue |
| 124 | +<template> |
| 125 | + <div class="privacy-settings"> |
| 126 | + <h3>Privacy Settings</h3> |
| 127 | + <label> |
| 128 | + <input |
| 129 | + type="checkbox" |
| 130 | + :checked="utm.trackingEnabled.value" |
| 131 | + @change="toggleTracking" |
| 132 | + /> |
| 133 | + Enable UTM tracking |
| 134 | + </label> |
| 135 | + <button @click="utm.clearData" v-if="utm.data.value.length > 0"> |
| 136 | + Clear tracking data ({{ utm.data.value.length }} entries) |
| 137 | + </button> |
| 138 | + </div> |
| 139 | +</template> |
| 140 | +
|
| 141 | +<script setup> |
| 142 | +const utm = useNuxtUTM() |
| 143 | +
|
| 144 | +const toggleTracking = (event) => { |
| 145 | + if (event.target.checked) { |
| 146 | + utm.enableTracking() |
| 147 | + } else { |
| 148 | + utm.disableTracking() |
| 149 | + } |
| 150 | +} |
| 151 | +</script> |
| 152 | +``` |
| 153 | + |
| 154 | +### Accessing UTM Data |
| 155 | + |
| 156 | +You can use `useNuxtUTM` composable to access the UTM data: |
67 | 157 |
|
68 | 158 | ```vue |
69 | 159 | <script setup> |
70 | | -import { useNuxtApp } from 'nuxt/app' |
71 | | -const { $utm } = useNuxtApp() |
| 160 | +const utm = useNuxtUTM() |
| 161 | +
|
| 162 | +// Access the collected data |
| 163 | +console.log(utm.data.value) |
72 | 164 | </script> |
73 | 165 | ``` |
74 | 166 |
|
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 |
| 167 | +> Remember: You don't need to import the composable because Nuxt imports it automatically. |
| 168 | +
|
| 169 | +### Data Structure |
| 170 | + |
| 171 | +The `data` property contains an array of UTM parameters collected. Each element in the array represents a set of UTM parameters collected from a URL visit, and is structured as follows |
76 | 172 |
|
77 | 173 | ```json |
78 | 174 | [ |
@@ -104,7 +200,15 @@ Regardless of the option you choose to use the module, the `utm' object will con |
104 | 200 | ] |
105 | 201 | ``` |
106 | 202 |
|
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. |
| 203 | +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. |
| 204 | + |
| 205 | +### Key Features |
| 206 | + |
| 207 | +- **Runtime Control**: Enable/disable tracking dynamically based on user consent |
| 208 | +- **Privacy Friendly**: Respects user preferences and provides clear data management |
| 209 | +- **Persistent Preferences**: Tracking preferences are saved and persist across sessions |
| 210 | +- **Data Clearing**: Ability to completely remove all collected data |
| 211 | +- **Session Management**: Automatically manages sessions to avoid duplicate tracking |
108 | 212 |
|
109 | 213 | ## Development |
110 | 214 |
|
|
0 commit comments