【发布时间】:2019-06-15 02:26:07
【问题描述】:
我想知道应该在哪里添加 Google Adsense 提供的 <script></script>。
他们说将其添加到 <head></head>,但在 Gatsby 中,您将 Helmet 设置为 <head>。
我还尝试将脚本添加到 html.js 文件中,该文件位于带有 {``} 的 <head> 标记以转义 <script> 标记,但它会在网站顶部输出脚本内容。
TL;DR:将 Adsense 添加到使用 GatsbyJS 构建的网站的最佳方式是什么?
- 我尝试使用 react adsense package,但我不明白如何将它与 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抑制浏览器错误。 -
<script dangerouslySetInnerHTML={{ __html: '(adsbygoogle = window.adsbygoogle || []).push({google_ad_client: "something",enable_page_level_ads: true});', }} />
标签: reactjs jsx adsense gatsby head