|
| 1 | +--- |
| 2 | +title: Umami Analytics |
| 3 | +description: Use Umami Analytics in your Nuxt app. |
| 4 | +links: |
| 5 | + - label: Source |
| 6 | + icon: i-simple-icons-github |
| 7 | + to: https://github.com/nuxt/scripts/blob/main/src/runtime/registry/umami-analytics.ts |
| 8 | + size: xs |
| 9 | +--- |
| 10 | + |
| 11 | +[Umami](https://umami.is/) collects all the metrics you care about to help you make better decisions. |
| 12 | + |
| 13 | +The simplest way to load Umami Analytics globally in your Nuxt App is to use Nuxt config. Alternatively you can directly |
| 14 | +use the [useScriptUmamiAnalytics](#useScriptUmamiAnalytics) composable. |
| 15 | + |
| 16 | +## Loading Globally |
| 17 | + |
| 18 | +If you don't plan to send custom events you can use the [Environment overrides](https://nuxt.com/docs/getting-started/configuration#environment-overrides) to |
| 19 | +disable the script in development. |
| 20 | + |
| 21 | +::code-group |
| 22 | + |
| 23 | +```ts [Always enabled] |
| 24 | +export default defineNuxtConfig({ |
| 25 | + scripts: { |
| 26 | + registry: { |
| 27 | + umamiAnalytics: { |
| 28 | + websiteId: 'YOUR_WEBSITE_ID' |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | +}) |
| 33 | +``` |
| 34 | + |
| 35 | +```ts [Production only] |
| 36 | +export default defineNuxtConfig({ |
| 37 | + $production: { |
| 38 | + scripts: { |
| 39 | + registry: { |
| 40 | + umamiAnalytics: { |
| 41 | + websiteId: 'YOUR_WEBSITE_ID', |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +}) |
| 47 | +``` |
| 48 | + |
| 49 | +```ts [Environment Variables] |
| 50 | +export default defineNuxtConfig({ |
| 51 | + scripts: { |
| 52 | + registry: { |
| 53 | + umamiAnalytics: true, |
| 54 | + } |
| 55 | + }, |
| 56 | + // you need to provide a runtime config to access the environment variables |
| 57 | + runtimeConfig: { |
| 58 | + public: { |
| 59 | + scripts: { |
| 60 | + umamiAnalytics: { |
| 61 | + // .env |
| 62 | + // NUXT_PUBLIC_SCRIPTS_UMAMI_ANALYTICS_WEBSITE_ID=<your websiteId> |
| 63 | + websiteId: '' |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | +}) |
| 69 | +``` |
| 70 | + |
| 71 | +:: |
| 72 | + |
| 73 | +## useScriptUmamiAnalytics |
| 74 | + |
| 75 | +The `useScriptUmamiAnalytics` composable lets you have fine-grain control over when and how Umami Analytics is loaded on your site. |
| 76 | + |
| 77 | +```ts |
| 78 | +const plausible = useScriptUmamiAnalytics({ |
| 79 | + websiteId: 'YOUR_WEBSITE_ID' |
| 80 | +}) |
| 81 | +``` |
| 82 | + |
| 83 | +Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about advanced usage. |
| 84 | + |
| 85 | +### Self-hosted Umami |
| 86 | + |
| 87 | +If you are using a self-hosted version of Umami, you will need to provide an explicit src for the script so that |
| 88 | +the API events are sent to the correct endpoint. |
| 89 | + |
| 90 | +```ts |
| 91 | +useScriptUmamiAnalytics({ |
| 92 | + scriptInput: { |
| 93 | + src: 'https://my-self-hosted/script.js' |
| 94 | + } |
| 95 | +}) |
| 96 | +``` |
| 97 | + |
| 98 | +### UmamiAnalyticsApi |
| 99 | + |
| 100 | +```ts |
| 101 | +export interface UmamiAnalyticsApi { |
| 102 | + track: ((payload?: Record<string, any>) => void) &((event_name: string, event_data: Record<string, any>) => void) |
| 103 | + identify: (session_data?: Record<string, any>) => void |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### Config Schema |
| 108 | + |
| 109 | +You must provide the options when setting up the script for the first time. |
| 110 | + |
| 111 | +```ts |
| 112 | +export const UmamiAnalyticsOptions = object({ |
| 113 | + websiteId: string(), // required |
| 114 | + /** |
| 115 | + * By default, Umami will send data to wherever the script is located. |
| 116 | + * You can override this to send data to another location. |
| 117 | + */ |
| 118 | + hostUrl: optional(string()), |
| 119 | + /** |
| 120 | + * By default, Umami tracks all pageviews and events for you automatically. |
| 121 | + * You can disable this behavior and track events yourself using the tracker functions. |
| 122 | + * https://umami.is/docs/tracker-functions |
| 123 | + */ |
| 124 | + autoTrack: optional(boolean()), |
| 125 | + /** |
| 126 | + * If you want the tracker to only run on specific domains, you can add them to your tracker script. |
| 127 | + * This is a comma delimited list of domain names. |
| 128 | + * Helps if you are working in a staging/development environment. |
| 129 | + */ |
| 130 | + domains: optional(array(string())), |
| 131 | + /** |
| 132 | + * If you want the tracker to collect events under a specific tag. |
| 133 | + * Events can be filtered in the dashboard by a specific tag. |
| 134 | + */ |
| 135 | + tag: optional(string()), |
| 136 | +}) |
| 137 | +``` |
| 138 | + |
| 139 | +## Example |
| 140 | + |
| 141 | +Using Umami Analytics only in production while using `track` to send a conversion event. |
| 142 | + |
| 143 | +::code-group |
| 144 | + |
| 145 | +```vue [ConversionButton.vue] |
| 146 | +<script setup lang="ts"> |
| 147 | +const { track } = useScriptUmamiAnalytics() |
| 148 | +
|
| 149 | +// noop in development, ssr |
| 150 | +// just works in production, client |
| 151 | +track('event', { name: 'conversion-step' }) |
| 152 | +
|
| 153 | +function sendConversion() { |
| 154 | + track('event', { name: 'conversion' }) |
| 155 | +} |
| 156 | +</script> |
| 157 | +
|
| 158 | +<template> |
| 159 | + <div> |
| 160 | + <button @click="sendConversion"> |
| 161 | + Send Conversion |
| 162 | + </button> |
| 163 | + </div> |
| 164 | +</template> |
| 165 | +``` |
| 166 | + |
| 167 | +:: |
0 commit comments