Scripts

Google Adsense

Google AdSense allows you to monetize your website by displaying relevant ads from Google.

Nuxt Scripts provides:

  • useScriptGoogleAdsense(): A composable to manage Google AdSense dynamically.
  • <ScriptGoogleAdsense>: A headless component to embed ads directly in your Nuxt app.
Google Adsense

View source

Nuxt Config Setup

Add this to your nuxt.config.ts to load Google Adsense globally. Alternatively you can use the useScriptGoogleAdsense composable for more control.

export default defineNuxtConfig({
  scripts: {
    registry: {
      googleAdsense: {
        client: 'ca-pub-1234567890123456',
        trigger: 'onNuxtReady',
      }
    }
  }
})

This config automatically enables first-party mode (bundle + proxy). See below to customise.

useScriptGoogleAdsense()

The useScriptGoogleAdsense composable lets you have fine-grain control over when and how Google Adsense is loaded on your site.

const { proxy } = useScriptGoogleAdsense()

// ad slots render automatically

Please follow the Registry Scripts guide to learn more about advanced usage.

First-Party Mode: Privacy Focused Proxy

No extra config needed. The script is bundled from your domain (faster loads, no extra DNS lookup) and runtime requests are reverse-proxied through your server with automatic anonymisation (user IPs stay hidden from Google Adsense, works with ad blockers). Learn more.

Mode
Bundle Proxy
Privacy
IP, language, and hardware fingerprints are anonymised. Screen dimensions pass through so heatmaps render accurately.
export default defineNuxtConfig({
  scripts: {
    // ✅ First-party mode: bundled + proxied
    registry: {
      googleAdsense: {
        client: 'ca-pub-1234567890123456',
        trigger: 'onNuxtReady',
      },
    },
  },
})

Example

Using Google Adsense in a component with the proxy to send events .

<script setup lang="ts">
const { proxy } = useScriptGoogleAdsense()

// noop in development, ssr
// just works in production, client
function handleAction() {
  // ad slots render automatically
}
</script>

<template>
  <div>
    <button @click="handleAction">
      Send Event
    </button>
  </div>
</template>

Where to Find <your-id> (Publisher ID)

Find your Google AdSense Publisher ID (also known as ca-pub-XXXXXXX) in your Google AdSense Account:

  1. Log in to your Google AdSense account.
  2. Navigate to Account > Settings (click on your profile icon > "Account information").
  3. Locate the Publisher ID under Account Information.
  4. Replace <your-id> in the config above with your actual ID.
You can also manage Auto Ads settings from your Google AdSense Dashboard to control ad types, placements, and revenue optimization.

Site Ownership Verification

Automatic Meta Tag Insertion

If you provide a client, Nuxt automatically inserts a meta tag on the page for Google to verify your site ownership.

nuxt.config.ts
export default defineNuxtConfig({
  scripts: {
    registry: {
      googleAdsense: {
        client: 'ca-pub-<your-id>', // AdSense Publisher ID
      },
    },
  },
})

Using ads.txt for Verification

Google recommends adding an ads.txt file for ad revenue eligibility.

Steps:

  1. Create a new file: public/ads.txt
  2. Add the following content:
    google.com, pub-<your-id>, DIRECT, f08c47fec0942fa0
    
  3. Replace <your-id> with your AdSense Publisher ID.
Why use ads.txt? It helps prevent ad fraud and ensures that only your site can display your ads.

Enabling Auto Ads

Auto Ads allow Google to automatically place ads for better optimization.

nuxt.config.ts
export default defineNuxtConfig({
  scripts: {
    registry: {
      googleAdsense: {
        client: 'ca-pub-<your-id>', // AdSense Publisher ID
        autoAds: true, // Enable Auto Ads
      },
    },
  },
})

Using <ScriptGoogleAdsense> Component

It provides a simple way to embed ads in your Nuxt app.

<template>
  <ScriptGoogleAdsense
    data-ad-client="ca-pub-<your-id>"
    data-ad-slot="1234567890"
    data-ad-format="auto"
  />
</template>

Component Props

PropDescription
data-ad-clientYour Google Adsense Publisher ID(ca-pub-XXXXXXXXXX).
data-ad-slotYour Ad Slot ID (available in AdSense dashboard).
data-ad-formatAd format type (auto, rectangle, horizontal, vertical, fluid, autorelaxed).
data-ad-layoutLayout (in-article, in-feed, fixed).
data-full-width-responsiveSet to true to make the ad responsive.

Example Usage with data-ad-layout

To specify a layout for your ads (such as "in-article"), you can use the data-ad-layout attribute:

<template>
  <ScriptGoogleAdsense
    data-ad-client="ca-pub-<your-id>"
    data-ad-slot="1234567890"
    data-ad-format="fluid"
    data-ad-layout="in-article"
  />
</template>

How to Handle Ad-Blockers?

If a user has an ad-blocker enabled, you can show fallback content.

<template>
  <ScriptGoogleAdsense data-ad-client="ca-pub-..." data-ad-slot="...">
    <template #error>
      <!-- Fallback content -->
      <p>Please support us by disabling your ad blocker.</p>
    </template>
  </ScriptGoogleAdsense>
</template>

Using useScriptGoogleAdsense() Composable

The useScriptGoogleAdsense() composable allows fine-grain control over the AdSense script.

export function useScriptGoogleAdsense<T extends GoogleAdsenseApi>(
  _options?: GoogleAdsenseInput
) {}

See the Registry Scripts Guide for advanced usage.

Need more help? Check out the official Google AdSense Guide
clientstring

The Google Adsense ID.

autoAdsboolean = false

Enable or disable Auto Ads.