【问题标题】:How to embed Google Ad Sense into React Component?如何将 Google Adsense 嵌入到 React 组件中?
【发布时间】:2015-05-17 01:36:00
【问题描述】:

手头的问题专门针对 Google Ad Sense,但可能适用于任何脚本标记插入。我不明白如何将这样的东西添加到我的组件中。

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                            <!-- My Ad-->
                            <ins class="adsbygoogle"
                                 style="display:block"
                                 data-ad-client="ca-pub-24524524"
                                 data-ad-slot="152452452"
                                 data-ad-format="auto"></ins>
                            <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                            </script>

这样的事情可能吗?

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

这样的事情不需要第三方脚本。

Ad-Sense 需要一个看起来像这样的第三方脚本,这应该在反应之前加载,或者如果您使用任何类型的模板(即 django 模板等...)

将其放入模板中:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

然后将您的广告意义包括在内,删除评论(假设您正在使用 JSX)并转换 google 给您的内容

谷歌给了你什么:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> 
<!-- yourAdname-->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-23452425"
     data-ad-slot="24524524"
     data-ad-format="auto">
</ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

改成这样:

<ins className="adsbygoogle"
     style={{display:'block'}}
     data-ad-client="ca-pub-23452425"
     data-ad-slot="24524524"
     data-ad-format="auto">
</ins>

然后把实际的执行代码放到componentDidMount函数中

(adsbygoogle = window.adsbygoogle || []).push({});

我认为类似的解决方案几乎适用于任何相同的情况,而不仅仅是 Ad-Sense。

一个常见的误解是人们通常认为他们可以将脚本标记放在危险的SetInnerHTML 属性中,但事实并非如此。它使用不会执行脚本标签的 setInnerHTML。

【讨论】:

  • 视频不可用。解决方案是否改变了?
  • 感谢@Chris Hawkes - 这正是我需要的新 React 站点。
【解决方案2】:

根据 Chris Hawkes 的精彩回答,这里是适用于 Typescript 的解决方案。

按照上述步骤操作后,您将在componentDidMount() 中收到错误,因为编译器不知道adsbygoogle 变量和Window 对象上的adsbygoogle 属性。

要解决这些问题:

  1. declare var adsbygoogle: any; 全局(我在 index.tsx 中完成,首先是所有组件)
  2. componentDidMount(),写(adsbygoogle = (window as any).adsbygoogle || []).push({});

【讨论】:

    【解决方案3】:

    也许你可以使用这个反应组件:react-adsence。支持谷歌和百度。

    Google AdSence 为您提供以下广告代码:

    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <!-- adapte_ad -->
    <ins class="adsbygoogle"
         style="display:block"
         data-ad-client="ca-pub-7292810486004926"
         data-ad-slot="9220497478"
         data-ad-format="auto"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    

    那么如何将它包含在 React 组件中呢?很简单:

    1. 编写一个组件类,并用styleclientslotformat 为其提供道具。
    2. 在 componentDidMount 生命周期方法中执行 (adsbygoogle = window.adsbygoogle || []).push({});

    然后获取react-adsence。怎么用?

    import AdSence from 'react-adsence';
    
    <AdSence.Google client='ca-pub-7292810486004926'
                    slot='7806394673' />
    

    在此之前,您应该在 HTML 中添加script

    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    

    注意:用你的修改客户端/槽。

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 2010-10-07
      • 2022-01-04
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 2021-01-20
      相关资源
      最近更新 更多