我想知道在哪里添加Google Adsense提供的<script></script>
。他们说要将其添加到<head></head>
中,但在Gatsby中,您可以使用Helmet作为<head>
。 我还尝试在html.js文件中添加脚本,其中包含一个带有{``}
的<head>
标记来转义<script>
标记,但它会在网站顶部输出脚本内容。
TL;DR:使用GatsbyJS构建的网站添加Adsense的最佳方法是什么?
- 我尝试使用react adsense包,但不知道如何与Gatsby一起使用。
- 我尝试将
<script>
标记添加到html.js中,但无法编译。 - 如果使用
{``}
进行转义,则会在网站顶部按原样输出脚本。
<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"
/>
{this.props.headComponents}
{`<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>`}
{` <script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-1540853335472527",
enable_page_level_ads: true
});
</script>
`}
</head>
来源:html.js
网站应该被Google爬虫检测到。
dangerouslySetInnerHTML
来抑制浏览器错误。 - Dr. Kevin Wang<script dangerouslySetInnerHTML={{ __html: '(adsbygoogle = window.adsbygoogle || []).push({google_ad_client: "something",enable_page_level_ads: true});', }} />
- Vic