Hassan Agmir Hassan Agmir

How to Add AdSense Code to a React App

Hassan Agmir
How to Add AdSense Code to a React App

Google AdSense is one of the most popular and effective ways to monetize a website. If you’ve built your website using React, you might be wondering how to integrate AdSense ads into your app. While React’s component-based architecture makes it slightly different from traditional HTML and JavaScript integration, adding AdSense to your React app is straightforward when you know the steps.

This guide will walk you through the process of adding AdSense code to your React application.

Prerequisites

Before you start, make sure:

  1. You have a Google AdSense account and approval to display ads.
  2. You have your AdSense ad unit code ready.
  3. Your React app is set up and running locally or deployed to a live server.
  4. You’re familiar with React components and basic JavaScript.

Steps to Add AdSense Code to Your React App

1. Enable Google AdSense in Your App

Google AdSense requires you to place its JavaScript snippet in your app to load and display ads. Here’s how to include it:

a. Add the AdSense Script Globally

Add the Google AdSense script to your HTML file. In a React app, this file is typically public/index.html.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>React App</title>
    <!-- Google AdSense Script -->
    <script
      async
      src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
    ></script>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

The async attribute ensures the script does not block your app’s rendering.

b. Add AdSense Code to Components

Once the script is included, you can use your AdSense code in the components where you want ads to appear.

2. Create an Ad Component

To keep your code clean and reusable, create a dedicated Ad component for rendering ads.

Example: AdSense Component

import React, { useEffect } from 'react';

const AdSenseAd = ({ client, slot, format = 'auto', responsive = 'true' }) => {
  useEffect(() => {
    // Ensure the ads are reloaded when the component is mounted
    if (window.adsbygoogle) {
      window.adsbygoogle.push({});
    }
  }, []);

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

export default AdSenseAd;

3. Use the Ad Component

To display ads, use the AdSenseAd component and provide the necessary props such as your client and slot IDs.

import React from 'react';
import AdSenseAd from './AdSenseAd';

const HomePage = () => {
  return (
    <div>
      <h1>Welcome to My React App</h1>
      <p>This is the homepage of my React app.</p>

      {/* Display AdSense Ad */}
      <AdSenseAd
        client="ca-pub-XXXXXXXXXXXXXXXX"
        slot="XXXXXXXXXX"
      />

      <p>Enjoy our content!</p>
    </div>
  );
};

export default HomePage;

4. Testing Ads Locally

AdSense ads may not display properly when testing locally due to domain restrictions. To test:

  1. Deploy your app to a live server (e.g., Netlify, Vercel, or Firebase).
  2. Ensure your domain is approved in your AdSense account.

5. Troubleshooting Common Issues

  • Ads Not Showing:
    • Verify that your AdSense account is active.
    • Ensure your ad units are correctly configured in the AdSense dashboard.
    • Check the browser console for any AdSense-related errors.
  • Responsive Ads:
    • Use data-full-width-responsive="true" to ensure the ads adapt to different screen sizes.
  • Ad Blockers:
    • Inform users to disable ad blockers if ads do not display.

Additional Tips

  • Optimize Ad Placement: Place ads in high-visibility areas but avoid intrusive placements.
  • Follow AdSense Policies: Ensure compliance with Google AdSense policies to avoid account suspension.
  • Monitor Performance: Use the AdSense dashboard to track your ad performance and adjust as needed.

Following these steps, you can effectively integrate Google AdSense into your React app and monetize your content. With proper placement and adherence to best practices, AdSense can become a significant revenue source for your application

Thanks ♥

Hassan Agmir

Subscribe to my Newsletters

Stay updated with the latest programming tips, tricks, and IT insights! Join my community to receive exclusive content on coding best practices.

© Copyright 2025 by Hassan Agmir . Built with ❤ by Me