DEV Community

Cover image for Google Ads and React 101: A Beginner’s Guide
DivyanshuLohani
DivyanshuLohani

Posted on

Google Ads and React 101: A Beginner’s Guide

The "How Hard Can It Be?" Moment


So you’ve built a beautiful React project, and now you’re thinking, Why not monetize it? You google, "how to add Google Ads in React," and suddenly you’re drowning in a sea of outdated tutorials, confusing documentation, and forum posts that contradict each other.

I’ve been there. So let’s break it down into simple, actionable steps without the fluff.

Prerequisites

Before you start, you need to have a live production website. Google has deprecated the data-ads-test attribute, which means ads will not work in development mode or on localhost. Your site must be publicly accessible for AdSense to serve ads.

Step 1: Set Up a Google AdSense Account

Before writing any code, you need an AdSense account. If you don’t have one, go to Google AdSense and sign up. You’ll need:

  • A Google account (You obviously have one unless you woke up from a coma after 20 years)
  • A live website (if your project is online)
  • Approval from Google (can take a few days)

Once approved, you’ll get access to your publisher ID and the ability to create ad units.

Step 2: Generate Your Ad Code

  1. Go to AdSense Dashboard → Click Ads.
  2. Choose By ad unit → Select Display ads.
  3. Customize the ad size and format.
  4. Copy the ad code snippet that Google provides.

It should look something like this

Step 3: Adding Google Ads to Your React Project

Create a component named GoogleAds

Click image to copy

Add the script tag in the index.html present in the public folder if you're using create-react-app (which you shouldn't be its been officially dead now)

Then you can use the GoogleAd component anywhere you want to place the ads.

Conclusion

Adding Google Ads in React isn’t as terrifying as it seems. It’s mostly about copying the correct code, handling script loading properly, and being patient with Google’s approval process.

Now, go forth and monetize While I figure out why my ads sometimes refuse to load when I need them the most.

Top comments (1)

Collapse
 
hoxogip profile image
hoxogip • Edited

To add Google Ads to a React project, first make sure your website is live and approved by Google AdSense, as ads won’t work in development or on localhost. Once approved, create display ad units in AdSense and copy the provided ad code snippet. In your React project, create a GoogleAds component and ensure the AdSense script is loaded properly, typically in index.html or via a script loader. Then, use the component wherever you want ads to appear. Proper script handling and patience with Google’s approval process are key for the ads to display correctly.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.