|
| 1 | +--- |
| 2 | +title: Reddit Pixel |
| 3 | +description: Use Reddit 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/reddit-pixel.ts |
| 8 | + size: xs |
| 9 | +--- |
| 10 | + |
| 11 | +[Reddit Pixel](https://advertising.reddithelp.com/en/categories/custom-audiences-and-conversion-tracking/reddit-pixel) helps you track conversions and build audiences for your Reddit advertising campaigns. |
| 12 | + |
| 13 | +Nuxt Scripts provides a registry script composable `useScriptRedditPixel` to easily integrate Reddit Pixel in your Nuxt app. |
| 14 | + |
| 15 | +### Nuxt Config Setup |
| 16 | + |
| 17 | +The simplest way to load Reddit Pixel globally in your Nuxt App is to use Nuxt config. Alternatively you can directly |
| 18 | +use the [useScriptRedditPixel](#useScriptRedditPixel) 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 | + redditPixel: { |
| 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 | + redditPixel: { |
| 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 | + redditPixel: true, |
| 62 | + } |
| 63 | + }, |
| 64 | + // you need to provide a runtime config to access the environment variables |
| 65 | + runtimeConfig: { |
| 66 | + public: { |
| 67 | + scripts: { |
| 68 | + redditPixel: { |
| 69 | + id: '', // NUXT_PUBLIC_SCRIPTS_REDDIT_PIXEL_ID |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | +}) |
| 75 | +``` |
| 76 | + |
| 77 | +```text [.env] |
| 78 | +NUXT_PUBLIC_SCRIPTS_REDDIT_PIXEL_ID=<YOUR_ID> |
| 79 | +``` |
| 80 | + |
| 81 | +## useScriptRedditPixel |
| 82 | + |
| 83 | +The `useScriptRedditPixel` composable lets you have fine-grain control over when and how Reddit Pixel is loaded on your site. |
| 84 | + |
| 85 | +```ts |
| 86 | +const { proxy } = useScriptRedditPixel({ |
| 87 | + id: 'YOUR_ID' |
| 88 | +}) |
| 89 | +// example |
| 90 | +proxy.rdt('track', 'Lead') |
| 91 | +``` |
| 92 | + |
| 93 | +Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about advanced usage. |
| 94 | + |
| 95 | +### RedditPixelApi |
| 96 | + |
| 97 | +```ts |
| 98 | +export interface RedditPixelApi { |
| 99 | + rdt: RdtFns & { |
| 100 | + sendEvent: (rdt: RedditPixelApi['rdt'], args: unknown[]) => void |
| 101 | + callQueue: unknown[] |
| 102 | + } |
| 103 | +} |
| 104 | +type RdtFns |
| 105 | + = & ((event: 'init', id: string) => void) |
| 106 | + & ((event: 'track', eventName: string) => void) |
| 107 | +``` |
| 108 | +
|
| 109 | +### Config Schema |
| 110 | +
|
| 111 | +You must provide the options when setting up the script for the first time. |
| 112 | +
|
| 113 | +```ts |
| 114 | +export const RedditPixelOptions = object({ |
| 115 | + id: string(), |
| 116 | +}) |
| 117 | +``` |
| 118 | + |
| 119 | +## Example |
| 120 | + |
| 121 | +Using Reddit Pixel only in production while using `rdt` to send a tracking event. |
| 122 | + |
| 123 | +::code-group |
| 124 | + |
| 125 | +```vue [TrackingButton.vue] |
| 126 | +<script setup lang="ts"> |
| 127 | +const { proxy } = useScriptRedditPixel() |
| 128 | +
|
| 129 | +// noop in development, ssr |
| 130 | +// just works in production, client |
| 131 | +function trackConversion() { |
| 132 | + proxy.rdt('track', 'Lead') |
| 133 | +} |
| 134 | +</script> |
| 135 | +
|
| 136 | +<template> |
| 137 | + <div> |
| 138 | + <button @click="trackConversion"> |
| 139 | + Track Conversion |
| 140 | + </button> |
| 141 | + </div> |
| 142 | +</template> |
| 143 | +``` |
| 144 | + |
| 145 | +:: |
0 commit comments