I'm trying to set html content of a new element but when I check to see if slot.getHtml() gives me the html element (set below), it shows an empty string. Any idea why this could be happening?

var slot = googletag.defineSlot('/1234567/sports', [160, 600], 'ozge')   .addService(googletag.companionAds())   .addService(googletag.pubads())   .addService(googletag.content()); googletag.content().setContent(slot, '<h2>Custom content in ad slot.</h2>') 

I use the code snippet in this link to view the newly defined slot: https://gist.github.com/rdillmanCN/a70ec955d9a982127fefadabe8b898b5

Tag:google-ads-api, javascript, google-publisher-tag

Only one comment.

  1. rabsom

    EDIT : As per detailed here, contentService will soon be deprecated (March 29, 2022).

    As detailed here, googletag.content() is used to set the content of a slot manually. It adds html content to the targeted slot div before the generated adcall iframe :

    var slot = googletag.defineSlot('adpath', [728, 90], 'div-1') .addService(googletag.content()) .addService(googletag.pubads()); var content = '<a href="www.mydestinationsite.com"><img src="www.mysite.com/img.png"></img></a>'; googletag.content().setContent(slot, content);

    Html result :

    slot.getHtml() will extract the html inside the generated iframe. If it returns an empty string, it means your slot adcall is empty. It does not extract the googletag.content(), but the google ad iframe innerHTML. That is why it returns an empty string.

    Warning : from what I experienced, googletag.content().setContent() will set the desired html only if the adcall returns nothing to display (empty adcall). As long as a banner is displayed, your googletag.content() will be cleaned :

    From my understanding, googletag.content().setContent() is used to set html to display before your adcall is done or if your adcall returns nothing to display.

    To conclude : slot.getHtml() is not able to get anything outside the generated ad iframe. It returns an empty string when the adcall returns an empty impression.

Add a new comment.