Skip to content

Commit a3863f1

Browse files
committed
feat: Toggle class on ads slots when they become empty
1 parent 6636702 commit a3863f1

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/gpt-ads/lib/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const DEFAULT_OPTIONS = {
66
individualRefresh: false,
77
responsive: false,
88
collapseEmptyDivs: false,
9+
emptyClass: 'is-empty',
910
};
1011

1112
export const GPT_LIB_SCRIPT_ID = 'google-publisher-tag-lib-script';

packages/gpt-ads/lib/templates/component.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default {
66
currentSizeMappingIndex: null,
77
windowResizeListenerDebounce: null,
88
ghostMode: <%= options.ghostMode %>,
9+
isEmpty: true,
910
}),
1011
props: {
1112
adUnit: {
@@ -121,6 +122,12 @@ export default {
121122
refreshSlot() {
122123
googletag.pubads().refresh([this.adSlot]);
123124
},
125+
handleSlotRenderEnded (event) {
126+
if (event.slot.getSlotId().getDomId() !== this.divId) {
127+
return;
128+
}
129+
this.isEmpty = !!event.isEmpty;
130+
},
124131
/**
125132
* Window resize event listener
126133
* Attached only when responsive mode is enabled, it checks wether a different size
@@ -174,11 +181,15 @@ export default {
174181
collapseEmptyDiv,
175182
} = this;
176183

184+
177185
// Init Ad slot
178186
googletag.cmd.push(() => {
187+
const pubadsService = googletag.pubads()
188+
pubadsService.addEventListener('slotRenderEnded', this.handleSlotRenderEnded);
189+
179190
const adSlot = googletag
180191
.defineSlot(adUnitPath, this.formattedSize, divId)
181-
.addService(googletag.pubads());
192+
.addService(pubadsService);
182193

183194
// Collapse empty div slot-level override
184195
if (collapseEmptyDiv !== null) {
@@ -227,12 +238,14 @@ export default {
227238
window.removeEventListener('resize', this.handleWindowResize);
228239
},
229240
render(h) {
230-
const { divId, style } = this;
241+
const { divId, style, isEmpty } = this;
242+
let classAttr = isEmpty ? '<%= options.emptyClass %>' : '';
231243

232244
return h('div', {
233245
style,
234246
attrs: {
235247
id: divId,
248+
class: classAttr,
236249
},
237250
domProps: { innerHTML: '' },
238251
});

0 commit comments

Comments
 (0)