1- # Simple Analytics Nuxt Module
1+ # Simple Analytics for Nuxt
22
3- A Nuxt module for integrating Simple Analytics with server-side tracking capabilities .
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 .
44
5- ## Quick Setup
6-
7- Install the module to your Nuxt application:
5+ ## Installation
86
97``` bash
10- npm install @simpleanalytics/nuxt
8+ npm i @simpleanalytics/nuxt
9+ ```
10+
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
1117```
1218
13- Add the module to your ` nuxt.config.ts ` :
19+ ## Configuration
20+
21+ Add the module to your ` nuxt.config.ts ` and optionally set your hostname:
1422
1523``` ts
24+ // nuxt.config.ts
1625export default defineNuxtConfig ({
26+ // ...
1727 modules: [" @simpleanalytics/nuxt" ],
1828 simpleAnalytics: {
19- hostname: " your-domain.com" ,
20- enabled: true ,
21- proxy: true ,
29+ hostname: " example.com" , // optional, if you don't use SIMPLE_ANALYTICS_HOSTNAME
2230 },
2331});
2432```
2533
2634## Usage
2735
28- ### Server-side Pageview Tracking
36+ ### Client-side analytics
37+
38+ The module uses the ` simple-analytics-vue ` plugin to auto-inject the Simple Analytics script.
39+
40+ ### Tracking events in client components
2941
30- Track pageviews automatically on the server:
42+ To track events programmatically, inject the ` saEvent ` function.
3143
3244``` vue
3345<script setup>
34- // This will run on the server and track the pageview
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">
3565if (import.meta.server) {
3666 await trackPageview({
37- hostname: "your-domain.com",
3867 metadata: {
39- source: "homepage ",
68+ source: "some extra context ",
4069 },
4170 });
4271}
4372</script>
4473```
4574
46- ### Server-side Event Tracking
47-
48- Track custom events from API routes or server-side code:
75+ #### In Nitro API routes
4976
5077``` ts
51- // In a server API route
78+ // server/api/signup.post.ts
5279export default defineEventHandler (async (event ) => {
5380 await trackEvent (" user_signup" , {
5481 event ,
@@ -58,71 +85,10 @@ export default defineEventHandler(async (event) => {
5885 },
5986 });
6087
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- },
88+ // ...
11689});
11790```
11891
119- ### Environment Variables
120-
121- ``` bash
122- # Required: Your Simple Analytics hostname
123- SIMPLE_ANALYTICS_HOSTNAME=your-domain.com
124- ```
125-
12692## API Reference
12793
12894### ` trackPageview(options) `
@@ -151,18 +117,3 @@ Track a custom event on the server.
151117 - ` metadata ` (object): Additional metadata to track
152118 - ` ignoreMetrics ` (object): Metrics to ignore for this event
153119 - ` collectDnt ` (boolean): Whether to collect data when DNT is enabled
154-
155- ## License
156-
157- MIT License - see the [ LICENSE] ( LICENSE ) file for details.
158-
159- <!-- Badges -->
160-
161- [ npm-version-src ] : https://img.shields.io/npm/v/@simpleanalytics/nuxt/latest.svg?style=flat&colorA=020420&colorB=00DC82
162- [ npm-version-href ] : https://npmjs.com/package/@simpleanalytics/nuxt
163- [ npm-downloads-src ] : https://img.shields.io/npm/dm/@simpleanalytics/nuxt.svg?style=flat&colorA=020420&colorB=00DC82
164- [ npm-downloads-href ] : https://npm.chart.dev/@simpleanalytics/nuxt
165- [ license-src ] : https://img.shields.io/npm/l/@simpleanalytics/nuxt.svg?style=flat&colorA=020420&colorB=00DC82
166- [ license-href ] : https://npmjs.com/package/@simpleanalytics/nuxt
167- [ nuxt-src ] : https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js
168- [ nuxt-href ] : https://nuxt.com
0 commit comments