buttler wk
buttler wk

Reputation: 169

Google adsense - Issue in html.js file in gatsby site

My gatsby site got approved from google. i added html.js file and did same as mentioned in the below thread. How to add Adsense to a website built with GatsbyJS?

in prod env (live website), i m getting an error as "unexpected token &" as below:

https://i.sstatic.net/CPyQg.png

On clicking the error message, it is pointing the below code in html.js:

https://i.sstatic.net/iheP1.png

 <script>
          {`
                    (adsbygoogle = window.adsbygoogle || []).push({
                      google_ad_client: "ca-pub-xxxxxxx0300195",
                      enable_page_level_ads: true
                    });
                  `}
        </script>

my html.js code:

import React from "react"
import PropTypes from "prop-types"

export default function HTML(props) {
  return (
    <html {...props.htmlAttributes}>
      <head>
        <meta charSet="utf-8" />
        <meta httpEquiv="x-ua-compatible" content="ie=edge" />
        <meta
          name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no"
        />
        <script
          async
          src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
        ></script>
        <script>
          {`
                    (adsbygoogle = window.adsbygoogle || []).push({
                      google_ad_client: "ca-pub-xxxxxx30300195",
                      enable_page_level_ads: true
                    });
                  `}
        </script>
        {props.headComponents}
      </head>
      <body {...props.bodyAttributes}>
        {props.preBodyComponents}
        <div
          key={`body`}
          id="___gatsby"
          dangerouslySetInnerHTML={{ __html: props.body }}
        />
        {props.postBodyComponents}
      </body>
    </html>
  )
}

HTML.propTypes = {
  htmlAttributes: PropTypes.object,
  headComponents: PropTypes.array,
  bodyAttributes: PropTypes.object,
  preBodyComponents: PropTypes.array,
  body: PropTypes.string,
  postBodyComponents: PropTypes.array,
}

Upvotes: 1

Views: 178

Answers (1)

Ferran Buireu
Ferran Buireu

Reputation: 29320

Use it like:

    <script
      type='text/javascript'
      dangerouslySetInnerHTML={{
        __html: `
         (adsbygoogle = window.adsbygoogle || []).push({
                  google_ad_client: "ca-pub-xxxxxxx0300195",
                  enable_page_level_ads: true
                })`,
      }}
    />

Applied:

import React from "react"
import PropTypes from "prop-types"

export default function HTML(props) {
  return (
    <html {...props.htmlAttributes}>
      <head>
        <meta charSet="utf-8" />
        <meta httpEquiv="x-ua-compatible" content="ie=edge" />
        <meta
          name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no"
        />
        <script
          async
          src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
        ></script>
        <script
          type='text/javascript'
          dangerouslySetInnerHTML={{
            __html: `
             (adsbygoogle = window.adsbygoogle || []).push({
                      google_ad_client: "ca-pub-xxxxxxx0300195",
                      enable_page_level_ads: true
                    })`,
          }}
        />
        {props.headComponents}
      </head>
      <body {...props.bodyAttributes}>
        {props.preBodyComponents}
        <div
          key={`body`}
          id="___gatsby"
          dangerouslySetInnerHTML={{ __html: props.body }}
        />
        {props.postBodyComponents}
      </body>
    </html>
  )
}

HTML.propTypes = {
  htmlAttributes: PropTypes.object,
  headComponents: PropTypes.array,
  bodyAttributes: PropTypes.object,
  preBodyComponents: PropTypes.array,
  body: PropTypes.string,
  postBodyComponents: PropTypes.array,
}

Upvotes: 1

Related Questions