Skip to content
Advertisement

Can’t get Google Ads to show in React no matter what I do

I’ve been following this example:

https://www.jamesbaum.co.uk/blether/using-google-adsense-with-react/

I have this component:

JavaScript

I have this on my index.html:

JavaScript

When I run my test page I get no ads rendering and this error:

enter image description here

I have no idea what this error means or how to resolve it.

It’s critical to mention that the client and slot IDs work perfectly in a non-react test app So something else must be wrong here.

also, I am testing this via localhost:8080 — which works for the non react test app, so I do not think it’s a localhost/google ads issue.

IN OUR OLD NON-REACT APP

in our <header>:

JavaScript

in our <page.php>:

JavaScript

This produces a working ad right now, even from 127.0.0.1:80 (running via Docker) Our problem is our lack of ability to make this work inside a React component.

Advertisement

Answer

You are getting HTTP 400 (i.e. Bad Request) because the data being sent to google ad servers seems wrong.

The data-ad-client value should have the format like ca-pub-00000000000000 which is obtained from adsense administration page. (I believe this is not your case!!!)

The data you provide, div-gpt-ad seems as an id of a divfor google publisher tag which has the id /164808479/Leaderboard. So in order to use that in your react application properly you can use this library: https://github.com/nfl/react-gpt

So below you can find the modified code which is suitable for your case. Just add react-gptto your react application.

JavaScript

You can see the above code working beautifully in this link: https://codesandbox.io/embed/determined-bash-k30nq

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement