Skip to content

Commit eeec84d

Browse files
committed
feat: add script settings support
1 parent ce2a469 commit eeec84d

File tree

16 files changed

+2642
-6194
lines changed

16 files changed

+2642
-6194
lines changed

examples/nuxt-v3/middleware/tracking-middleware.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,5 @@ export default defineNuxtRouteMiddleware(async (to) => {
44
return;
55
}
66

7-
if (to.path.startsWith("/api/")) {
8-
return;
9-
}
10-
117
await trackPageview();
128
});

examples/nuxt-v4/app/middleware/tracking-middleware.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,5 @@ export default defineNuxtRouteMiddleware(async (to) => {
44
return;
55
}
66

7-
if (to.path.startsWith("/api/")) {
8-
return;
9-
}
10-
117
await trackPageview();
128
});

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"format": "prettier --write \"**/*.{ts,tsx,md}\""
1212
},
1313
"devDependencies": {
14-
"@changesets/cli": "^2.29.5",
15-
"turbo": "^2.5.5"
14+
"turbo": "^2.5.6"
1615
},
1716
"packageManager": "[email protected]"
1817
}

packages/nuxt/README.md

Lines changed: 117 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,54 @@
1-
# Simple Analytics for Nuxt
1+
# Simple Analytics Nuxt Module
22

3-
This Nuxt module provides a simple way to add privacy-friendly pageview and event tracking using Simple Analytics to your Nuxt 3 or 4 application.
3+
A Nuxt module for integrating Simple Analytics with server-side tracking capabilities.
44

5-
## Installation
5+
## Quick Setup
6+
7+
Install the module to your Nuxt application:
68

79
```bash
8-
npm i @simpleanalytics/nuxt
10+
npm install @simpleanalytics/nuxt
911
```
1012

11-
## Environment Variables
12-
13-
Set your website domain (as added in your [Simple Analytics dashboard](https://dashboard.simpleanalytics.com/)):
14-
15-
```txt
16-
SIMPLE_ANALYTICS_HOSTNAME=example.com
17-
```
18-
19-
## Configuration
20-
21-
Add the module to your `nuxt.config.ts` and optionally set your hostname:
13+
Add the module to your `nuxt.config.ts`:
2214

2315
```ts
24-
// nuxt.config.ts
2516
export default defineNuxtConfig({
26-
// ...
2717
modules: ["@simpleanalytics/nuxt"],
2818
simpleAnalytics: {
29-
hostname: "example.com", // optional, if you don't use SIMPLE_ANALYTICS_HOSTNAME
19+
hostname: "your-domain.com",
20+
enabled: true,
21+
proxy: true,
3022
},
3123
});
3224
```
3325

3426
## Usage
3527

36-
### Client-side analytics
37-
38-
The module uses the `simple-analytics-vue` plugin to auto-inject the Simple Analytics script.
28+
### Server-side Pageview Tracking
3929

40-
### Tracking events in client components
41-
42-
To track events programmatically, inject the `saEvent` function.
30+
Track pageviews automatically on the server:
4331

4432
```vue
4533
<script setup>
46-
import { inject } from "vue";
47-
48-
const saEvent = inject("saEvent");
49-
50-
// e.g.: send event when liking a comment
51-
const likeComment = (comment) => {
52-
saEvent(`comment_like_${comment.id}`);
53-
};
54-
</script>
55-
```
56-
57-
### Server-side tracking (SSR & API)
58-
59-
Track page views or events during server side rendering or in API routes:
60-
61-
#### In server-rendered pages
62-
63-
```vue
64-
<script setup lang="ts">
34+
// This will run on the server and track the pageview
6535
if (import.meta.server) {
6636
await trackPageview({
37+
hostname: "your-domain.com",
6738
metadata: {
68-
source: "some extra context",
39+
source: "homepage",
6940
},
7041
});
7142
}
7243
</script>
7344
```
7445

75-
#### In Nitro API routes
46+
### Server-side Event Tracking
47+
48+
Track custom events from API routes or server-side code:
7649

7750
```ts
78-
// server/api/signup.post.ts
51+
// In a server API route
7952
export default defineEventHandler(async (event) => {
8053
await trackEvent("user_signup", {
8154
event,
@@ -85,24 +58,80 @@ export default defineEventHandler(async (event) => {
8558
},
8659
});
8760

88-
// ...
61+
return { success: true };
62+
});
63+
```
64+
65+
## Configuration
66+
67+
### Module Options
68+
69+
```ts
70+
export default defineNuxtConfig({
71+
simpleAnalytics: {
72+
// Your Simple Analytics hostname
73+
hostname: "your-domain.com",
74+
75+
// Enable/disable the module
76+
enabled: true,
77+
78+
// Enable/disable proxy
79+
proxy: true,
80+
81+
// Auto-collect events
82+
autoCollect: true,
83+
84+
// Collect data even when DNT is enabled
85+
collectDnt: false,
86+
87+
// Dashboard mode
88+
mode: "dash",
89+
90+
// Ignore specific metrics
91+
ignoreMetrics: {
92+
referrer: false,
93+
utm: false,
94+
country: false,
95+
session: false,
96+
timeonpage: false,
97+
scrolled: false,
98+
useragent: false,
99+
screensize: false,
100+
viewportsize: false,
101+
language: false,
102+
},
103+
104+
// Ignore specific pages
105+
ignorePages: ["/admin", "/private"],
106+
107+
// Allow specific URL parameters
108+
allowParams: ["ref", "source"],
109+
110+
// Non-unique parameters
111+
nonUniqueParams: ["utm_source"],
112+
113+
// Strict UTM parameter parsing
114+
strictUtm: true,
115+
},
89116
});
90117
```
91118

92-
## API Reference
119+
### Environment Variables
120+
121+
```bash
122+
# Required: Your Simple Analytics hostname
123+
SIMPLE_ANALYTICS_HOSTNAME=your-domain.com
124+
```
125+
126+
## API Reference (Nuxt)
93127

94128
### `trackPageview(options)`
95129

96130
Track a pageview on the server.
97131

98132
**Parameters:**
99133

100-
- `options` (object):
101-
- `hostname` (string): Your Simple Analytics hostname
102-
- `metadata` (object): Additional metadata to track
103-
- `ignoreMetrics` (object): Metrics to ignore for this pageview
104-
- `collectDnt` (boolean): Whether to collect data when DNT is enabled
105-
- `strictUtm` (boolean): Whether to use strict UTM parameter parsing
134+
- `options` (object): Additional metadata to track (optional)
106135

107136
### `trackEvent(eventName, options)`
108137

@@ -111,9 +140,41 @@ Track a custom event on the server.
111140
**Parameters:**
112141

113142
- `eventName` (string): Name of the event to track
143+
- `options` (object): Additional metadata to track (option)
144+
145+
## API Reference (Nitro)
146+
147+
### `trackPageview(requestEvent, options)`
148+
149+
Track a pageview on the server.
150+
151+
**Parameters:**
152+
153+
- `options` (object): Additional metadata to track (optional)
154+
155+
### `trackEvent(requestEvent, eventName, options)`
156+
157+
Track a custom event on the server.
158+
159+
**Parameters:**
160+
114161
- `options` (object):
115-
- `headers` (Headers): Request headers
116162
- `hostname` (string): Your Simple Analytics hostname
117163
- `metadata` (object): Additional metadata to track
118-
- `ignoreMetrics` (object): Metrics to ignore for this event
164+
- `ignoreMetrics` (object): Metrics to ignore for this pageview
119165
- `collectDnt` (boolean): Whether to collect data when DNT is enabled
166+
- `strictUtm` (boolean): Whether to use strict UTM parameter parsing
167+
168+
## Migration to v2.0
169+
170+
## Do Not Track (DNT)
171+
172+
### Client-side analytics
173+
174+
### Server-side analytics
175+
176+
## Ignoring metrics To also record DNT visitors you can add data-collect-dnt="true" to the script tag
177+
178+
## License
179+
180+
MIT License - see the [LICENSE](LICENSE) file for details.

packages/nuxt/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,21 @@
3737
},
3838
"dependencies": {
3939
"@nuxt/kit": "^4.0.3",
40-
"simple-analytics-vue": "^3.0.3"
40+
"simple-analytics-vue": "^3.1.0"
4141
},
4242
"devDependencies": {
43-
"@nuxt/devtools": "^2.6.2",
44-
"@nuxt/eslint-config": "^1.8.0",
43+
"@nuxt/devtools": "^2.6.3",
44+
"@nuxt/eslint-config": "^1.9.0",
4545
"@nuxt/module-builder": "^1.0.2",
4646
"@nuxt/schema": "^4.0.3",
4747
"@nuxt/test-utils": "^3.19.2",
48-
"@types/node": "^24.2.1",
48+
"@types/node": "^24.3.0",
4949
"changelogen": "^0.6.2",
50-
"eslint": "^9.33.0",
50+
"eslint": "^9.34.0",
5151
"nuxt": "^4",
5252
"typescript": "~5.9.2",
5353
"vitest": "^3.2.4",
54-
"vue-tsc": "^3.0.5"
54+
"vue-tsc": "^3.0.5",
55+
"vue": "^3"
5556
}
5657
}

packages/nuxt/src/module.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
addServerImports,
77
addPlugin,
88
} from "@nuxt/kit";
9-
import type { SimpleAnalyticsOptions } from "./runtime/server/lib/options";
9+
import type { SimpleAnalyticsOptions } from "./runtime/interfaces";
1010

1111
export interface ModuleOptions extends SimpleAnalyticsOptions {
1212
enabled?: boolean;
@@ -39,23 +39,25 @@ export default defineNuxtModule<ModuleOptions>({
3939

4040
addPlugin(resolver.resolve("./runtime/plugin"));
4141

42+
// Server rendering imports
4243
addImports([
4344
{
4445
name: "trackEvent",
45-
from: resolver.resolve("./runtime/track-event"),
46+
from: resolver.resolve("./runtime/server/track-event"),
4647
meta: {
4748
description: "Track a custom event with Simple Analytics",
4849
},
4950
},
5051
{
5152
name: "trackPageview",
52-
from: resolver.resolve("./runtime/track-pageview"),
53+
from: resolver.resolve("./runtime/server/track-pageview"),
5354
meta: {
5455
description: "Track a pageview with Simple Analytics",
5556
},
5657
},
5758
]);
5859

60+
// Nitro imports
5961
addServerImports([
6062
{
6163
name: "trackEvent",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
export type AnalyticsMetadata =
22
| Record<string, string | boolean | number | Date>
33
| undefined;
4+
5+
export interface SimpleAnalyticsOptions {
6+
autoCollect?: boolean;
7+
collectDnt?: boolean;
8+
domain?: string;
9+
hostname?: string;
10+
mode?: "dash";
11+
ignoreMetrics?: {
12+
referrer?: boolean;
13+
utm?: boolean;
14+
country?: boolean;
15+
session?: boolean;
16+
timeonpage?: boolean;
17+
scrolled?: boolean;
18+
useragent?: boolean;
19+
screensize?: boolean;
20+
viewportsize?: boolean;
21+
language?: boolean;
22+
};
23+
ignorePages?: string[];
24+
allowParams?: string[];
25+
nonUniqueParams?: string[];
26+
strictUtm?: boolean;
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { H3Event } from "h3";
2+
3+
export interface NitroContext {
4+
event: H3Event;
5+
}

packages/nuxt/src/runtime/nitro/track-event.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ import {
55
isEnhancedBotDetectionEnabled,
66
} from "../server/lib/utils";
77
import { parseHeaders } from "../server/lib/headers";
8-
import type { H3Event } from "h3";
98
import { useRuntimeConfig } from "#imports";
10-
11-
type NitroContext = {
12-
event: H3Event;
13-
};
9+
import type { NitroContext } from "./interface";
1410

1511
type TrackEventOptions = TrackingOptions & NitroContext;
1612

@@ -30,11 +26,7 @@ export async function trackEvent(
3026
return;
3127
}
3228

33-
const headers = options.event.headers;
34-
35-
if (!headers) {
36-
return;
37-
}
29+
const { headers } = options.event;
3830

3931
if (isDoNotTrackEnabled(headers) && !options?.collectDnt) {
4032
return;

0 commit comments

Comments
 (0)