Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: googletag.display errors when ad slot not in DOM #142

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/Advertising.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export default class Advertising {
typeof this.config.useAPS === 'undefined'
? typeof window.apstag !== 'undefined'
: this.config.useAPS;
this.apsSlotType = this.config.aps && this.config.aps.simplerGPT ? 'gpt' : 'aps';
this.apsSlotType =
this.config.aps && this.config.aps.simplerGPT ? 'gpt' : 'aps';
this.executePlugins('setup');
const { slots, outOfPageSlots, queue, isPrebidUsed, isAPSUsed } = this;
this.setupCustomEvents();
Expand Down Expand Up @@ -194,6 +195,8 @@ export default class Advertising {
this.onError
);
}

this.displaySlots(id);
}

isConfigReady() {
Expand Down Expand Up @@ -367,20 +370,9 @@ export default class Advertising {
}
}

displaySlots() {
displaySlots(id) {
this.executePlugins('displaySlots');
this.config.slots.forEach(({ id }) => {
window.googletag.display(id);
});
}

displayOutOfPageSlots() {
this.executePlugins('displayOutOfPageSlot');
if (this.config.outOfPageSlots) {
this.config.outOfPageSlots.forEach(({ id }) => {
window.googletag.display(id);
});
}
window.googletag.display(id);
}

refreshInterstitialSlot() {
Expand Down Expand Up @@ -425,8 +417,6 @@ export default class Advertising {
pubads.enableSingleRequest();

window.googletag.enableServices();
this.displaySlots();
this.displayOutOfPageSlots();
this.refreshInterstitialSlot();
}

Expand Down
48 changes: 22 additions & 26 deletions src/Advertising.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,10 @@ describe('When I instantiate an advertising main module, with Prebid', () => {
void it('are enabled', () =>
expect(global.googletag.enableServices).toHaveBeenCalledTimes(1)));
//----------------------------------------------------------------------------------------------------
describe('the display method of GPT', () => {
it('is called for each slot', () =>
expect(global.googletag.display).toHaveBeenCalledTimes(2));
it('is called with the DIV ID of the “foo” ad', () =>
expect(global.googletag.display).toHaveBeenCalledWith(DIV_ID_FOO));
it('is called with the DIV ID of the “bar” ad', () =>
expect(global.googletag.display).toHaveBeenCalledWith(DIV_ID_BAR));
});
describe('the slots of the advertising module instance', () =>
void it('are correct', () =>
expect(advertising.slots).toMatchSnapshot()));
void it('are correct', () => {
expect(advertising.slots).toMatchSnapshot();
}));
describe('the GPT size mappings of the advertising module instance', () =>
void it('are correct', () =>
expect(advertising.gptSizeMappings).toMatchSnapshot()));
Expand Down Expand Up @@ -411,6 +404,13 @@ describe('When I instantiate an advertising main module, with Prebid', () => {
expect(
global.googletag.pubads().refresh.mock.calls
).toMatchSnapshot()));

describe('the display method of GPT', () => {
it('is called once', () =>
expect(global.googletag.display).toHaveBeenCalledTimes(1));
it('is called with the DIV ID of the “foo” ad', () =>
expect(global.googletag.display).toHaveBeenCalledWith(DIV_ID_FOO));
});
});
describe('and I activate the “foo” ad with a custom events object to collapse its slot', () => {
let collapse;
Expand Down Expand Up @@ -467,9 +467,12 @@ describe('When I instantiate an advertising main module', () => {
describe('the targeting for asynchronous GPT', () =>
void it('is not set', () =>
expect(global.pbjs.setTargetingForGPTAsync).toHaveBeenCalledTimes(0)));
describe('the ad slot', () =>
describe('the ad slot', () => {
void it('is not refreshed', () =>
expect(global.googletag.pubads().refresh).toHaveBeenCalledTimes(0)));
expect(global.googletag.pubads().refresh).toHaveBeenCalledTimes(0));
void it('is not displayed', () =>
expect(global.googletag.pubads().display).toHaveBeenCalledTimes(0));
});
describe('and I call the setup method', () => {
beforeEach(() => advertising.setup());
describe('a bid', () =>
Expand Down Expand Up @@ -573,12 +576,7 @@ describe('When I instantiate an advertising main module with plugins', () => {
describe("the plugin's hook for GPT teardown", () =>
void it('is not called', () =>
expect(plugins[0].teardownGpt).toHaveBeenCalledTimes(0)));
describe("the plugin's hook for displaying slots", () =>
void it('is called', () =>
expect(plugins[0].displaySlots).toHaveBeenCalled()));
describe("the plugin's hook for displaying outOfPage slots", () =>
void it('is called', () =>
expect(plugins[0].displayOutOfPageSlot).toHaveBeenCalled()));

describe("the plugin's hook for refreshing interstitial slot", () =>
void it('is called', () =>
expect(plugins[0].refreshInterstitialSlot).toHaveBeenCalled()));
Expand Down Expand Up @@ -822,14 +820,6 @@ describe('When I instantiate an advertising main module without Prebid.js or APS
void it('are enabled', () =>
expect(global.googletag.enableServices).toHaveBeenCalledTimes(1)));
//----------------------------------------------------------------------------------------------------
describe('the display method of GPT', () => {
it('is called for each slot', () =>
expect(global.googletag.display).toHaveBeenCalledTimes(2));
it('is called with the DIV ID of the “foo” ad', () =>
expect(global.googletag.display).toHaveBeenCalledWith(DIV_ID_FOO));
it('is called with the DIV ID of the “bar” ad', () =>
expect(global.googletag.display).toHaveBeenCalledWith(DIV_ID_BAR));
});
describe('the slots of the advertising module instance', () =>
void it('are correct', () =>
expect(advertising.slots).toMatchSnapshot()));
Expand All @@ -855,6 +845,12 @@ describe('When I instantiate an advertising main module without Prebid.js or APS
expect(
global.googletag.pubads().refresh.mock.calls
).toMatchSnapshot()));
describe('the display method of GPT', () => {
it('is called for each slot', () =>
expect(global.googletag.display).toHaveBeenCalledTimes(1));
it('is called with the DIV ID of the “foo” ad', () =>
expect(global.googletag.display).toHaveBeenCalledWith(DIV_ID_FOO));
});
});
});
afterEach(() => {
Expand Down
98 changes: 4 additions & 94 deletions src/__snapshots__/Advertising.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,12 @@ Array [
Array [
"div-gpt-ad-foo",
],
Array [
"div-gpt-ad-bar",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
],
},
"enableServices": [MockFunction] {
Expand Down Expand Up @@ -1040,26 +1033,7 @@ Object {
},
],
},
"display": [MockFunction] {
"calls": Array [
Array [
"div-gpt-ad-foo",
],
Array [
"div-gpt-ad-bar",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
],
},
"display": [MockFunction],
"enableServices": [MockFunction] {
"calls": Array [
Array [],
Expand Down Expand Up @@ -1688,26 +1662,7 @@ Object {
},
],
},
"display": [MockFunction] {
"calls": Array [
Array [
"div-gpt-ad-foo",
],
Array [
"div-gpt-ad-bar",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
],
},
"display": [MockFunction],
"enableServices": [MockFunction] {
"calls": Array [
Array [],
Expand Down Expand Up @@ -2546,19 +2501,12 @@ Array [
Array [
"div-gpt-ad-foo",
],
Array [
"div-gpt-ad-bar",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
],
},
"enableServices": [MockFunction] {
Expand Down Expand Up @@ -3407,26 +3355,7 @@ Object {
},
],
},
"display": [MockFunction] {
"calls": Array [
Array [
"div-gpt-ad-foo",
],
Array [
"div-gpt-ad-bar",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
],
},
"display": [MockFunction],
"enableServices": [MockFunction] {
"calls": Array [
Array [],
Expand Down Expand Up @@ -4055,26 +3984,7 @@ Object {
},
],
},
"display": [MockFunction] {
"calls": Array [
Array [
"div-gpt-ad-foo",
],
Array [
"div-gpt-ad-bar",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
Object {
"type": "return",
"value": undefined,
},
],
},
"display": [MockFunction],
"enableServices": [MockFunction] {
"calls": Array [
Array [],
Expand Down
Loading