Nuxt Umami
Integrate Umami Analytics into your Nuxt websites / applications.
Features
- ๐ Open Source
- โจ SSR Support, of course
- โ No extra script: no loading delay, instant availability
- ๐ Escapes most ad & script blockers (catch me if you can)
- ๐ฏ Simple, feature complete, extensive config
- โ Typescript, JSDocs, auto completion
- โ
Easy debuggin' (one
console.log
at a time) - โ Auto imported, available (almsot) everywhere
!IMPORTANT Nuxt Umami v2 uses features that are only available in Nuxt 3 (Nuxt Layers). Check out Nuxt Umami v1 for Nuxt 2 compat.
Setup
๐ Try it online
Step 1: Install and add to Nuxt
Install using your favorite package manager...
pnpm add nuxt-umami #pnpm
npm install nuxt-umami #npm
Then add nuxt-umami
to your extends
array in nuxt.config
:
defineNuxtConfig({
extends: ['nuxt-umami']
});
Or, you can totally skip the installation process and do
defineNuxtConfig({
extends: ['github:ijkml/nuxt-umami']
});
Step 2: Configure Umami
You can provide configuration options using the app.config.ts
file or appConfig
property of the Nuxt config.
app.config.ts
file
(recommended for readability and ease of update)
export default defineAppConfig({
umami: {
// ...umami config here
},
});
appConfig
property
defineNuxtConfig({
extends: ['nuxt-umami'],
appConfig: {
umami: {
// ...umami config here
},
},
});
Environment Variables
!NOTE Available in
^2.1.0
and takes precedence overappConfig
.
You can provide the host
and id
config (only) as environment variables. Simply add NUXT_PUBLIC_UMAMI_HOST
and NUXT_PUBLIC_UMAMI_ID
to your .env
file, and that's it.
NUXT_PUBLIC_UMAMI_HOST="https://domain.tld"
NUXT_PUBLIC_UMAMI_ID="abc123-456def-ghi789"
Step 3:
Use it
<script setup>
function complexCalc() {
// ... do something
umTrackEvent('complex-btn', { propA: 1, propB: 'two', propC: false });
}
</script>
<template>
<button @click="umTrackEvent('button-1')">
Button 1
</button>
<button @click="complexCalc">
Button 2
</button>
</template>
Configuration
option | type | description | required | default |
---|---|---|---|---|
host | string | Your Umami endpoint. This is where your script is hosted. Eg: https://ijkml.xyz/ . | yes | '' |
id | string | Unique website-id provided by Umami. | yes | '' |
domains | string | Array<string> | Limit tracker to specific domains by providing an array or comma-separated list (without 'http'). Leave blank for all domains. | no | undefined |
ignoreDnt | boolean | Option to ignore browsers' Do Not Track setting. | no | true |
autoTrack | boolean | Option to automatically track page views. | no | true |
ignoreLocalhost | boolean | Option to prevent tracking on localhost. | no | false |
customEndpoint | string | Set a custom COLLECT_API_ENDPOINT . See Docs. | no | undefined |
version | 1 | 2 | Umami version | no | 1 |
useDirective | boolean | Option to enable the v-umami directive. See below. | no | false |
debug | boolean | Option to enable error logs (in production), useful for testing in prod :) | no | false |
Usage
Two functions are auto-imported, umTrackView()
and umTrackEvent()
. Use them however and wherever you like. These functions work even in <script setup>
without the onMounted
hook. The v-umami
directive can be enabled in the config.
Available Methods
umTrackView(url, referrer)
url
: the path being tracked, eg/about
,/contact?by=phone#office
. This can be correctly inferred. Equivalent ofrouter.fullPath
.referrer
: the page referrer. This can be correctly inferred. Equivalent ofdocument.referrer
or theref
search param in the url (eg:example.com/?ref=thereferrer
).
umTrackEvent(eventName, eventData)
eventName
: a string type texteventData
: an object in the format{key: value}
, wherekey
is a string andvalue
is a string, number, or boolean.
Reference: Umami Tracker Functions.
Vue Directive
!NOTE Available from
^2.5.0
. AdduseDirective: true
to your config.
You can pass a string as the event name, or an object containing a name
property (required, this is the event name). Every other property will be passed on as event data.
<button v-umami="'Event-Name'">
Event Button
</button>
<button v-umami="{name: 'Event-Name'}">
as object
</button>
<button v-umami="{name: 'Event-Name', position: 'left', ...others}">
with event details
</button>
Live Debugging
For cases where you need that console.log('here')
in live sites, set debug: true
in your config.
Umami v2
To use Umami v2, set version: 2
in the Umami config.
Setup guide
Learn how to host your own Umami instance and set up your Nuxt app using Miracle Onyenma's simple guide.
Issues, Bugs, Ideas?
Open an issue. Contributions are welcome, just send a PR! If you encounter any issues, don't hesitate to open an issue. I'm always available to help and resolve any bugs.