Skip to content

Commit 59d0350

Browse files
committed
Add option to enable/disable the module
1 parent e5e3fa5 commit 59d0350

File tree

6 files changed

+114
-15
lines changed

6 files changed

+114
-15
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,33 @@ That's it! You can now use Nuxt UTM in your Nuxt app ✨
5353

5454
## Usage
5555

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+
enabled: true, // defaults to true
65+
},
66+
});
67+
```
68+
69+
#### Options
70+
71+
- `enabled`: Boolean (default: `true`) - Controls whether UTM tracking is active. When set to `false`, the module won't track any UTM parameters or add any functionality to your app.
72+
73+
### Accessing UTM Data
74+
75+
You can use `useNuxtUTM` composable to access the UTM object:
5776

5877
```vue
5978
<script setup>
6079
const utm = useNuxtUTM();
6180
</script>
6281
```
82+
6383
> Remember: You don't need to import the composable because nuxt imports it automatically.
6484
6585
Alternatively, you can get the UTM information through the Nuxt App with the following instructions:
@@ -106,7 +126,7 @@ In the `$utm` array, each entry provides a `timestamp` indicating when the UTM p
106126

107127
### Devenv
108128

109-
You can take advantage of [devenv.sh](https://devenv.sh) to quickly create the development environment for this this project. Use it in combination with [direnv](https://direnv.net/) to quickly load all the environment while navigating into the project directory in your shell.
129+
You can take advantage of [devenv.sh](https://devenv.sh) to quickly create the development environment for this this project. Use it in combination with [direnv](https://direnv.net/) to quickly load all the environment while navigating into the project directory in your shell.
110130

111131
```bash
112132
# Install dependencies

playground/nuxt.config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
export default defineNuxtConfig({
2+
app: {
3+
head: {
4+
title: "Nuxt UTM Playground",
5+
},
6+
},
27
modules: ["../src/module"],
3-
utm: {},
8+
utm: {
9+
enabled: true,
10+
},
411
devtools: { enabled: true },
512
});

src/module.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
import { defineNuxtModule, addPlugin, addImports, createResolver } from "@nuxt/kit";
1+
import {
2+
defineNuxtModule,
3+
addPlugin,
4+
addImports,
5+
createResolver,
6+
} from "@nuxt/kit";
27

3-
// Module options TypeScript interface definition
4-
export interface ModuleOptions {}
8+
export interface ModuleOptions {
9+
enabled: boolean;
10+
}
511

612
export default defineNuxtModule<ModuleOptions>({
713
meta: {
814
name: "utm",
915
configKey: "utm",
1016
},
11-
// Default configuration options of the Nuxt module
12-
defaults: {},
13-
setup() {
17+
defaults: {
18+
enabled: true,
19+
},
20+
setup(options) {
1421
const resolver = createResolver(import.meta.url);
1522

16-
// Do not add the extension since the `.ts` will be transpiled to `.mjs` after `npm run prepack`
17-
addPlugin(resolver.resolve("./runtime/plugin"));
18-
addImports({
19-
name: 'useNuxtUTM',
20-
from: resolver.resolve('runtime/composables'),
21-
})
23+
if (options.enabled) {
24+
// Do not add the extension since the `.ts` will be transpiled to `.mjs` after `npm run prepack`
25+
addPlugin(resolver.resolve("./runtime/plugin"));
26+
addImports({
27+
name: "useNuxtUTM",
28+
from: resolver.resolve("runtime/composables"),
29+
});
30+
}
2231
},
2332
});

test/fixtures/disabled/app.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<div>
3+
<h1>UTM Tracker</h1>
4+
<pre>{{ $utm }}</pre>
5+
</div>
6+
</template>
7+
8+
<script setup>
9+
import { useNuxtApp } from "nuxt/app";
10+
11+
const { $utm } = useNuxtApp();
12+
</script>

test/fixtures/disabled/nuxt.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import UtmModule from "../../../src/module";
2+
3+
export default defineNuxtConfig({
4+
modules: [UtmModule],
5+
utm: {
6+
enabled: false,
7+
},
8+
});

test/integration.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,46 @@ describe("ssr", async () => {
113113
});
114114
});
115115
});
116+
117+
describe("Module configuration", () => {
118+
describe("when enabled", async () => {
119+
await setup({
120+
rootDir: fileURLToPath(new URL("./fixtures/basic", import.meta.url)),
121+
server: true,
122+
browser: true,
123+
setupTimeout: 60000,
124+
build: true,
125+
});
126+
127+
it("should track UTM parameters", async () => {
128+
const page = await createPage(
129+
"/?utm_source=test_source&utm_medium=test_medium"
130+
);
131+
const rawData = await page.evaluate(() =>
132+
window.localStorage.getItem("nuxt-utm-data")
133+
);
134+
const entries = JSON.parse(rawData ?? "[]");
135+
expect(entries.length).toBeGreaterThan(0);
136+
});
137+
});
138+
139+
describe("when disabled", async () => {
140+
await setup({
141+
rootDir: fileURLToPath(new URL("./fixtures/disabled", import.meta.url)),
142+
server: true,
143+
browser: true,
144+
setupTimeout: 60000,
145+
build: true,
146+
});
147+
148+
it("should not track UTM parameters", async () => {
149+
const page = await createPage(
150+
"/?utm_source=test_source&utm_medium=test_medium"
151+
);
152+
const rawData = await page.evaluate(() =>
153+
window.localStorage.getItem("nuxt-utm-data")
154+
);
155+
expect(rawData).toBeNull();
156+
});
157+
});
158+
});

0 commit comments

Comments
 (0)