我如何使用reactjs与谷歌DFP/AdSense



我想用Facebook的reactjs框架(JSX)构建一个项目,但考虑到它的呈现方式,我该如何使用它与出版商的双击?

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

是否有一个替代的adwords脚本/界面我可以使用?

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

感谢rcs在freenode #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.

(修复apo)

这是一个处理显示广告的简单组件,考虑到最佳的可见性实践。(感谢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

最新更新