📚
Ncmaz- Nextjs Headless WordPress Blog, Magazine
View demoBuy theme now
  • 💝A few words of thanks
  • 👥Community
  • 📦Inside the downloaded package
  • 💁Support
  • 🌠Domains and hosting?
  • Getting Started Setup
    • ⭐Getting Started
    • Step 1. Configure WordPress Server
    • Step 2. Install the WP theme & plugins.
    • Step 3. Check Your Permalinks and WPGraphQL Settings
    • Step 4. Update environment variables.
    • Step 5. Deploying the Ncmaz-Faust to Vercel
      • 5.1 - Deploy Ncmaz to Vercel via a GitHub repo.
      • 5.2 - Or deploy Ncmaz to Vercel using Vercel CLI
      • 5.3 - Error when deploying to Vercel using the GitHub repo.
      • 5.4 - Other Hosting Options
    • Step 6. Set your Front-end Site URL. Congratulations 🎉
    • Step 7. Import demo data
      • [ERROR] Failed to import "Home 2" page
    • Step 8. Connecting MailPoet to Ncmaz-frontend
    • Step 9. Setting Logo, Socials, SEO, ... Site settings JSON
  • Development on localhost
    • Run on localhost
      • Error when run start
    • Deployments
  • WordPress
    • Build a home page with Ncmaz Gutenberg
    • Config Mailpoet
    • You don't have permission to submit the post.
    • Compatible with other plugins?
    • Using Headless WordPress with Next.js and Vercel
    • Create Anchor Links Manually with WordPress Block Editor
    • Create post-format Video, Audio, and Gallery
    • Contact form
  • Header
    • Header navigation menus
    • Avatar dropdown menus
  • Footer
    • Footer menu
    • Footer subscribe form
  • Single post page
    • Setting single style and toggle single sidebar
    • Setting Post format Video, Audio, Gallery
  • Ncmaz-faust
    • Install node_module
    • Custom border-radius
    • Custom color, fonts
    • Components
    • Sources And Credits
    • Translate the theme into your language
    • RTL mode
    • Nextjs-google-analytics
    • 414 Request-URI Too Large
    • Sitemap XML, robot.txt
    • Integrate Google AdSense in NextJS
    • Site Settings JSON
  • For developers
    • Project initiation
    • Enviroment variables
    • Ncmaz-faust-core plugin.
    • Ncmaz-faust project
    • Coming soon!
  • Change log
    • Update theme?
    • Change log
Powered by GitBook
On this page
  • 1 - Sign up for a Google AdSense account
  • 2 - Create an AdSense component
  • 3 - Add AdSense script to _document.tsx
  • 4 - Use the AdSense component in pages
  • 5 - Submit your website for review

Was this helpful?

  1. Ncmaz-faust

Integrate Google AdSense in NextJS

PreviousSitemap XML, robot.txtNextSite Settings JSON

Last updated 6 months ago

Was this helpful?

1 - Sign up for a Google AdSense account

Before starting, make sure you have a Google AdSense account. If not, sign up at .

2 - Create an AdSense component

Create a React component to hold the AdSense ad code. Create a new tsx file inside the /components folder and name it AdSense.tsx

src/components/AdSense.tsx

import React, { useEffect } from 'react';

interface AdSenseProps {
  client: string;
  slot: string;
  style?: React.CSSProperties;
}

const AdSense: React.FC<AdSenseProps> = ({ client, slot, style }) => {
  useEffect(() => {
    try {
      (window.adsbygoogle = window.adsbygoogle || []).push({});
    } catch (err) {
      console.error(err);
    }
  }, []);

  return (
    <ins
      className="adsbygoogle"
      style={style || { display: 'block' }}
      data-ad-client={client}
      data-ad-slot={slot}
      data-ad-format="auto"
      data-full-width-responsive="true"
    />
  );
};

export default AdSense;

3 - Add AdSense script to _document.tsx

Add the AdSense script to inside the <Head> tag of the src/pages/_document.tsx file to ensure it's loaded on every page.

Note: Replace ca-pub-XXXXXXXXXXXXXXXX with your actual AdSense publisher ID.

// ... other code ...

      <Head>
        
        // ... other code ...
        
        <meta
          httpEquiv="Content-Security-Policy"
          content="script-src 'self' 'unsafe-inline' 'unsafe-eval' https://pagead2.googlesyndication.com https://partner.googleadservices.com https://tpc.googlesyndication.com https://www.googletagservices.com"
        />
         
        <script
           async
           src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"
           crossOrigin="anonymous"
         />
          
      </Head>
         
     // ... other code ...

Note: Replace ca-pub-XXXXXXXXXXXXXXXX with your actual AdSense publisher ID.

4 - Use the AdSense component in pages

Now you can use the AdSense component in any page of your Next.js application.

You can find WordPress Template System pages inside the /src/wp-templates/ folder.

And you can find other pages like: Author page, Seach, Reading list, Submission, ... inside the /src/pages/ folder.

import AdSense from '@/components/AdSense';

const Page: FaustTemplate<GetPageQuery> = (props) => {
  return (
    <div>
     {/* Other page content */}
      <AdSense client="ca-pub-XXXXXXXXXXXXXXXX" slot="1234567890" />
      {/* Other page content */}
    </div>
  );
};

5 - Submit your website for review

After integrating AdSense into your website, submit it for review in your AdSense account.

Important notes:

  1. Make sure to comply with all Google AdSense policies to avoid rejection or account suspension.

  2. Consider the performance impact when adding ads. Too many ads can slow down your website.

  3. Always test thoroughly after implementation to ensure everything works as expected.

https://www.google.com/adsense/