2016-12-08 40 views
0

有人可以帮我处理Google GPT googletag.cmd队列重新排序问题吗?Google GPT - googletag.cmd队列重新排序

(1)在家里HTML网页,我有这样的:

googletag.cmd.push(function() { defineSlot1(); }); 
googletag.cmd.push(function() { defineSlot2(); }); 
googletag.cmd.push(function() { defineSlot3(); }); 
googletag.cmd.push(function() { defineSlot4(); }); 

(2)连接JS,我动态添加:

googletag.cmd.push(function() { defineSlot5(); }); 

(3)所以现在在队列googletag.cmd的,像这样的顺序:

[slot1, slot2, slot3, slot4, slot5]; 

我的问题是,如何移动slot5到所述第二位置之后slot1中,像这样:

[slot1, slot5, slot2, slot3, slot4]; 

https://developers.google.com/doubleclick-gpt/reference, googletag.commandArray仅具有推(F)的方法,并且是googletag.cmd的不是标准阵列。

如何做到这一点?谢谢。

回答

0

Google GPT库以异步模式加载,它最初定义为googletag.cmd作为常规数组。

<script type='text/javascript'> 
var googletag = googletag || {}; 
googletag.cmd = googletag.cmd || []; 
(function() { 
var gads = document.createElement('script'); 
gads.async = true; 
gads.type = 'text/javascript'; 
var useSSL = 'https:' == document.location.protocol; 
gads.src = (useSSL ? 'https:' : 'http:') + 
'//www.googletagservices.com/tag/js/gpt.js'; 
var node = document.getElementsByTagName('script')[0]; 
node.parentNode.insertBefore(gads, node); 
})(); 
</script> 

当主库被下载的(它发生在页面呈现平行),它最初定义的数组转换成自定义对象(https://developers.google.com/doubleclick-gpt/reference#googletag.CommandArray),所以你确实不能对其进行修改。

回到你的问题:

  1. 如果使用 googletag.pubads().enableSingleRequest()https://developers.google.com/doubleclick-gpt/reference#googletag.PubAdsService_enableSingleRequest)所有广告位的将被查询并在第一的googletag.display(...)调用显示。

  2. 如果不使用enableSingleRequest单个查询将在每次无论其定义顺序

你叫googletag.display(…)特定广告位时,发送考虑第1页第2页和我不不明白为什么你会关心插槽定义的顺序。 无论如何,如果你真的需要它,你可以定义自己的队列变量

window.custom_cmd = []; 

和按如下方式使用它,而不是googletag.cmd整个页面,这样它看起来:你的后

custom_cmd.push(function() { 
// regular defineSlot(…) calls 
}); 

,然后某处附加的js文件加载只是

// reorder it or whatever else 
… 
// copy your queue into the regular queue 
for (var i = 0; i < custom_cmd.length; i++) { 
    googletag.cmd.push(custom_cmd[i]); 
} 

该代码会触发广告,呼吁