Clarity
Clarity by Microsoft is a screen recorder and heatmap tool that helps you understand how users interact with your website.
Script Stats
Nuxt Config Setup
The simplest way to load Clarity globally in your Nuxt App is to use Nuxt config. Alternatively you can directly use the useScriptClarity composable.
export default defineNuxtConfig({
scripts: {
registry: {
clarity: {
id: 'xxxxxxxxxx'
}
}
}
})useScriptClarity
The useScriptClarity composable lets you have fine-grain control over when and how Clarity is loaded on your site.
const { proxy } = useScriptClarity()
proxy.clarity('event', 'conversion')Please follow the Registry Scripts guide to learn more about advanced usage.
First-Party Mode
This script supports First-Party Mode which routes all traffic through your domain for improved privacy and ad blocker bypass.
export default defineNuxtConfig({
scripts: {
firstParty: true,
registry: {
clarity: { id: 'xxxxxxxxxx'}
}
}
})To opt-out for this specific script:
useScriptClarity({
id: 'xxxxxxxxxx',
scriptOptions: {
firstParty: false
}
})Example
Using Clarity only in production while using the proxy to send events.
<script setup lang="ts">
const { proxy } = useScriptClarity()
// noop in development, ssr
// just works in production, client
function handleAction() {
proxy.clarity('event', 'conversion')
}
</script>
<template>
<div>
<button @click="handleAction">
Send Event
</button>
</div>
</template>ClarityApi
type ClarityFunctions = ((fn: 'start', options: { content: boolean, cookies: string[], dob: number, expire: number, projectId: string, upload: string }) => void)
& ((fn: 'identify', id: string, session?: string, page?: string, userHint?: string) => Promise<{
id: string
session: string
page: string
userHint: string
}>)
& ((fn: 'consent') => void)
& ((fn: 'set', key: any, value: any) => void)
& ((fn: 'event', value: any) => void)
& ((fn: 'upgrade', upgradeReason: any) => void)
& ((fn: string, ...args: any[]) => void)
export interface ClarityApi {
clarity: ClarityFunctions & {
q: any[]
v: string
}
}
Config Schema
You must provide the options when setting up the script for the first time.
export const ClarityOptions = object({
/**
* The Clarity token.
*/
id: pipe(string(), minLength(10)),
})