Nuxt Umami v2
Documentation for Nuxt Umami v2.
Still here? v3
is out, simpler and better.
Check out what's new and how to upgrade.
Setup
Step 1: Install and add to Nuxt
Install using your favorite package manager...
Then add nuxt-umami
to the extends
array in nuxt.config
:
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: easier to read and update)
appConfig
property
Environment Variables
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. Please note that provided env variables override appConfig
.
Step 3: Use it
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 |
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 freely. Please note that in <script setup>
, these functions might fail silently. Use the onMounted
hook or call them on user interaction.
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.
Method Return
Both umTrackEvent
and umTrackView
return a Promise with an ok
status that you can await or chain, Promise<{ok: boolean}>
.
Vue Directive
To use directive v-umami
, add useDirective: 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.
Prevent tracking yourself
To prevent tracking yourself, add the key umami.disabled
to your browser's localStorage. Set the value to 1.
See: tips.
FAQS and Quirks
- I don't see errors in live sites...
- If you're debugging live sites, set
debug: true
in your config.
- If you're debugging live sites, set
- Can I use Umami v2/Cloud?
- Yes. To use Umami v2, set
version: 2
in the Nuxt-Umami config.
- Yes. To use Umami v2, set
nuxt typecheck
fails! What can I do to resolve it?- The problem could be tied to
pnpm
's dependency hoisting. Thanks to Jeet for discovering this. Simply create a.npmrc
file in the root of your Nuxt project and addshamefully-hoist=true
. If that doesn't work, I'll be happy to look into it further.
- The problem could be tied to
- Welp, I am getting some CORS errors!
- Some adblockers like uBlock and Ghostery block Umami Cloud's endpoints. Try to disable your adblockers (yes, all of them). Also, double-check your config and Umami version. If all else fails, host your own Umami instance.
autoTrack
is not working?- The current implementation of
autoTrack
relies on<NuxtPage>
being present in your app. If you don't have<NuxtPage>
, you'd have to callumTrackView()
yourselfonMounted()
. See this issue.
- The current implementation of
- How do I set up my own Umami instance?
- Miracle Onyenma published a simple guide in his blog. Check it out.
- Should I sponsor this project?
- Absolutely, you can do that here.