React DFP Documentation

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

 getAdSenseAttributes

Use the method getAdSenseAttributes() to read the set of AdSense attributes configured either via <DFPSlotsProvider adSenseAttributes={} /> or via DFPManager.setAdSenseAttributes({...attributes})

See google gpt docs for details:  link

Example:

ADS GO HERE...
AdSense attributes: {}

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 = {
      adSenseAttributes: DFPManager.getAdSenseAttributes(),
    };
  }

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

  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>
          AdSense attributes: {JSON.stringify(this.state.adSenseAttributes)}
        </div>
      </div>
    );
  }
}

Notes:

  1. Line 9: we read the current set of AdSense attributes using  DFPManager.getAdSenseAttributes() and store them in the state variable 'adSenseAttributes'.
  2. Lines 25,28: we define two ad slots using the <AdSlot>component.
  3. Lines 14-18: we configure the adSense attributes site_url for the page using the method:
    DFPManager.setAdSenseAttributes() , and load the ads in this page using
    DFPManager.load([id]),   then we read the current set of AdSense attributes using the method: DFPManager.getAdSenseAttributes() and finally update the state variable 'adSenseAttributes'.
  4. Line 31: we print the vaue of the state variable: 'adSenseAttributes'.