-
Notifications
You must be signed in to change notification settings - Fork 75
feat(registry): add TikTok Pixel script #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
798cb74
feat(registry): add TikTok Pixel script
harlan-zw bbbcafc
fix(tiktok-pixel): add required sdkid and lib query params
harlan-zw 1bfcda1
fix(tiktok-pixel): guard init call when id missing
harlan-zw 017b169
feat(playground): add TikTok Pixel demo page
harlan-zw 3b0134f
Merge branch 'main' into feat/tiktok-pixel
harlan-zw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| --- | ||
| title: TikTok Pixel | ||
| description: Use TikTok Pixel in your Nuxt app. | ||
| links: | ||
| - label: Source | ||
| icon: i-simple-icons-github | ||
| to: https://github.com/nuxt/scripts/blob/main/src/runtime/registry/tiktok-pixel.ts | ||
| size: xs | ||
| --- | ||
|
|
||
| [TikTok Pixel](https://ads.tiktok.com/help/article/tiktok-pixel) lets you measure, optimize and build audiences for your TikTok ad campaigns. | ||
|
|
||
| Nuxt Scripts provides a registry script composable `useScriptTikTokPixel` to easily integrate TikTok Pixel in your Nuxt app. | ||
|
|
||
| ### Nuxt Config Setup | ||
|
|
||
| The simplest way to load TikTok Pixel globally in your Nuxt App is to use Nuxt config. Alternatively you can directly | ||
| use the [useScriptTikTokPixel](#usescripttiktokpixel) composable. | ||
|
|
||
| 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 | ||
| disable the script in development. | ||
|
|
||
| ::code-group | ||
|
|
||
| ```ts [Always enabled] | ||
| export default defineNuxtConfig({ | ||
| scripts: { | ||
| registry: { | ||
| tiktokPixel: { | ||
| id: 'YOUR_PIXEL_ID' | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ```ts [Production only] | ||
| export default defineNuxtConfig({ | ||
| $production: { | ||
| scripts: { | ||
| registry: { | ||
| tiktokPixel: { | ||
| id: 'YOUR_PIXEL_ID' | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| :: | ||
|
|
||
| #### With Environment Variables | ||
|
|
||
| If you prefer to configure your id using environment variables. | ||
|
|
||
| ```ts [nuxt.config.ts] | ||
| export default defineNuxtConfig({ | ||
| scripts: { | ||
| registry: { | ||
| tiktokPixel: true, | ||
| } | ||
| }, | ||
| // you need to provide a runtime config to access the environment variables | ||
| runtimeConfig: { | ||
| public: { | ||
| scripts: { | ||
| tiktokPixel: { | ||
| id: '', // NUXT_PUBLIC_SCRIPTS_TIKTOK_PIXEL_ID | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| ```text [.env] | ||
| NUXT_PUBLIC_SCRIPTS_TIKTOK_PIXEL_ID=<YOUR_ID> | ||
| ``` | ||
|
|
||
| ## useScriptTikTokPixel | ||
|
|
||
| The `useScriptTikTokPixel` composable lets you have fine-grain control over when and how TikTok Pixel is loaded on your site. | ||
|
|
||
| ```ts | ||
| const { proxy } = useScriptTikTokPixel({ | ||
| id: 'YOUR_PIXEL_ID' | ||
| }) | ||
|
|
||
| // Track an event | ||
| proxy.ttq('track', 'ViewContent', { | ||
| content_id: '123', | ||
| content_name: 'Product Name', | ||
| value: 99.99, | ||
| currency: 'USD' | ||
| }) | ||
| ``` | ||
|
|
||
| Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about advanced usage. | ||
|
|
||
| ### TikTokPixelApi | ||
|
|
||
| ```ts | ||
| export interface TikTokPixelApi { | ||
| ttq: TtqFns & { | ||
| push: TtqFns | ||
| loaded: boolean | ||
| queue: any[] | ||
| } | ||
| } | ||
|
|
||
| type TtqFns = | ||
| & ((cmd: 'track', event: StandardEvents | string, properties?: EventProperties) => void) | ||
| & ((cmd: 'page') => void) | ||
| & ((cmd: 'identify', properties: IdentifyProperties) => void) | ||
| & ((cmd: string, ...args: any[]) => void) | ||
|
|
||
| type StandardEvents = | ||
| | 'ViewContent' | 'ClickButton' | 'Search' | 'AddToWishlist' | ||
| | 'AddToCart' | 'InitiateCheckout' | 'AddPaymentInfo' | 'CompletePayment' | ||
| | 'PlaceAnOrder' | 'Contact' | 'Download' | 'SubmitForm' | ||
| | 'CompleteRegistration' | 'Subscribe' | ||
| ``` | ||
|
|
||
| ### Config Schema | ||
|
|
||
| You must provide the options when setting up the script for the first time. | ||
|
|
||
| ```ts | ||
| export const TikTokPixelOptions = object({ | ||
| id: string(), | ||
| trackPageView: optional(boolean()), // default: true | ||
| }) | ||
| ``` | ||
|
|
||
| ## Example | ||
|
|
||
| Using TikTok Pixel to track a purchase event. | ||
|
|
||
| ::code-group | ||
|
|
||
| ```vue [PurchaseButton.vue] | ||
| <script setup lang="ts"> | ||
| const { proxy } = useScriptTikTokPixel() | ||
|
|
||
| function trackPurchase() { | ||
| proxy.ttq('track', 'CompletePayment', { | ||
| content_id: 'product-123', | ||
| content_name: 'Awesome Product', | ||
| value: 49.99, | ||
| currency: 'USD' | ||
| }) | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <button @click="trackPurchase"> | ||
| Complete Purchase | ||
| </button> | ||
| </template> | ||
| ``` | ||
|
|
||
| :: | ||
|
|
||
| ## Identifying Users | ||
|
|
||
| You can identify users for advanced matching: | ||
|
|
||
| ```ts | ||
| const { proxy } = useScriptTikTokPixel() | ||
|
|
||
| proxy.ttq('identify', { | ||
| email: 'user@example.com', | ||
| phone_number: '+1234567890' | ||
| }) | ||
| ``` | ||
|
|
||
| ## Disabling Auto Page View | ||
|
|
||
| By default, TikTok Pixel tracks page views automatically. To disable: | ||
|
|
||
| ```ts | ||
| export default defineNuxtConfig({ | ||
| scripts: { | ||
| registry: { | ||
| tiktokPixel: { | ||
| id: 'YOUR_PIXEL_ID', | ||
| trackPageView: false | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { useRegistryScript } from '../utils' | ||
| import { object, string, optional, boolean } from '#nuxt-scripts-validator' | ||
| import type { RegistryScriptInput } from '#nuxt-scripts/types' | ||
|
|
||
| type StandardEvents | ||
| = 'ViewContent' | ||
| | 'ClickButton' | ||
| | 'Search' | ||
| | 'AddToWishlist' | ||
| | 'AddToCart' | ||
| | 'InitiateCheckout' | ||
| | 'AddPaymentInfo' | ||
| | 'CompletePayment' | ||
| | 'PlaceAnOrder' | ||
| | 'Contact' | ||
| | 'Download' | ||
| | 'SubmitForm' | ||
| | 'CompleteRegistration' | ||
| | 'Subscribe' | ||
|
|
||
| interface EventProperties { | ||
| content_id?: string | ||
| content_type?: string | ||
| content_name?: string | ||
| contents?: Array<{ content_id: string, content_type?: string, content_name?: string, price?: number, quantity?: number }> | ||
| currency?: string | ||
| value?: number | ||
| description?: string | ||
| query?: string | ||
| [key: string]: any | ||
| } | ||
|
|
||
| interface IdentifyProperties { | ||
| email?: string | ||
| phone_number?: string | ||
| external_id?: string | ||
| } | ||
|
|
||
| type TtqFns | ||
| = ((cmd: 'track', event: StandardEvents | string, properties?: EventProperties) => void) | ||
| & ((cmd: 'page') => void) | ||
| & ((cmd: 'identify', properties: IdentifyProperties) => void) | ||
| & ((cmd: string, ...args: any[]) => void) | ||
|
|
||
| export interface TikTokPixelApi { | ||
| ttq: TtqFns & { | ||
| push: TtqFns | ||
| loaded: boolean | ||
| queue: any[] | ||
| } | ||
| } | ||
|
|
||
| declare global { | ||
| interface Window extends TikTokPixelApi {} | ||
| } | ||
|
|
||
| export const TikTokPixelOptions = object({ | ||
| id: string(), | ||
| trackPageView: optional(boolean()), // default true | ||
| }) | ||
|
|
||
| export type TikTokPixelInput = RegistryScriptInput<typeof TikTokPixelOptions, true, false, false> | ||
|
|
||
| export function useScriptTikTokPixel<T extends TikTokPixelApi>(_options?: TikTokPixelInput) { | ||
| return useRegistryScript<T, typeof TikTokPixelOptions>('tiktokPixel', options => ({ | ||
| scriptInput: { | ||
| src: 'https://analytics.tiktok.com/i18n/pixel/events.js', | ||
| crossorigin: false, | ||
| }, | ||
| schema: import.meta.dev ? TikTokPixelOptions : undefined, | ||
| scriptOptions: { | ||
| use() { | ||
| return { ttq: window.ttq } | ||
| }, | ||
| }, | ||
| clientInit: import.meta.server | ||
| ? undefined | ||
| : () => { | ||
| const ttq: TikTokPixelApi['ttq'] = window.ttq = function (...params: any[]) { | ||
| // @ts-expect-error untyped | ||
| if (ttq.callMethod) { | ||
| // @ts-expect-error untyped | ||
| ttq.callMethod(...params) | ||
| } | ||
| else { | ||
| ttq.queue.push(params) | ||
| } | ||
| } as any | ||
| ttq.push = ttq | ||
| ttq.loaded = true | ||
| ttq.queue = [] | ||
| ttq('init', options?.id) | ||
| if (options?.trackPageView !== false) { | ||
| ttq('page') | ||
| } | ||
| }, | ||
| }), _options) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.