如何在Google DFP/AdSense中使用reactjs

Tra*_*er1 12 javascript adsense reactjs

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

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

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

Tra*_*er1 7

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

感谢freenode #reactjs中的rcs:

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.
Run Code Online (Sandbox Code Playgroud)

(固定)

  • 我想知道这是否有效?我试图将代码放在componentDidMount中,但它似乎不起作用. (2认同)
  • `<ins className ="adsbygoogle"style = {{display:'inline-block',width:'336px',height:'280px'}} data-ad-client ="ca-pub-11111111"data-ad-在React组件中,slot ="11111111"> </ ins>`我将类替换为className,将样式替换为数组对象,使其成为标准的React组件语法.但似乎谷歌不知道我已经把代码放在页面上虽然.. (2认同)

小智 5

这是一个处理展示广告的简单组件,同时考虑了可见度最佳做法。(感谢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;
Run Code Online (Sandbox Code Playgroud)

我已经使用react-waypoint来确保适当的可见性(直到用户看到广告才加载广告)。当用户向下滚动时,航点到达视口底部时,displayAd()会显示广告。可以增加偏移量,但是希望这将是一个让您有大致了解的良好起点。

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