【问题标题】:How do I use reactjs with Google DFP/AdSense如何在 Google DFP/AdSense 中使用 reactjs
【发布时间】:2014-10-15 14:27:24
【问题描述】:

我想使用 Facebook 的 reactjs 框架 (JSX) 构建一个项目,但考虑到它的呈现方式,我将如何将它与 Doubleclick for publishers 一起使用?

如果我触发了 adsense/urchin,我如何告诉 React 不要更改/更新这些项目?

我可以使用其他 AdWords 脚本/界面吗?

【问题讨论】:

  • 你实现了吗?可以分享一下吗?
  • @Unenumrated 还没有,被其他一些东西重定向,可能会在 4 月底之前完成......如果你在我的回答中看到评论线程,你可以看到它从哪里开始...我可以将我的解决方案发布为 node/npm 模块,以便更轻松地重复使用它。

标签: javascript adsense reactjs


【解决方案1】:

(当我有一个 jsx 示例时,将用一个 jsx 示例来清理这个答案,现在......)

感谢freenode中的rcs #reactjs:

rcs> tracker1: componentDidMount and make sure 
you're not throwing it away when you don't mean to

tracker1> rcs: so use componentDidMount for adsense/dfp 
binding, and simply don't change the render output?

rcs> tracker1: Yeah. Your render should write out the 
<ins class="adbygoogle" data-analytics-uacct ..... > 
piece, and you should fire off the adsbygoogle.push in 
componentDidMount

tracker1> cool... didn't think it would be that easy for 
some reason...

rcs> tracker1: Or for dfp, handle the defineAdSlot in CDM, 
and googletag.pubads().refresh() on something that fires 
after they're all written out.

rcs> The thing that will trip you up is if you're firing 
things that make React thing that written node needs to get 
removed and replaced, instead of moved, etc.

rcs> But that shouldn't be a primary worry -- just something 
to keep in the back of your head if you're seeing more 
impressions/ad loads than you should be.

tracker1> they'll only change on a navigation/route change

rcs> Keep in mind that adsense TOS is vague on ajax page loads.
rcs> Or 'client side' page loads.

(修正 apos)

【讨论】:

  • 我想知道这是否有效?我试图将代码放在 componentDidMount 中,但它似乎不起作用。
  • 它似乎在我的测试中有效,没有机会进一步挖掘。
  • 哈哈。我现在仍然看到一个空白的白框,不知道这是正常还是什么..
  • &lt;ins className="adsbygoogle" style={{display:'inline-block',width:'336px',height:'280px'}} data-ad-client="ca-pub-11111111" data-ad-slot="11111111"&gt;&lt;/ins&gt; 在 React 组件中,我将类替换为 className 并将样式替换为数组对象,以使其成为标准的 React 组件语法。但似乎谷歌不知道我已将代码放在页面上..
  • @Shih-MinLee 您是否将googletag.pubads().refresh() 放入您的componentDidMount 事件处理程序中?如果您使用 DFP,您应该创建一个容器 div,并使用 googletag.defineSlotgoogletag.display,这就是我为此所做的。
【解决方案2】:

这是一个处理展示广告的简单组件,考虑到viewability best practices。 (感谢Tracker1 提供这里的 cmets,这极大地帮助了我整理这些内容。)

import React from 'react';
import Waypoint from 'react-waypoint';

let instance = 0;

class Ads extends React.Component {
  static propTypes = {
    id: React.PropTypes.string,
    width: React.PropTypes.number.isRequired,
    height: React.PropTypes.number.isRequired,
    adslot: React.PropTypes.string.isRequired
  }

  constructor() {
    this.__id = 'ads-instance-' + ++instance;
    this.displayAd = this.displayAd.bind(this);
  }

  get id() {
    return this.props.id || this.__id;
  }

  componentDidMount() {
    googletag.cmd.push(function() {
      // Define the ad slot
      googletag
        .defineSlot(
          this.props.adslot, 
          [this.props.width, this.props.height], 
          this.id
        )
        .addService(googletag.pubads());

      // Start ad fetching
      googletag.enableServices();
    });
  }
  render() {
    return (
      <div style={{width:this.props.width, height:this.props.height}}>
        <Waypoint onEnter={this.displayAd} />
        <div id={this.id}></div>
      </div>
    );
  }
  displayAd() {
    const id = this.id;
    googletag.cmd.push(function() {
      googletag.display(id);
    });
  }
}

export default Ads;

我使用 react-waypoint 来确保适当的可见性(在用户看到广告之前不加载广告)。当路点在用户向下滚动到视口底部时,displayAd() 会显示广告。偏移量可以增强,但希望这将是一个很好的起点,可以为您提供总体思路。

如果您在一个页面上有多个广告位,则可以使用唯一 ID 代替 #ad-container

【讨论】:

  • 只是一些建议,您可能希望将其包装到一个 npm 模块中,即使它的可用性有限......另外,通过属性传递插槽大小/adslot/id,以及如果未指定,则生成唯一 id。
  • 这是一个非常有用的资源,我正在查看我们移动团队的广告集成,想知道它到底如何与 React 配合得很好,看到它之后就更有意义了,感谢您抽出宝贵时间公布你是如何实现它的!
猜你喜欢
  • 2015-09-27
  • 2012-11-16
  • 2016-01-23
  • 1970-01-01
  • 2013-05-31
  • 2018-02-09
  • 2015-07-29
  • 2019-03-12
  • 1970-01-01
相关资源
最近更新 更多