问题 如何在Google DFP / AdSense中使用reactjs


我想用Facebook的reactjs框架(JSX)构建一个项目,但考虑到它的呈现方式,我如何在发布者的Doubleclick中使用它?

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

我可以使用替代adwords脚本/界面吗?


4264
2017-08-21 20:08


起源

你实现了吗?你能分享吗? - Unenumrated
@Unenumrated还没有,被重定向到其他一些东西,可能会在4月底之前实现这个...如果你在我的答案上看到评论帖,你可以看到它开始的地方......我可以发布我的解决方案作为节点/ npm模块,因此可以更容易地重复使用。 - Tracker1


答案:


这是一个处理显示广告的简单组件,并考虑到了这一点 可见性最佳实践。 (谢谢 Tracker1 对于这里的评论,这极大地帮助了我把它们放在一起。)

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


4
2018-05-16 18:53



只是一些建议,你可能想把它包装成一个npm模块,即使它的可用性有限......也可以通过属性传递插槽大小/ adslot / id,以及如果一个人没有生成一个唯一的id没有指明。 - Tracker1
这是一个非常有用的资源,我正在浏览我们的移动团队的广告整合,想知道它如何在反应中发挥得很好,这看起来更有意义,所以感谢花时间发布你是如何实现它的! - Lux.Capacitor


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

感谢freenode #reactjs中的rcs:

rcs> tracker1: componentDidMount and make sure 
you&aposre not throwing it away when you don&apost mean to

tracker1> rcs: so use componentDidMount for adsense/dfp 
binding, and simply don&apost 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&apost 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&aposre all written out.

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

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

tracker1> they&aposll only change on a navigation/route change

rcs> Keep in mind that adsense TOS is vague on ajax page loads.
rcs> Or &aposclient side&apos page loads.

(固定)


7
2017-08-21 22:02



我想知道这是否有效?我试图将代码放在componentDidMount中,但它似乎不起作用。 - Shih-Min Lee
它似乎在我的测试中起作用,从未有机会进一步挖掘。 - Tracker1
哈哈了。我现在仍然看到一个空白的白色盒子,如果这是正常的话还是dunno ... - Shih-Min Lee
<ins className="adsbygoogle" style={{display:'inline-block',width:'336px',height:'280px'}} data-ad-client="ca-pub-11111111" data-ad-slot="11111111"></ins> 在React组件中,我将类替换为className,将样式替换为数组对象,使其成为标准的React组件语法。但似乎谷歌不知道我已经把代码放在页面上虽然.. - Shih-Min Lee
@ Shih-MinLee你放了吗? googletag.pubads().refresh() 在你的 componentDidMount 事件处理程序如果您使用的是DFP,则应创建容器div,然后使用 googletag.defineSlot 和 googletag.display这就是我为此所做的。 - Tracker1