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.
npm i @simpleanalytics/nuxt
Set your website domain (as added in your Simple Analytics dashboard):
SIMPLE_ANALYTICS_HOSTNAME=example.com
Add the module to your nuxt.config.ts
and optionally set your hostname:
// nuxt.config.ts
export default defineNuxtConfig({
// ...
modules: ["@simpleanalytics/nuxt"],
simpleAnalytics: {
hostname: "example.com", // optional, if you don't use SIMPLE_ANALYTICS_HOSTNAME
},
});
The module uses the simple-analytics-vue
plugin to auto-inject the Simple Analytics script.
To track events programmatically, inject the saEvent
function.
<script setup>
import { inject } from "vue";
const saEvent = inject("saEvent");
// e.g.: send event when liking a comment
const likeComment = (comment) => {
saEvent(`comment_like_${comment.id}`);
};
</script>
Track page views or events during server side rendering or in API routes:
<script setup lang="ts">
if (import.meta.server) {
await trackPageview({
metadata: {
source: "some extra context",
},
});
}
</script>
// server/api/signup.post.ts
export default defineEventHandler(async (event) => {
await trackEvent("user_signup", {
event,
metadata: {
source: "registration_form",
user_type: "new",
},
});
// ...
});
Track a pageview on the server.
Parameters:
options
(object):hostname
(string): Your Simple Analytics hostnamemetadata
(object): Additional metadata to trackignoreMetrics
(object): Metrics to ignore for this pageviewcollectDnt
(boolean): Whether to collect data when DNT is enabledstrictUtm
(boolean): Whether to use strict UTM parameter parsing
Track a custom event on the server.
Parameters:
eventName
(string): Name of the event to trackoptions
(object):headers
(Headers): Request headershostname
(string): Your Simple Analytics hostnamemetadata
(object): Additional metadata to trackignoreMetrics
(object): Metrics to ignore for this eventcollectDnt
(boolean): Whether to collect data when DNT is enabled