我在页面标题中定义了所有 adSlot,并且在 JavaScript 中,我想根据这些 adSlot 在页面上的位置刷新其中的“部分”。
这是我目前所拥有的:
googletag.cmd.push(function() {
slot1 = googletag.defineSlot('/1234/example', [[728, 90], 'gpt-divId1')
.addService(googletag.pubads());
slot2 = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId2')
.addService(googletag.pubads());
slot3 = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId3')
.addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();
然后我将该定义的插槽的名称放入 data-gpt-slot
中每个 div 的属性,如下所示:<div attr="slot1" id="gpt-divId1">
.
在一个单独的 JavaScript 中,我尝试运行我的 DOM,获取所有 data-gpt-slot
的列表。特定区域内的值并将它们添加到 googletag.pubads().refresh();
如果我手动调用googletag.pubads().refresh([slot1,slot2])
,这些广告将会刷新,但如果我尝试通过 jQuery 构建该字符串,它就不会播放。
var gptdivs = [];
$('.myContainer .gpt-holder').each(function(){
gptdivs.push($(this).attr('data-gpt-slot'));
});
googletag.pubads().refresh([gptdivs]);
我怀疑我遗漏了 $(this).attr('data-gpt-slot')
属性的一些信息和slot1
, slot2
等等?
最佳答案
问题是您尝试错误地使用刷新方法。它将实际的广告单元实例作为参数,而不仅仅是字符串形式的名称。
试试这个:
var adunits = {};
adunits['slot1'] = googletag.defineSlot('/1234/example', [[728, 90], 'gpt-divId1').addService(googletag.pubads());
adunits['slot2'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId2').addService(googletag.pubads());
adunits['slot3'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId3').addService(googletag.pubads());
然后刷新广告单元的代码变为:
var gptdivs = [];
$('.myContainer .gpt-holder').each(function(){
gptdivs.push(adunits[$(this).attr('data-gpt-slot')]);
});
googletag.pubads().refresh(gptdivs);
关于javascript - 如何定位我想要使用 GPT 刷新的广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329560/