1

Google AdSense give me a script to added in the head tag

<script data-ad-client="ca-pub-XXXXXXXXXXXXXXX" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

my code:

<Helmet>
  <script
   data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
   async
   src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
   ></script>
</Helmet>

return me this error

AdSense head tag doesn't support data-react-helmet attribute. how can I fix that?

enter image description here

2
  • You don't need to fix it. It's a warning, not an error. It just tells that you AdSense JS will ignore that attribute. Usually data- attributes set on <script> tag used to change AdSense ads behavior. So that "head tag doesn't support" message is just to help developers detect errors if they misspelled attribute names. Commented Oct 5, 2020 at 20:54
  • @MikitaBelahlazau Thanks for you but sry I forget to add the image of console error can see it plz. Commented Oct 6, 2020 at 8:40

1 Answer 1

3

I fixed by adding the script to the html.js I need the head tag to be injected in a global context inside every page so I put it in the html.js file then I can add the body tag whenever I want.

Another way is to add the script in the gatsby-ssr.js like this:

const React = require("react")

const HeadComponents = [
 <script
   key="1-http-ads"
   data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
   async
   src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
   />,
]

exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
  setHeadComponents(HeadComponents)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.