React DFP Documentation

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

 setCollapseEmptyDivs

Use the method setCollapseEmptyDivs([boolean]) to nables collapsing of slot divs when there is no ad content to display.

The method accepts one parameter that expects the following values:

  1. false : collapse slots after ads are fetched.
  2. true : collapse divs before ads are fetched.
  3. null/undefined : do not collapse divs.

Important note: although this method will be always available and is meant for 'public usage',
<DFPSlotProvider collapseEmptyDivs /> is the recomended way to approach this behavior, please use that one one if possible.

Example:

ADS GO HERE...

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
import React, { Component } from 'react';
import { AdSlot, DFPManager } from 'react-dfp';


export default class Example extends Component {
  componentDidMount() {
    DFPManager.setCollapseEmptyDivs(true);
    DFPManager.load();
  }

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

Notes:

  1. Lines 14,29: we define two ad slots using the <AdSlot>component.
  2. Lines 7-8: we enable divs collapsing, using the method:
    DFPManager.setCollapseEmptyDivs(true) , we pass a 'true' which means: 'collapse divs before fetching the ads'.