DEV Community


Posted on


How to integrate Google AdSense in NextJS

If you are having a blog, you might want to monetize it. Adding ads to your blog is among the effective ways to monetize it.

In this article, I will show you how you can integrate Google AdSense into your NextJs app.

How do I get my site approved for google AdSense?

To have your site approved for showing Google ads, there are steps to follow carefully.

Image description

You can check these eligibility criteria here carefully to avoid being rejected 
in the first place.

Pro Tip: Before you request Google Adsense approval, make sure your website has at least 10 dynamic articles (content should be changing regularly) and a proper domain name. 

If your site is eligible for google AdSense, you will get a response from google on how to get started.

It takes a few days to get approved but in some cases, it might take between 2 and 4 weeks.

Image description

If your site is approved for google AdSense, follow the following steps to integrate it into your NextJs app 
and start showing ads.

Add your client in the NextJs _document script

pages/_document.tsx 👇🏽

import { Html, Head, Main, NextScript } from 'next/document';
import Script from 'next/script';

export default function Document() {
  return (
    <Html lang="en">
        <Main />
        <NextScript />
Enter fullscreen mode Exit fullscreen mode

change NEXT_PUBLIC_GOOGLE_ADS_CLIENT_ID. to the env variable containing your ads client id (ca-pub-*********)

Create an ad banner component

components/AdBanner.tsx 👇🏽

import { useEffect } from 'react';

const AdBanner = (props) => {
  useEffect(() => {
    try {
      (window.adsbygoogle = window.adsbygoogle || []).push({});
    } catch (err) {
  }, []);

  return (
      className="adsbygoogle adbanner-customize"
        display: 'block',
        overflow: 'hidden',
export default AdBanner;
Enter fullscreen mode Exit fullscreen mode

Get Google ads block codes from your adsense dashboard.

Notice that we are using the default code from the ad banner component above ☝🏾

adsense dashboard

Image description

let's try with Display ads .

click on display ads card

Image description

Choose your ad layout (depends on the place you want to place it)

Image description

Copy these codes (Ignore the rest. we already have them defined in our banner component) and click done 

Image description

Import the ad banner component anywhere your want this kind of ad to show and ad the codes copies above as props to the ad banner.

Our ad banner component imported will look like this 👇🏽

Enter fullscreen mode Exit fullscreen mode

Deploy the changes and on the hosted version of your app , the add banner will be showing the ad where your placed the component ✨

Feel free to drop any issue in the comments if you need help

You can also read article on my blog and feel free to drop questions or issues if you need help. 👉🏽 How to integrate Google AdSense in NextJS

Top comments (0)

Top Heroku Alternatives (For Free!)

Recently Heroku shut down free Heroku Dynos, free Heroku Postgres, and free Heroku Data for Redis on November 28th, 2022. So Meshv Patel put together some free alternatives in this classic DEV post.