React DFP Documentation

  ·  Build Status Npm Downloads Minizipped size GitHub license Dependencies
Minizipped size

 getAdSenseAttribute

Use the method getAdSenseAttribute(name) to read the value of an arbitrary AdSense attribute. Attributes can be set via <DFPSlotsProvider adSenseAttributes={} /> or via DFPManager.setAdSenseAttributes({...attributes}) , or ultimately via DFPManager.setAdSenseAttribute(name, value).

See google gpt docs for details:  link

Example:

ADS GO HERE...
SiteUrl:

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React, { Component } from 'react';
import { AdSlot, DFPManager } from 'react-dfp';


export default class Example extends Component {
  constructor(props) {
    super(props);
    this.state = {
      siteUrl: DFPManager.getAdSenseAttribute('site_url'),
    };
  }

  componentDidMount() {
    DFPManager.setAdSenseAttributes({ site_url: 'foo.bar.baz' });
    DFPManager.load();
    this.setState({
      siteUrl: DFPManager.getAdSenseAttribute('site_url'),
    });
  }

  render() {
    return (
      <div>
        <div className="desktop-ads">
          <AdSlot slotId="test1" dfpNetworkId="9999" sizes={[[728, 90], [300, 250]]} adUnit="homepage/1" />
        </div>
        <div className="mobile-ads">
          <AdSlot slotId="test2" dfpNetworkId="9999" sizes={[[320, 50], [300, 50]]} adUnit="homepage/mobile" />
        </div>
        <div>
          SiteUrl: {this.state.siteUrl}
        </div>
      </div>
    );
  }
}

Notes:

  1. Line 9: we read the current value fo the site_url attribute using the method  DFPManager.getAdSenseAttribute() and store it in the state variable 'siteUrl'.
  2. Lines 25,28: we define two ad slots using the <AdSlot>component.
  3. Lines 14-18: we configure the adSense attribute site_url for the page using the method:
    DFPManager.setAdSenseAttributes({...attrs}) , and load the ads in this page using
    DFPManager.load([id]),   then we read the new/current value of the AdSense attribute 'site_url' that we've just set using the method: DFPManager.getAdSenseAttribute(name) and finally update the state variable 'siteUrl'.
  4. Line 31: we print the vaue of the state variable: 'siteUrl'.