Skip to content

Commit

Permalink
ability to enable/disable singleRequest mode. (#91)
Browse files Browse the repository at this point in the history
- new prop and dfpManager methods.
 - docs.
 - unittests.
  • Loading branch information
jaanauati authored Dec 29, 2018
1 parent a4ecaca commit 6ace158
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ DFPManager.load();

| Property | Type | Example | Description |
| ------------------ | ------------- | ----------- | ------- |
| autoLoad | bool (default true) | ``` { false } ``` | Tell to the provider if it should load the ads when the slots are mounted. |
| autoLoad | boolean (default true) | ``` { false } ``` | Tell to the provider if it should load the ads when the slots are mounted. |
| dfpNetworkId | string | ``` "1122" ``` | DFP Account id. |
| singleRequest | boolean (default true) | ```<DFPSlotsProvider singleRequest=false> ... </DFPSlotsProvider>``` | Enables or disables the gpt's singleRequest feature. |
| adUnit | string | ``` "homepage" ``` | The adunit you want to target the boxes (children / contained boxes). |
| sizeMapping | array of objects. | ```{ [ {viewport: [1024, 768], sizes:[[728, 90], [300, 250]]}, {viewport: [900, 768], sizes:[[300, 250]] }] } ``` | Set the size mappings to be applied to the nested ad slots. |
| adSenseAttributes | object | ``` { "site_url": "my.site.com", ... } ``` | Object with adSense attributes that will be applied globaly (see: https://developers.google.com/doubleclick-gpt/adsense_attributes). |
Expand Down
3 changes: 3 additions & 0 deletions js/dfpslotsprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class DFPSlotsProvider extends React.Component {
]).isRequired,
autoLoad: PropTypes.bool,
dfpNetworkId: PropTypes.string.isRequired,
singleRequest: PropTypes.bool,
adUnit: PropTypes.string,
sizeMapping: PropTypes.arrayOf(PropTypes.object),
adSenseAttributes: PropTypes.object,
Expand All @@ -31,6 +32,7 @@ export default class DFPSlotsProvider extends React.Component {

static defaultProps = {
autoLoad: true,
singleRequest: true,
collapseEmptyDivs: null,
};

Expand All @@ -52,6 +54,7 @@ export default class DFPSlotsProvider extends React.Component {
}

componentDidMount() {
DFPManager.configureSingleRequest(this.props.singleRequest);
DFPManager.setAdSenseAttributes(this.props.adSenseAttributes);
DFPManager.setCollapseEmptyDivs(this.props.collapseEmptyDivs);
if (this.props.autoLoad && !this.loadAdsIfPossible()) {
Expand Down
13 changes: 12 additions & 1 deletion js/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ let loadAlreadyCalled = false;
let loadInProgress = false;
let loadPromise = null;
let googleGPTScriptLoadPromise = null;
let singleRequestEnabled = true;
const registeredSlots = {};
let managerAlreadyInitialized = false;
const globalTargetingArguments = {};
const globalAdSenseAttributes = {};

const DFPManager = Object.assign(new EventEmitter().setMaxListeners(0), {

singleRequestIsEnabled() {
return singleRequestEnabled;
},

configureSingleRequest(value) {
singleRequestEnabled = !!value;
},

getAdSenseAttribute(key) {
return globalAdSenseAttributes[key];
},
Expand Down Expand Up @@ -197,7 +206,9 @@ const DFPManager = Object.assign(new EventEmitter().setMaxListeners(0), {
});

googletag.cmd.push(() => {
googletag.pubads().enableSingleRequest();
if (this.singleRequestIsEnabled()) {
googletag.pubads().enableSingleRequest();
}

if (this.collapseEmptyDivs === true || this.collapseEmptyDivs === false) {
googletag.pubads().collapseEmptyDivs(this.collapseEmptyDivs);
Expand Down
4 changes: 4 additions & 0 deletions lib/dfpslotsprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ function (_React$Component) {
}, {
key: "componentDidMount",
value: function componentDidMount() {
_manager.default.configureSingleRequest(this.props.singleRequest);

_manager.default.setAdSenseAttributes(this.props.adSenseAttributes);

_manager.default.setCollapseEmptyDivs(this.props.collapseEmptyDivs);
Expand Down Expand Up @@ -113,6 +115,7 @@ _defineProperty(DFPSlotsProvider, "propTypes", {
children: _propTypes.default.oneOfType([_propTypes.default.element, _propTypes.default.array]).isRequired,
autoLoad: _propTypes.default.bool,
dfpNetworkId: _propTypes.default.string.isRequired,
singleRequest: _propTypes.default.bool,
adUnit: _propTypes.default.string,
sizeMapping: _propTypes.default.arrayOf(_propTypes.default.object),
adSenseAttributes: _propTypes.default.object,
Expand All @@ -131,5 +134,6 @@ _defineProperty(DFPSlotsProvider, "childContextTypes", {

_defineProperty(DFPSlotsProvider, "defaultProps", {
autoLoad: true,
singleRequest: true,
collapseEmptyDivs: null
});
11 changes: 10 additions & 1 deletion lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ var loadAlreadyCalled = false;
var loadInProgress = false;
var loadPromise = null;
var googleGPTScriptLoadPromise = null;
var singleRequestEnabled = true;
var registeredSlots = {};
var managerAlreadyInitialized = false;
var globalTargetingArguments = {};
var globalAdSenseAttributes = {};
var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), {
singleRequestIsEnabled: function singleRequestIsEnabled() {
return singleRequestEnabled;
},
configureSingleRequest: function configureSingleRequest(value) {
singleRequestEnabled = !!value;
},
getAdSenseAttribute: function getAdSenseAttribute(key) {
return globalAdSenseAttributes[key];
},
Expand Down Expand Up @@ -220,7 +227,9 @@ var DFPManager = Object.assign(new _events.EventEmitter().setMaxListeners(0), {
});
});
googletag.cmd.push(function () {
googletag.pubads().enableSingleRequest();
if (_this3.singleRequestIsEnabled()) {
googletag.pubads().enableSingleRequest();
}

if (_this3.collapseEmptyDivs === true || _this3.collapseEmptyDivs === false) {
googletag.pubads().collapseEmptyDivs(_this3.collapseEmptyDivs);
Expand Down
83 changes: 83 additions & 0 deletions spec/test-dfpslotsprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,89 @@ describe('DFPSlotsProvider', () => {
sinon.assert.calledWithMatch(DFPManager.registerSlot, { ...providerProps, ...compProps });
});

it('Gets singleRequest enabled by default', () => {
const providerProps = {
dfpNetworkId: '1000',
adUnit: 'foo/bar/baz',
};

const compProps = {
slotId: 'testElement1',
sizes: [[728, 90]],
};

TestUtils.renderIntoDocument(
<DFPSlotsProvider {...providerProps}>
<AdSlot {...compProps} />
</DFPSlotsProvider>,
);

expect(DFPManager.singleRequestIsEnabled()).equal(true);
});

it('Can disable singleRequest', () => {
const providerProps = {
dfpNetworkId: '1000',
adUnit: 'foo/bar/baz',
singleRequest: false,
};

const compProps = {
slotId: 'testElement1',
sizes: [[728, 90]],
};

TestUtils.renderIntoDocument(
<DFPSlotsProvider {...providerProps}>
<AdSlot {...compProps} />
</DFPSlotsProvider>,
);

expect(DFPManager.singleRequestIsEnabled()).equal(false);
});

it('Can enable singleRequest', () => {
const providerProps = {
dfpNetworkId: '1000',
adUnit: 'foo/bar/baz',
singleRequest: true,
};

const compProps = {
slotId: 'testElement1',
sizes: [[728, 90]],
};

TestUtils.renderIntoDocument(
<DFPSlotsProvider {...providerProps}>
<AdSlot {...compProps} />
</DFPSlotsProvider>,
);

expect(DFPManager.singleRequestIsEnabled()).equal(true);
});

it('Disables singleRequest', () => {
const providerProps = {
dfpNetworkId: '1000',
adUnit: 'foo/bar/baz',
singleRequest: false,
};

const compProps = {
slotId: 'testElement1',
sizes: [[728, 90]],
};

TestUtils.renderIntoDocument(
<DFPSlotsProvider {...providerProps}>
<AdSlot {...compProps} />
</DFPSlotsProvider>,
);

expect(DFPManager.singleRequestIsEnabled()).equal(false);
});

it('Registers a refreshable AdSlot', () => {
const providerProps = {
dfpNetworkId: '1000',
Expand Down
14 changes: 14 additions & 0 deletions spec/test-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import { DFPManager } from '../lib';

describe('DFPManager', () => {

describe('Single Request', () => {
it('Gets singleRequest enabled by default', function registersAdSlot() {
expect(DFPManager.singleRequestIsEnabled()).equal(true);
});
it('Can disable singleRequest', function registersAdSlot() {
DFPManager.configureSingleRequest(false);
expect(DFPManager.singleRequestIsEnabled()).equal(false);
});
it('Can enable singleRequest', function registersAdSlot() {
DFPManager.configureSingleRequest(true);
expect(DFPManager.singleRequestIsEnabled()).equal(true);
});
});

describe('AdSense attributes', () => {
beforeEach(function beforeEach() {
this.argsList1 = {
Expand Down

0 comments on commit 6ace158

Please sign in to comment.