Skip to content

Commit fce7505

Browse files
Update Nuxt integration documentation for Simple Analytics
1 parent b7bcc79 commit fce7505

File tree

1 file changed

+91
-47
lines changed

1 file changed

+91
-47
lines changed

_docs/31_integrations/00_nuxt.md

Lines changed: 91 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,127 @@
11
---
2-
title: Install Simple Analytics with Nuxt 3
2+
title: Install Simple Analytics with Nuxt
33
hidden: true
44
category: integrations
55
permalink: /install-simple-analytics-with-nuxt
6-
last_modified_at: 2023-03-03
6+
last_modified_at: 2025-10-24
77
---
88

9-
We have a package that works with both Nuxt 2 and Nuxt 3.
9+
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.
1010

11-
## Nuxt 3
11+
## Installation
1212

13-
Run this command to install Simple Analytics for Vue:
13+
```bash
14+
npm i @simpleanalytics/nuxt
15+
```
16+
17+
## Environment Variables
18+
19+
Set your website domain (as added in your [Simple Analytics dashboard](https://dashboard.simpleanalytics.com/)):
1420

1521
```bash
16-
npm install simple-analytics-vue --save-dev
22+
SIMPLE_ANALYTICS_HOSTNAME=example.com
1723
```
1824

19-
Create a file called `plugins/simpleanalytics.client.js`:
25+
> Omit `www.`, just the root domain or if you use a subdomain, include that. Like `sub.example.com`.
26+
27+
## Configuration
2028

21-
```js
22-
import SimpleAnalytics from "simple-analytics-vue";
29+
Add the module to your `nuxt.config.ts` and optionally set your hostname:
2330

24-
export default defineNuxtPlugin((nuxtApp) => {
25-
nuxtApp.vueApp.use(SimpleAnalytics);
31+
```ts
32+
// nuxt.config.ts
33+
export default defineNuxtConfig({
34+
// ...
35+
modules: ["@simpleanalytics/nuxt"],
36+
simpleAnalytics: {
37+
// hostname: "example.com", // optional, if you don't use SIMPLE_ANALYTICS_HOSTNAME
38+
},
2639
});
2740
```
2841

29-
That's it! Continue only if you want to go advanced.
42+
## Usage
3043

31-
If you want to skip your own visits on development:
44+
### Client-side analytics
3245

33-
```js
34-
import SimpleAnalytics from "simple-analytics-vue";
46+
The module uses the `simple-analytics-vue` plugin to auto-inject the Simple Analytics script.
3547

36-
export default defineNuxtPlugin((nuxtApp) => {
37-
nuxtApp.vueApp.use(SimpleAnalytics, {
38-
skip: process.env.NODE_ENV !== "production",
39-
});
40-
});
48+
### Tracking events in client components
49+
50+
To track events programmatically, inject the `saEvent` function.
51+
52+
```vue
53+
<script setup>
54+
import { inject } from "vue";
55+
56+
const saEvent = inject("saEvent");
57+
58+
// e.g.: send event when liking a comment
59+
const likeComment = (comment) => {
60+
saEvent(`comment_like_${comment.id}`);
61+
};
62+
</script>
4163
```
4264

43-
If you have a [custom domain for bypassing ad-blockers](/bypass-ad-blockers) set up, use this:
65+
### Server-side tracking (SSR & API)
4466

45-
```js
46-
import SimpleAnalytics from "simple-analytics-vue";
67+
Track page views or events during server side rendering or in API routes:
4768

48-
export default defineNuxtPlugin((nuxtApp) => {
49-
nuxtApp.vueApp.use(SimpleAnalytics, {
50-
skip: process.env.NODE_ENV !== "production",
51-
domain: "api.example.com"
69+
#### In server-rendered pages
70+
71+
```vue
72+
<script setup lang="ts">
73+
if (import.meta.server) {
74+
await trackPageview({
75+
metadata: {
76+
source: "some extra context",
77+
},
5278
});
53-
});
79+
}
80+
</script>
5481
```
5582

56-
## Nuxt 2
57-
58-
Run this command to install Simple Analytics for Vue:
83+
#### In Nitro API routes
84+
85+
```ts
86+
// server/api/signup.post.ts
87+
export default defineEventHandler(async (event) => {
88+
await trackEvent("user_signup", {
89+
event,
90+
metadata: {
91+
source: "registration_form",
92+
user_type: "new",
93+
},
94+
});
5995

60-
```bash
61-
npm install [email protected] --save-dev
96+
// ...
97+
});
6298
```
6399

64-
Create a file in your plugin folder with the name `simple-analytics.js`:
100+
## API Reference
65101

66-
```js
67-
// ~/plugins/simple-analytics.js
102+
### `trackPageview(options)`
68103

69-
import SimpleAnalytics from "simple-analytics-vue";
70-
import Vue from "vue";
104+
Track a pageview on the server.
71105

72-
Vue.use(SimpleAnalytics, { skip: process.env.NODE_ENV !== "production" });
73-
```
106+
**Parameters:**
74107

75-
Then on your `nuxt.config.js`, make sure to include the plugin with `ssr: false` as we only want to run it on the client:
108+
- `options` (object):
109+
- `hostname` (string): Your Simple Analytics hostname
110+
- `metadata` (object): Additional metadata to track
111+
- `ignoreMetrics` (object): Metrics to ignore for this pageview
112+
- `collectDnt` (boolean): Whether to collect data when DNT is enabled
113+
- `strictUtm` (boolean): Whether to use strict UTM parameter parsing
76114

77-
```js
78-
// nuxt.config.js
115+
### `trackEvent(eventName, options)`
79116

80-
export default {
81-
plugins: [{ src: "~/plugins/simple-analytics.js", ssr: false }],
82-
};
83-
```
117+
Track a custom event on the server.
118+
119+
**Parameters:**
120+
121+
- `eventName` (string): Name of the event to track
122+
- `options` (object):
123+
- `headers` (Headers): Request headers
124+
- `hostname` (string): Your Simple Analytics hostname
125+
- `metadata` (object): Additional metadata to track
126+
- `ignoreMetrics` (object): Metrics to ignore for this event
127+
- `collectDnt` (boolean): Whether to collect data when DNT is enabled

0 commit comments

Comments
 (0)