|
| 1 | +--- |
| 2 | +title: TikTok Pixel |
| 3 | +description: Use TikTok Pixel 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/tiktok-pixel.ts |
| 8 | + size: xs |
| 9 | +--- |
| 10 | + |
| 11 | +[TikTok Pixel](https://ads.tiktok.com/help/article/tiktok-pixel) lets you measure, optimize and build audiences for your TikTok ad campaigns. |
| 12 | + |
| 13 | +Nuxt Scripts provides a registry script composable `useScriptTikTokPixel` to easily integrate TikTok Pixel in your Nuxt app. |
| 14 | + |
| 15 | +### Nuxt Config Setup |
| 16 | + |
| 17 | +The simplest way to load TikTok Pixel globally in your Nuxt App is to use Nuxt config. Alternatively you can directly |
| 18 | +use the [useScriptTikTokPixel](#usescripttiktokpixel) 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 | + tiktokPixel: { |
| 30 | + id: 'YOUR_PIXEL_ID' |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | +}) |
| 35 | +``` |
| 36 | + |
| 37 | +```ts [Production only] |
| 38 | +export default defineNuxtConfig({ |
| 39 | + $production: { |
| 40 | + scripts: { |
| 41 | + registry: { |
| 42 | + tiktokPixel: { |
| 43 | + id: 'YOUR_PIXEL_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 | + tiktokPixel: true, |
| 62 | + } |
| 63 | + }, |
| 64 | + // you need to provide a runtime config to access the environment variables |
| 65 | + runtimeConfig: { |
| 66 | + public: { |
| 67 | + scripts: { |
| 68 | + tiktokPixel: { |
| 69 | + id: '', // NUXT_PUBLIC_SCRIPTS_TIKTOK_PIXEL_ID |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | +}) |
| 75 | +``` |
| 76 | + |
| 77 | +```text [.env] |
| 78 | +NUXT_PUBLIC_SCRIPTS_TIKTOK_PIXEL_ID=<YOUR_ID> |
| 79 | +``` |
| 80 | + |
| 81 | +## useScriptTikTokPixel |
| 82 | + |
| 83 | +The `useScriptTikTokPixel` composable lets you have fine-grain control over when and how TikTok Pixel is loaded on your site. |
| 84 | + |
| 85 | +```ts |
| 86 | +const { proxy } = useScriptTikTokPixel({ |
| 87 | + id: 'YOUR_PIXEL_ID' |
| 88 | +}) |
| 89 | + |
| 90 | +// Track an event |
| 91 | +proxy.ttq('track', 'ViewContent', { |
| 92 | + content_id: '123', |
| 93 | + content_name: 'Product Name', |
| 94 | + value: 99.99, |
| 95 | + currency: 'USD' |
| 96 | +}) |
| 97 | +``` |
| 98 | + |
| 99 | +Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about advanced usage. |
| 100 | + |
| 101 | +### TikTokPixelApi |
| 102 | + |
| 103 | +```ts |
| 104 | +export interface TikTokPixelApi { |
| 105 | + ttq: TtqFns & { |
| 106 | + push: TtqFns |
| 107 | + loaded: boolean |
| 108 | + queue: any[] |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +type TtqFns = |
| 113 | + & ((cmd: 'track', event: StandardEvents | string, properties?: EventProperties) => void) |
| 114 | + & ((cmd: 'page') => void) |
| 115 | + & ((cmd: 'identify', properties: IdentifyProperties) => void) |
| 116 | + & ((cmd: string, ...args: any[]) => void) |
| 117 | + |
| 118 | +type StandardEvents = |
| 119 | + | 'ViewContent' | 'ClickButton' | 'Search' | 'AddToWishlist' |
| 120 | + | 'AddToCart' | 'InitiateCheckout' | 'AddPaymentInfo' | 'CompletePayment' |
| 121 | + | 'PlaceAnOrder' | 'Contact' | 'Download' | 'SubmitForm' |
| 122 | + | 'CompleteRegistration' | 'Subscribe' |
| 123 | +``` |
| 124 | +
|
| 125 | +### Config Schema |
| 126 | +
|
| 127 | +You must provide the options when setting up the script for the first time. |
| 128 | +
|
| 129 | +```ts |
| 130 | +export const TikTokPixelOptions = object({ |
| 131 | + id: string(), |
| 132 | + trackPageView: optional(boolean()), // default: true |
| 133 | +}) |
| 134 | +``` |
| 135 | + |
| 136 | +## Example |
| 137 | + |
| 138 | +Using TikTok Pixel to track a purchase event. |
| 139 | + |
| 140 | +::code-group |
| 141 | + |
| 142 | +```vue [PurchaseButton.vue] |
| 143 | +<script setup lang="ts"> |
| 144 | +const { proxy } = useScriptTikTokPixel() |
| 145 | +
|
| 146 | +function trackPurchase() { |
| 147 | + proxy.ttq('track', 'CompletePayment', { |
| 148 | + content_id: 'product-123', |
| 149 | + content_name: 'Awesome Product', |
| 150 | + value: 49.99, |
| 151 | + currency: 'USD' |
| 152 | + }) |
| 153 | +} |
| 154 | +</script> |
| 155 | +
|
| 156 | +<template> |
| 157 | + <button @click="trackPurchase"> |
| 158 | + Complete Purchase |
| 159 | + </button> |
| 160 | +</template> |
| 161 | +``` |
| 162 | + |
| 163 | +:: |
| 164 | + |
| 165 | +## Identifying Users |
| 166 | + |
| 167 | +You can identify users for advanced matching: |
| 168 | + |
| 169 | +```ts |
| 170 | +const { proxy } = useScriptTikTokPixel() |
| 171 | + |
| 172 | +proxy.ttq('identify', { |
| 173 | + email: 'user@example.com', |
| 174 | + phone_number: '+1234567890' |
| 175 | +}) |
| 176 | +``` |
| 177 | + |
| 178 | +## Disabling Auto Page View |
| 179 | + |
| 180 | +By default, TikTok Pixel tracks page views automatically. To disable: |
| 181 | + |
| 182 | +```ts |
| 183 | +export default defineNuxtConfig({ |
| 184 | + scripts: { |
| 185 | + registry: { |
| 186 | + tiktokPixel: { |
| 187 | + id: 'YOUR_PIXEL_ID', |
| 188 | + trackPageView: false |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +}) |
| 193 | +``` |
0 commit comments