|
| 1 | +--- |
| 2 | +title: Nuxt |
| 3 | +--- |
| 4 | + |
| 5 | +import Link from 'next/link'; |
| 6 | +import { Step, Steps } from 'fumadocs-ui/components/steps'; |
| 7 | + |
| 8 | +import { DeviceIdWarning } from '@/components/device-id-warning'; |
| 9 | +import { PersonalDataWarning } from '@/components/personal-data-warning'; |
| 10 | +import { Callout } from 'fumadocs-ui/components/callout'; |
| 11 | +import CommonSdkConfig from '@/components/common-sdk-config.mdx'; |
| 12 | +import WebSdkConfig from '@/components/web-sdk-config.mdx'; |
| 13 | + |
| 14 | +<Callout> |
| 15 | +Looking for a step-by-step tutorial? Check out the [Nuxt analytics guide](/guides/nuxt-analytics). |
| 16 | +</Callout> |
| 17 | + |
| 18 | +## Good to know |
| 19 | + |
| 20 | +Keep in mind that all tracking here happens on the client! |
| 21 | + |
| 22 | +Read more about server side tracking in the [Server Side Tracking](#track-server-events) section. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +<Steps> |
| 27 | +### Install dependencies |
| 28 | + |
| 29 | +```bash |
| 30 | +pnpm install @openpanel/nuxt |
| 31 | +``` |
| 32 | + |
| 33 | +### Initialize |
| 34 | + |
| 35 | +Add the module to your `nuxt.config.ts`: |
| 36 | + |
| 37 | +```typescript |
| 38 | +export default defineNuxtConfig({ |
| 39 | + modules: ['@openpanel/nuxt'], |
| 40 | + openpanel: { |
| 41 | + clientId: 'your-client-id', |
| 42 | + trackScreenViews: true, |
| 43 | + trackOutgoingLinks: true, |
| 44 | + trackAttributes: true, |
| 45 | + }, |
| 46 | +}); |
| 47 | +``` |
| 48 | + |
| 49 | +#### Options |
| 50 | + |
| 51 | +<CommonSdkConfig /> |
| 52 | +<WebSdkConfig /> |
| 53 | + |
| 54 | +##### Nuxt options |
| 55 | + |
| 56 | +- `clientId` - Your OpenPanel client ID (required) |
| 57 | +- `apiUrl` - The API URL to send events to (default: `https://api.openpanel.dev`) |
| 58 | +- `trackScreenViews` - Automatically track screen views (default: `true`) |
| 59 | +- `trackOutgoingLinks` - Automatically track outgoing links (default: `true`) |
| 60 | +- `trackAttributes` - Automatically track elements with `data-track` attributes (default: `true`) |
| 61 | +- `trackHashChanges` - Track hash changes in URL (default: `false`) |
| 62 | +- `disabled` - Disable tracking (default: `false`) |
| 63 | +- `proxy` - Enable server-side proxy to avoid adblockers (default: `false`) |
| 64 | + |
| 65 | +</Steps> |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +### Using the composable |
| 70 | + |
| 71 | +The `useOpenPanel` composable is auto-imported, so you can use it directly in any component: |
| 72 | + |
| 73 | +```vue |
| 74 | +<script setup> |
| 75 | +const op = useOpenPanel(); // Auto-imported! |
| 76 | +
|
| 77 | +function handleClick() { |
| 78 | + op.track('button_click', { button: 'signup' }); |
| 79 | +} |
| 80 | +</script> |
| 81 | +
|
| 82 | +<template> |
| 83 | + <button @click="handleClick">Trigger event</button> |
| 84 | +</template> |
| 85 | +``` |
| 86 | + |
| 87 | +### Accessing via useNuxtApp |
| 88 | + |
| 89 | +You can also access the OpenPanel instance directly via `useNuxtApp()`: |
| 90 | + |
| 91 | +```vue |
| 92 | +<script setup> |
| 93 | +const { $openpanel } = useNuxtApp(); |
| 94 | +
|
| 95 | +$openpanel.track('my_event', { foo: 'bar' }); |
| 96 | +</script> |
| 97 | +``` |
| 98 | + |
| 99 | +### Tracking Events |
| 100 | + |
| 101 | +You can track events with two different methods: by calling the `op.track()` method directly or by adding `data-track` attributes to your HTML elements. |
| 102 | + |
| 103 | +```vue |
| 104 | +<script setup> |
| 105 | +const op = useOpenPanel(); |
| 106 | +op.track('my_event', { foo: 'bar' }); |
| 107 | +</script> |
| 108 | +``` |
| 109 | + |
| 110 | +### Identifying Users |
| 111 | + |
| 112 | +To identify a user, call the `op.identify()` method with a unique identifier. |
| 113 | + |
| 114 | +```vue |
| 115 | +<script setup> |
| 116 | +const op = useOpenPanel(); |
| 117 | +
|
| 118 | +op.identify({ |
| 119 | + profileId: '123', // Required |
| 120 | + firstName: 'Joe', |
| 121 | + lastName: 'Doe', |
| 122 | + |
| 123 | + properties: { |
| 124 | + tier: 'premium', |
| 125 | + }, |
| 126 | +}); |
| 127 | +</script> |
| 128 | +``` |
| 129 | + |
| 130 | +### Setting Global Properties |
| 131 | + |
| 132 | +To set properties that will be sent with every event: |
| 133 | + |
| 134 | +```vue |
| 135 | +<script setup> |
| 136 | +const op = useOpenPanel(); |
| 137 | +
|
| 138 | +op.setGlobalProperties({ |
| 139 | + app_version: '1.0.2', |
| 140 | + environment: 'production', |
| 141 | +}); |
| 142 | +</script> |
| 143 | +``` |
| 144 | + |
| 145 | +### Incrementing Properties |
| 146 | + |
| 147 | +To increment a numeric property on a user profile. |
| 148 | + |
| 149 | +- `value` is the amount to increment the property by. If not provided, the property will be incremented by 1. |
| 150 | + |
| 151 | +```vue |
| 152 | +<script setup> |
| 153 | +const op = useOpenPanel(); |
| 154 | +
|
| 155 | +op.increment({ |
| 156 | + profileId: '1', |
| 157 | + property: 'visits', |
| 158 | + value: 1, // optional |
| 159 | +}); |
| 160 | +</script> |
| 161 | +``` |
| 162 | + |
| 163 | +### Decrementing Properties |
| 164 | + |
| 165 | +To decrement a numeric property on a user profile. |
| 166 | + |
| 167 | +- `value` is the amount to decrement the property by. If not provided, the property will be decremented by 1. |
| 168 | + |
| 169 | +```vue |
| 170 | +<script setup> |
| 171 | +const op = useOpenPanel(); |
| 172 | +
|
| 173 | +op.decrement({ |
| 174 | + profileId: '1', |
| 175 | + property: 'visits', |
| 176 | + value: 1, // optional |
| 177 | +}); |
| 178 | +</script> |
| 179 | +``` |
| 180 | + |
| 181 | +### Clearing User Data |
| 182 | + |
| 183 | +To clear the current user's data: |
| 184 | + |
| 185 | +```vue |
| 186 | +<script setup> |
| 187 | +const op = useOpenPanel(); |
| 188 | +
|
| 189 | +op.clear(); |
| 190 | +</script> |
| 191 | +``` |
| 192 | + |
| 193 | +## Server side |
| 194 | + |
| 195 | +If you want to track server-side events, you should create an instance of our Javascript SDK. Import `OpenPanel` from `@openpanel/sdk` |
| 196 | + |
| 197 | +<Callout> |
| 198 | +When using server events it's important that you use a secret to authenticate the request. This is to prevent unauthorized requests since we cannot use cors headers. |
| 199 | + |
| 200 | +You can use the same clientId but you should pass the associated client secret to the SDK. |
| 201 | + |
| 202 | +</Callout> |
| 203 | + |
| 204 | +```typescript |
| 205 | +import { OpenPanel } from '@openpanel/sdk'; |
| 206 | + |
| 207 | +const opServer = new OpenPanel({ |
| 208 | + clientId: '{YOUR_CLIENT_ID}', |
| 209 | + clientSecret: '{YOUR_CLIENT_SECRET}', |
| 210 | +}); |
| 211 | + |
| 212 | +opServer.track('my_server_event', { ok: '✅' }); |
| 213 | + |
| 214 | +// Pass `profileId` to track events for a specific user |
| 215 | +opServer.track('my_server_event', { profileId: '123', ok: '✅' }); |
| 216 | +``` |
| 217 | + |
| 218 | +### Serverless & Edge Functions |
| 219 | + |
| 220 | +If you log events in a serverless environment, make sure to await the event call to ensure it completes before the function terminates. |
| 221 | + |
| 222 | +```typescript |
| 223 | +import { OpenPanel } from '@openpanel/sdk'; |
| 224 | + |
| 225 | +const opServer = new OpenPanel({ |
| 226 | + clientId: '{YOUR_CLIENT_ID}', |
| 227 | + clientSecret: '{YOUR_CLIENT_SECRET}', |
| 228 | +}); |
| 229 | + |
| 230 | +export default defineEventHandler(async (event) => { |
| 231 | + // Await to ensure event is logged before function completes |
| 232 | + await opServer.track('my_server_event', { foo: 'bar' }); |
| 233 | + return { message: 'Event logged!' }; |
| 234 | +}); |
| 235 | +``` |
| 236 | + |
| 237 | +### Proxy events |
| 238 | + |
| 239 | +With the `proxy` option enabled, you can proxy your events through your server, which ensures all events are tracked since many adblockers block requests to third-party domains. |
| 240 | + |
| 241 | +```typescript title="nuxt.config.ts" |
| 242 | +export default defineNuxtConfig({ |
| 243 | + modules: ['@openpanel/nuxt'], |
| 244 | + openpanel: { |
| 245 | + clientId: 'your-client-id', |
| 246 | + proxy: true, // Enables proxy at /api/openpanel/* |
| 247 | + }, |
| 248 | +}); |
| 249 | +``` |
| 250 | + |
| 251 | +When `proxy: true` is set: |
| 252 | +- The module automatically sets `apiUrl` to `/api/openpanel` |
| 253 | +- A server handler is registered at `/api/openpanel/**` |
| 254 | +- All tracking requests route through your server |
| 255 | + |
| 256 | +This helps bypass adblockers that might block requests to `api.openpanel.dev`. |
0 commit comments