|
| 1 | +--- |
| 2 | +title: Clarity |
| 3 | +description: Use Clarity 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/clarity.ts |
| 8 | + size: xs |
| 9 | +--- |
| 10 | + |
| 11 | +[Clarity](https://clarity.microsoft.com/) by Microsoft is a screen recorder and heatmap tool that helps you understand how users interact with your website. |
| 12 | + |
| 13 | +Nuxt Scripts provides a registry script composable `useScriptClarity` to easily integrate Clarity in your Nuxt app. |
| 14 | + |
| 15 | +### Nuxt Config Setup |
| 16 | + |
| 17 | +The simplest way to load Clarity globally in your Nuxt App is to use Nuxt config. Alternatively you can directly |
| 18 | +use the [useScriptClarity](#useScriptClarity) composable. |
| 19 | + |
| 20 | +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 |
| 21 | +disable the script in development. |
| 22 | + |
| 23 | +::code-group |
| 24 | + |
| 25 | +```ts [Always enabled] |
| 26 | +export default defineNuxtConfig({ |
| 27 | + scripts: { |
| 28 | + registry: { |
| 29 | + clarity: { |
| 30 | + id: 'YOUR_ID' |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | +}) |
| 35 | +``` |
| 36 | + |
| 37 | +```ts [Production only] |
| 38 | +export default defineNuxtConfig({ |
| 39 | + $production: { |
| 40 | + scripts: { |
| 41 | + registry: { |
| 42 | + clarity: { |
| 43 | + id: 'YOUR_ID', |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +}) |
| 49 | +``` |
| 50 | + |
| 51 | +:: |
| 52 | + |
| 53 | +#### With Environment Variables |
| 54 | + |
| 55 | +If you prefer to configure your id using environment variables. |
| 56 | + |
| 57 | +```ts [nuxt.config.ts] |
| 58 | +export default defineNuxtConfig({ |
| 59 | + scripts: { |
| 60 | + registry: { |
| 61 | + clarity: true, |
| 62 | + } |
| 63 | + }, |
| 64 | + // you need to provide a runtime config to access the environment variables |
| 65 | + runtimeConfig: { |
| 66 | + public: { |
| 67 | + scripts: { |
| 68 | + clarity: { |
| 69 | + id: '', // NUXT_PUBLIC_SCRIPTS_CLARITY_ID |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | +}) |
| 75 | +``` |
| 76 | + |
| 77 | +```text [.env] |
| 78 | +NUXT_PUBLIC_SCRIPTS_CLARITY_ID=<YOUR_ID> |
| 79 | +``` |
| 80 | + |
| 81 | +## useScriptClarity |
| 82 | + |
| 83 | +The `useScriptClarity` composable lets you have fine-grain control over when and how Clarity is loaded on your site. |
| 84 | + |
| 85 | +```ts |
| 86 | +const { clarity } = useScriptClarity({ |
| 87 | + id: 'YOUR_ID' |
| 88 | +}) |
| 89 | +// example |
| 90 | +clarity("identify", "custom-id", "custom-session-id", "custom-page-id", "friendly-name") |
| 91 | +``` |
| 92 | + |
| 93 | +Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about advanced usage. |
| 94 | + |
| 95 | +### ClarityApi |
| 96 | + |
| 97 | +```ts |
| 98 | +type ClarityFunctions = ((fn: 'start', options: { content: boolean, cookies: string[], dob: number, expire: number, projectId: string, upload: string }) => void) |
| 99 | + & ((fn: 'identify', id: string, session?: string, page?: string, userHint?: string) => Promise<{ |
| 100 | + id: string |
| 101 | + session: string |
| 102 | + page: string |
| 103 | + userHint: string |
| 104 | +}>) |
| 105 | + & ((fn: 'consent') => void) |
| 106 | + & ((fn: 'set', key: any, value: any) => void) |
| 107 | + & ((fn: 'event', value: any) => void) |
| 108 | + & ((fn: 'upgrade', upgradeReason: any) => void) |
| 109 | + & ((fn: string, ...args: any[]) => void) |
| 110 | + |
| 111 | +export interface ClarityApi { |
| 112 | + clarity: ClarityFunctions & { |
| 113 | + q: any[] |
| 114 | + v: string |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +``` |
| 119 | + |
| 120 | +### Config Schema |
| 121 | + |
| 122 | +You must provide the options when setting up the script for the first time. |
| 123 | + |
| 124 | +```ts |
| 125 | +export const ClarityOptions = object({ |
| 126 | + /** |
| 127 | + * The Clarity token. |
| 128 | + */ |
| 129 | + id: pipe(string(), minLength(10)), |
| 130 | +}) |
| 131 | +``` |
| 132 | + |
| 133 | +## Example |
| 134 | + |
| 135 | +Using Clarity only in production while using `clarity` to send a conversion event. |
| 136 | + |
| 137 | +::code-group |
| 138 | + |
| 139 | +```vue [ConversionButton.vue] |
| 140 | +<script setup> |
| 141 | +const { clarity } = useScriptClarity() |
| 142 | +
|
| 143 | +// noop in development, ssr |
| 144 | +// just works in production, client |
| 145 | +function sendConversion() { |
| 146 | + clarity('event', 'conversion') |
| 147 | +} |
| 148 | +</script> |
| 149 | +
|
| 150 | +<template> |
| 151 | + <div> |
| 152 | + <button @click="sendConversion"> |
| 153 | + Send Conversion |
| 154 | + </button> |
| 155 | + </div> |
| 156 | +</template> |
| 157 | +``` |
| 158 | + |
| 159 | +```ts [nuxt.config.ts Mock development] |
| 160 | +import { isDevelopment } from 'std-env' |
| 161 | + |
| 162 | +export default defineNuxtConfig({ |
| 163 | + scripts: { |
| 164 | + registry: { |
| 165 | + clarity: isDevelopment |
| 166 | + ? 'mock' // script won't load unless manually callined load() |
| 167 | + : { |
| 168 | + id: 'YOUR_ID', |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | +}) |
| 173 | +``` |
| 174 | + |
| 175 | +:: |
0 commit comments