Skip to content

Commit

Permalink
Fixes after PR (using isGptPubadsDefined and window.googletag instead…
Browse files Browse the repository at this point in the history
… of window.top.googletag)
  • Loading branch information
ym-dlabuzov committed Oct 19, 2021
1 parent fd1cec9 commit 6751fd0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
16 changes: 10 additions & 6 deletions modules/yieldmoSyntheticInventoryModule.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { config } from '../src/config.js';
import { isGptPubadsDefined } from '../src/utils.js';

export const MODULE_NAME = 'Yieldmo Synthetic Inventory Module';

export function init(config) {
validateConfig(config);

window.top.googletag = window.top.googletag || {cmd: []};
if (!isGptPubadsDefined()) {
window.googletag = window.googletag || {};
window.googletag.cmd = window.googletag.cmd || [];
}

const googletag = window.top.googletag;
const googletag = window.googletag;
const containerName = 'ym_sim_container_' + config.placementId;

googletag.cmd.push(() => {
if (window.top.document.body) {
if (window.document.body) {
googletagCmd(config, containerName, googletag);
} else {
document.addEventListener('DOMContentLoaded', () => googletagCmd(config, containerName, googletag));
window.document.addEventListener('DOMContentLoaded', () => googletagCmd(config, containerName, googletag));
}
});
}
Expand All @@ -29,9 +33,9 @@ export function validateConfig(config) {
}

function googletagCmd(config, containerName, googletag) {
const gamContainer = window.top.document.createElement('div');
const gamContainer = window.document.createElement('div');
gamContainer.id = containerName;
window.top.document.body.appendChild(gamContainer);
window.document.body.appendChild(gamContainer);
googletag.defineSlot(config.adUnitPath, [1, 1], containerName)
.addService(googletag.pubads())
.setTargeting('ym_sim_p_id', config.placementId);
Expand Down
15 changes: 8 additions & 7 deletions test/spec/modules/yieldmoSyntheticInventoryModule_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const mockedYmConfig = {
};

const setGoogletag = () => {
window.top.googletag = {
window.googletag = {
cmd: [],
defineSlot: sinon.stub(),
addService: sinon.stub(),
Expand All @@ -20,20 +20,21 @@ const setGoogletag = () => {
enableServices: sinon.stub(),
display: sinon.stub(),
};
window.top.googletag.defineSlot.returns(window.top.googletag);
window.top.googletag.addService.returns(window.top.googletag);
return window.top.googletag;
window.googletag.defineSlot.returns(window.googletag);
window.googletag.addService.returns(window.googletag);
window.googletag.pubads.returns({getSlots: sinon.stub()});
return window.googletag;
}

describe('Yieldmo Synthetic Inventory Module', function() {
let config = Object.assign({}, mockedYmConfig);

beforeEach(function () {
delete window.top.googletag;
delete window.googletag;
});

afterEach(function () {
delete window.top.googletag;
delete window.googletag;
});

it('should be enabled with valid required params', function() {
Expand Down Expand Up @@ -78,7 +79,7 @@ describe('Yieldmo Synthetic Inventory Module', function() {
expect(gtag.display.getCall(0).args[0]).to.exist.and.to.equal(containerName);
expect(gtag.pubads.getCall(0)).to.not.be.null;

const gamContainerEl = window.top.document.getElementById(containerName);
const gamContainerEl = window.document.getElementById(containerName);
expect(gamContainerEl).to.not.be.null;

gamContainerEl.parentNode.removeChild(gamContainerEl);
Expand Down

0 comments on commit 6751fd0

Please sign in to comment.