|
| 1 | +--- |
| 2 | +title: Google Adsense |
| 3 | +description: Show Google Adsense ads in your Nuxt app. |
| 4 | +links: |
| 5 | + - label: useScriptGoogleAdsense |
| 6 | + icon: i-simple-icons-github |
| 7 | + to: https://github.com/nuxt/scripts/blob/main/src/runtime/registry/google-adsense.ts |
| 8 | + size: xs |
| 9 | + - label: "<ScriptGoogleAdsense>" |
| 10 | + icon: i-simple-icons-github |
| 11 | + to: https://github.com/nuxt/scripts/blob/main/src/runtime/components/ScriptGoogleAdsense.vue |
| 12 | + size: xs |
| 13 | +--- |
| 14 | + |
| 15 | +:UAlert{title="Experimental" description="The Google Adsense integration has not been fully tested, use with caution." color="yellow" variant="soft" class="not-prose"} |
| 16 | + |
| 17 | +[Google Adsense](https://www.google.com/adsense/start/) allows you to monetize your website by displaying ads. |
| 18 | + |
| 19 | +Nuxt Scripts provides a `useScriptGoogleAdsense` composable and a headless `ScriptGoogleAdsense` component to interact with the Google Adsense. |
| 20 | + |
| 21 | +## ScriptGoogleAdsense |
| 22 | + |
| 23 | +The `ScriptGoogleAdsense` component is a wrapper around the `useScriptGoogleAdsense` composable. It provides a simple way to embed ads in your Nuxt app. |
| 24 | + |
| 25 | +```vue |
| 26 | +<template> |
| 27 | + <ScriptGoogleAdsense |
| 28 | + data-ad-client="ca-pub-..." |
| 29 | + data-ad-slot="..." |
| 30 | + /> |
| 31 | +</template> |
| 32 | +``` |
| 33 | + |
| 34 | +### Handling Ad-blockers |
| 35 | + |
| 36 | +You can use these hooks to add a fallback when the Google Adsense script is blocked. |
| 37 | + |
| 38 | +```vue |
| 39 | +<template> |
| 40 | + <ScriptGoogleAdsense |
| 41 | + data-ad-client="ca-pub-..." |
| 42 | + data-ad-slot="..." |
| 43 | + > |
| 44 | + <template #error> |
| 45 | + <!-- Fallback ad --> |
| 46 | + Please support us by disabling your ad blocker. |
| 47 | + </template> |
| 48 | + </ScriptGoogleAdsense> |
| 49 | +</template> |
| 50 | +``` |
| 51 | + |
| 52 | +### Props |
| 53 | + |
| 54 | +The `ScriptGoogleAdsense` component supports all props that Google Adsense supports on the `<ins>` tag. See the [Ad tags documentation](https://developers.google.com/adsense/platforms/transparent/ad-tags) for more information. |
| 55 | + |
| 56 | +At a minimum you must provide the following tags: |
| 57 | +- `data-ad-client`: The Google Adsense ID. |
| 58 | +- `data-ad-slot`: The slot ID. |
| 59 | + |
| 60 | +Nuxt Scripts exposes the following additional props: |
| 61 | +- `trigger`: The trigger event to load the script. Default is `undefined`. See [Element Event Triggers](/docs/guides/script-triggers#element-event-triggers) for more information. |
| 62 | + |
| 63 | +### Slots |
| 64 | + |
| 65 | +There are a number of slots mapped to the script status that you can use to customize the ad experience. |
| 66 | + |
| 67 | +- **error**: |
| 68 | + The slot is used to display content when the ad fails to load. |
| 69 | + |
| 70 | +- **awaitingLoad** |
| 71 | + The slot is used to display content before the ad script is loaded. |
| 72 | + |
| 73 | +- **loaded** |
| 74 | + The slot is used to display content after the ad script is loaded. |
| 75 | + |
| 76 | +- **loading** |
| 77 | + The slot is used to display content while the ad script is loading. |
| 78 | + |
| 79 | +```vue |
| 80 | +<template> |
| 81 | + <ScriptGoogleAdsense |
| 82 | + serve="..." |
| 83 | + placement="..." |
| 84 | + > |
| 85 | + <template #awaitingLoad> |
| 86 | + Loading ads... |
| 87 | + </template> |
| 88 | + </ScriptGoogleAdsense> |
| 89 | +</template> |
| 90 | +``` |
| 91 | + |
| 92 | +## useScriptGoogleAdsense |
| 93 | + |
| 94 | +The `useScriptGoogleAdsense` composable lets you have fine-grain control over the Google Adsense script. |
| 95 | + |
| 96 | +```ts |
| 97 | +export function useScriptGoogleAdsense<T extends GoogleAdsenseApi>(_options?: GoogleAdsenseInput) {} |
| 98 | +``` |
| 99 | + |
| 100 | +Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about advanced usage. |
| 101 | + |
| 102 | +### GoogleAdsenseApi |
| 103 | + |
| 104 | +```ts |
| 105 | +export interface GoogleAdsenseApi { |
| 106 | + adsbygoogle: any[] & { loaded: boolean } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### GoogleAdsenseInput |
| 111 | + |
| 112 | +```ts |
| 113 | +export const GoogleAdsenseOptions = object({ |
| 114 | + /** |
| 115 | + * The Google Adsense ID. |
| 116 | + */ |
| 117 | + client: optional(string()), |
| 118 | +}) |
| 119 | +``` |
0 commit comments