5

I have a website front-end running on React.js.

I have added the following line to index.html's head tag.

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

The following code presents me with the error, "'adsbygoogle' is not defined no-undef"

componentDidMount () {
        (adsbygoogle = window.adsbygoogle || []).push({
              google_ad_client: "ca-pub-9142841210062390",
              enable_page_level_ads: true
         });
}

How can I get advertisements to display on my React.js web application?

This is with Google Auto Ads, not ad units.

  • Possible duplicate of [How to embed google adsense in react js](https://stackoverflow.com/questions/46035999/how-to-embed-google-adsense-in-react-js) – Hardik Modha Sep 07 '18 at 13:13
  • @HardikModha Modha As I've specified, placing the code in componentDidMount() results in my issue. The solution to that post doesn't help me. I am asking with regards to auto ads, not ad units. –  Sep 07 '18 at 13:43
  • That error looks like an eslint error, you could just disable it with `// eslint-disable no-undef` – Yaobin Then Oct 19 '18 at 00:25

1 Answers1

6

You have to write "window.adsbygoogle" in reactjs application instead of "adsbygoogle"

componentDidMount () {
    (window.adsbygoogle = window.adsbygoogle || []).push({
          google_ad_client: "ca-pub-9142841210062390",
          enable_page_level_ads: true
     });
}