Skip to content

Commit 7f9a989

Browse files
authored
Merge pull request #21 from blakemcintyre/fix-empty-adSlot-object-caused-by-ad-blocker
Fix Empty adSlot Object Caused by Ad Blocker
2 parents e2d0e33 + b640665 commit 7f9a989

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/Bling.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ class Bling extends Component {
668668

669669
clear() {
670670
const adSlot = this._adSlot;
671-
if (adSlot) {
671+
if (adSlot && adSlot.hasOwnProperty("getServices")) {
672672
// googletag.ContentService doesn't clear content
673673
const services = adSlot.getServices();
674674
if (this._divId && services.some(s => !!s.setContent)) {

src/createManager.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,12 @@ export class AdManager extends EventEmitter {
321321
if (!instance.notInViewport()) {
322322
instance.defineSlot();
323323
const adSlot = instance.adSlot;
324-
const services = adSlot.getServices();
325-
if (!hasPubAdsService) {
326-
hasPubAdsService = services.filter(service => !!service.enableAsyncRendering).length > 0;
324+
325+
if (adSlot && adSlot.hasOwnProperty("getServices")) {
326+
const services = adSlot.getServices();
327+
if (!hasPubAdsService) {
328+
hasPubAdsService = services.filter(service => !!service.enableAsyncRendering).length > 0;
329+
}
327330
}
328331
}
329332
});

test/Bling.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,23 @@ describe("Bling", () => {
216216
clear.restore();
217217
});
218218

219+
it("handles empty adSlot on clear", () => {
220+
const instance = new Bling();
221+
instance._adSlot = {};
222+
223+
expect(() => {
224+
instance.clear();
225+
}).to.not.throw("adSlot.getServices is not a function");
226+
});
227+
228+
it("calls getServices on adSlot on clear", () => {
229+
const instance = new Bling();
230+
const adSlot = sinon.mock({getServices: () => {}});
231+
adSlot.expects("getServices").once();
232+
instance._adSlot = adSlot;
233+
instance.clear();
234+
});
235+
219236
it("updates correlator", () => {
220237
const updateCorrelator = sinon.stub(Bling._adManager, "updateCorrelator");
221238

0 commit comments

Comments
 (0)