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
2516export 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
6535if (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
7952export 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
96130Track 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.
0 commit comments