<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Iamdeuos</title>
    <description>The latest articles on Forem by Iamdeuos (@deuos).</description>
    <link>https://forem.com/deuos</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2945865%2F33449920-c201-4f51-8763-2782c2647044.png</url>
      <title>Forem: Iamdeuos</title>
      <link>https://forem.com/deuos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/deuos"/>
    <language>en</language>
    <item>
      <title>How to Implement Google AdSense into ReactJS - 2025</title>
      <dc:creator>Iamdeuos</dc:creator>
      <pubDate>Sat, 03 May 2025 22:45:15 +0000</pubDate>
      <link>https://forem.com/deuos/how-to-implement-google-adsense-into-reactjs-2025-5g3h</link>
      <guid>https://forem.com/deuos/how-to-implement-google-adsense-into-reactjs-2025-5g3h</guid>
      <description>&lt;p&gt;Many of you guys probably came across the issue I faced while trying to figure out how to implement AdSense into my website. After many Google searches and hours spent on Stack Overflow questions, I finally figured out how to implement AdSense within ReactJS. This tutorial should guide you through the basic setups needed to set up ads on your React website. We’ll be serving ads via Client-side Manual Ads, not server-side. There is a Google API to handle server-side ads. My website, &lt;a href="https://freeenglishpractice.com/" rel="noopener noreferrer"&gt;FreeEnglishPractice.com&lt;/a&gt;, currently uses this method for ads. You might have to go into the lessons to see the ads, by the way.&lt;/p&gt;

&lt;p&gt;Some of the key things needed: Approved Adsense (I had my account approved before I started; I'm not sure if it makes a difference or not, but yeah), and the most important part is this package - &lt;a href="https://www.npmjs.com/package/@ctrl/react-adsense" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/@ctrl/react-adsense&lt;/a&gt;. I didn't create it, but whoever did is a godsend. This package also supports Typescript, so there's no need to worry about it.&lt;br&gt;
Remember to turn off uBlock Origin or any ad blockers for localhost since they will block the AdSense scripts.&lt;/p&gt;

&lt;p&gt;Okay, head over to your Public folder and create an ads.txt file. Then, go to Google AdSense, and it will prompt you with the line of text you should insert to authorize your domain to serve ads. That should be good for your ads.txt.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;google.com, pub-1234, DIRECT, 1234
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, head over to your Public folder and add the following &lt;code&gt;data-adbreak-test="on"&lt;/code&gt; into your Google AdSense Verification script to simulate ads showing up on your localhost. But in the production environment, remember to take it out.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script async
    data-adbreak-test="on"
    src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-123"
    crossorigin="anonymous"&amp;gt;
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Head over to AdSense → Ads → By ad unit. For this example, I will go with Auto Responsive Display ad to show you how you can reside the same ad for desktop and mobile. But it's up to you on what ads you prefer. So, just click on the display unit without changing any of the settings for now.&lt;/p&gt;

&lt;p&gt;This is the example code provided by AdSense; we will need some parts of this code for ours.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-12344"
     crossorigin="anonymous"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;!-- Exampe - Code Provided--&amp;gt;
&amp;lt;ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-12344"
     data-ad-slot="2222222"
     data-ad-format="auto"
     data-full-width-responsive="true"&amp;gt;&amp;lt;/ins&amp;gt;
&amp;lt;script&amp;gt;
     (adsbygoogle = window.adsbygoogle || []).push({});
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Head over to your source folder and create a new component AdsenseExample.tsx. Below is an example of mine that I used. You can view examples of how to use Adsense here: &lt;a href="https://www.npmjs.com/package/@ctrl/react-adsense" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/@ctrl/react-adsense&lt;/a&gt;. What I will show you is responsive for the same ad unit, so less work for both mobile and desktop.&lt;/p&gt;

&lt;p&gt;ClassName will be used for your style sheet. You can name it anything you want. Remember to turn off adTest in production. Fill in the information based on your AdSense code provided.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import { Adsense } from '@ctrl/react-adsense';
import './css/AdsenseStylesheet.css';

function AdsenseExample() {
        //console.log("Ads Shown")
        return (
            &amp;lt;Adsense
                className='ExampleAdSlot'
                client="ca-pub-1234"
                slot="2222222"
                adTest='on' //Dev Only
            /&amp;gt;
        )

    }
}

export default AdsenseExample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your stylesheet, you can change the width and height based on screen size and hide it based on screen changes using media queries. This is an example of a banner ad. Remember to display it as a block; the format works for most manual ads, but I am not sure about the custom feeds ads. You can experiment with that on your own.&lt;/p&gt;

&lt;p&gt;This is an example to show you how it works. Alter it based on your screen sizes and how you would like it to display. Google provides a list of the best sizes for ads and such here: &lt;a href="https://support.google.com/admanager/answer/1100453?hl=en" rel="noopener noreferrer"&gt;https://support.google.com/admanager/answer/1100453?hl=en&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Since AdTest is on, it will not show you any ads; instead, it will display a white rectangle. You can see this by inspecting the element or adding a block border in your CSS to make your life easier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.ExampleAdSlot{
    margin-top: 16px;
    height: 250px;
    width: 970px;
    display: block;
    border: solid; //Remove in Production
}

@media (min-width: 1280px) {
    .ExampleAdSlot{
        display: none !important;
    }
}

@media (max-width: 1000px) {
    .ExampleAdSlot{
        width: 728px;
        height: 90px;
    }
}

@media (max-width: 750px) {
    .ExampleAdSlot{
        width: 336px;
        height: 280px;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's done, and you can call your Ad component in whatever page you want it in. It will work.&lt;/p&gt;

&lt;p&gt;Error times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TagError: adsbygoogle.push() error: No slot size for availableWidth=0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a common error that I encountered at the beginning, but I no longer encounter it after using Google's recommended ad slots. This Stack Overflow post should clarify it more.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/50827616/tagerror-adsbygoogle-push-error-no-slot-size-for-availablewidth-0-when-i-use" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/50827616/tagerror-adsbygoogle-push-error-no-slot-size-for-availablewidth-0-when-i-use&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught TagError: adsbygoogle.push() error: All ins elements in the DOM with class=adsbygoogle already have ads in them.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since you are using ReactJS and you tend to refresh frequently, which I'm guilty of too, this error might pop up here and there. It's a React issue from my experience, as React tends to call everything twice in development mode. I haven't seen this error in production, so it's a React development thing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/74577652/how-to-solve-uncaught-tagerror-adsbygoogle-push-error-all-ins-elements-in-t" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/74577652/how-to-solve-uncaught-tagerror-adsbygoogle-push-error-all-ins-elements-in-t&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's All - Good Luck - Make Money.&lt;/p&gt;

&lt;p&gt;Also comment if you got issues, and lets see if we can figure them out.&lt;/p&gt;

</description>
      <category>react</category>
      <category>ads</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
