|
1 | 1 | --- |
2 | | -title: Install Simple Analytics with Nuxt 3 |
| 2 | +title: Install Simple Analytics with Nuxt |
3 | 3 | hidden: true |
4 | 4 | category: integrations |
5 | 5 | permalink: /install-simple-analytics-with-nuxt |
6 | | -last_modified_at: 2023-03-03 |
| 6 | +last_modified_at: 2025-10-24 |
7 | 7 | --- |
8 | 8 |
|
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. |
10 | 10 |
|
11 | | -## Nuxt 3 |
| 11 | +## Installation |
12 | 12 |
|
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/)): |
14 | 20 |
|
15 | 21 | ```bash |
16 | | -npm install simple-analytics-vue --save-dev |
| 22 | +SIMPLE_ANALYTICS_HOSTNAME=example.com |
17 | 23 | ``` |
18 | 24 |
|
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 |
20 | 28 |
|
21 | | -```js |
22 | | -import SimpleAnalytics from "simple-analytics-vue"; |
| 29 | +Add the module to your `nuxt.config.ts` and optionally set your hostname: |
23 | 30 |
|
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 | + }, |
26 | 39 | }); |
27 | 40 | ``` |
28 | 41 |
|
29 | | -That's it! Continue only if you want to go advanced. |
| 42 | +## Usage |
30 | 43 |
|
31 | | -If you want to skip your own visits on development: |
| 44 | +### Client-side analytics |
32 | 45 |
|
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. |
35 | 47 |
|
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> |
41 | 63 | ``` |
42 | 64 |
|
43 | | -If you have a [custom domain for bypassing ad-blockers](/bypass-ad-blockers) set up, use this: |
| 65 | +### Server-side tracking (SSR & API) |
44 | 66 |
|
45 | | -```js |
46 | | -import SimpleAnalytics from "simple-analytics-vue"; |
| 67 | +Track page views or events during server side rendering or in API routes: |
47 | 68 |
|
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 | + }, |
52 | 78 | }); |
53 | | -}); |
| 79 | +} |
| 80 | +</script> |
54 | 81 | ``` |
55 | 82 |
|
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 | + }); |
59 | 95 |
|
60 | | -```bash |
61 | | -npm install [email protected] --save-dev |
| 96 | + // ... |
| 97 | +}); |
62 | 98 | ``` |
63 | 99 |
|
64 | | -Create a file in your plugin folder with the name `simple-analytics.js`: |
| 100 | +## API Reference |
65 | 101 |
|
66 | | -```js |
67 | | -// ~/plugins/simple-analytics.js |
| 102 | +### `trackPageview(options)` |
68 | 103 |
|
69 | | -import SimpleAnalytics from "simple-analytics-vue"; |
70 | | -import Vue from "vue"; |
| 104 | +Track a pageview on the server. |
71 | 105 |
|
72 | | -Vue.use(SimpleAnalytics, { skip: process.env.NODE_ENV !== "production" }); |
73 | | -``` |
| 106 | +**Parameters:** |
74 | 107 |
|
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 |
76 | 114 |
|
77 | | -```js |
78 | | -// nuxt.config.js |
| 115 | +### `trackEvent(eventName, options)` |
79 | 116 |
|
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