Adsense ads not working in React Danderously set Innerhtml

126 Views Asked by At

I have some text which I am tryint to show using Dangerously set Inner html. The text is pretty long and changes based on user query, so I am using danderously set inner html to show it

I want to show Adsense ads in between these paragraphs, but that doesnt seem to be working.

in the text shown in dangerously set html, I have also included adsense script to show ads after every few paragraphs, and when inspected in chrome, I can see the script is at the right place too, but to doesnt show any ads.

There is a long fine called Chapter1.html which I am showing using dangerously set inner html, here is a small part of it:

<p>The hour’s just 9 o’clock.
    </p>
    <p>I should do laundry, clean and spend my time in the morning relaxing.
    </p>
    <p></p>
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7107200228987000"
        crossorigin="anonymous"></script>
    <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article"
        data-ad-format="fluid" data-ad-client="ca-pub-7107200228987000" data-ad-slot="9551463795"></ins>
    <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    <h3>Part 1</h3>
    <p>
        Inside Keyaki Mall in the forenoon, the atmosphere of Christmas Eve was in
        full swing.
    </p>
    <p>Inside the mall was decorated with even more gaudy ornaments than at the
        entrance.
    </p>

I cant find anything reliable on the internet, so I am here. any help is welcome.

1

There are 1 best solutions below

1
Mikita Belahlazau On

Few notes.

  1. Move <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7107200228987000" crossorigin="anonymous"></script> out of that snippet and put it in <head> of your site once.

  2. I believe using <script>(adsbygoogle = window.adsbygoogle || []).push({}); </script> with innerHTML doesn't work. innerHTML doesn't evaluate inlined <script> tags. Instead you need to call adsbygoogle.push({}) yourself after you set innerHTML.