我正在尝试在加载网站后加载googleads。但我没有收到广告。
我在这里使用googletag进行异步渲染。
我该怎么办?是否可以在加载页面后加载广告。
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
$(window).load(function(){
(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];
console.log(node);
node.parentNode.insertBefore(gads, node);
})();
});
function getSamsVisitorCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
</script>
<script type='text/javascript'>
$(window).load(function(){
var oGoogleDFP = {
'path' : "<%=navPath_header%>",
'page' : null,
'search' : null
};
if(location.pathname.indexOf('homepage.jsp')>-1 || location.href == 'http://www.samsclub.com/sams/'){
oGoogleDFP.path = '/Homepage';
oGoogleDFP.page = 'main';
}else if(location.pathname.indexOf('searchResults.jsp')>-1){
oGoogleDFP.page = 'searchresults';
}else if(location.pathname.indexOf('.ip')>-1 || location.pathname.indexOf('.cp')>-1){
oGoogleDFP.path = oGoogleDFP.path.split('/').slice(1); //remove All_Products
if(location.pathname.indexOf('.ip')>-1) oGoogleDFP.path.pop(); //remove product
oGoogleDFP.path = (oGoogleDFP.path.length > 4)? '/'+oGoogleDFP.path.slice(0,4).join('/') : '/'+oGoogleDFP.path.join('/');
oGoogleDFP.page = (location.pathname.indexOf('.ip')>-1)? 'product' : 'main';
}else if(location.pathname.indexOf('content.jsp')){
if(location.search.indexOf('pageName=')>-1) oGoogleDFP.path = '/Seasonal/'+location.search.split('pageName=')[1].split('&')[0];
oGoogleDFP.page = 'seasonal';
}
if(location.search.indexOf('searchTerm=')>-1) oGoogleDFP.search = location.search.split('searchTerm=')[1].split('&')[0];
googletag.cmd.push(function() {
googletag.defineSlot('/41919049/SamsClub-US'+oGoogleDFP.path, [[1000, 25], [1000, 300]], 'expandableAd').addService(googletag.pubads()).setTargeting('page',oGoogleDFP.page).setTargeting('search',oGoogleDFP.search);
googletag.defineSlot('/41919049/SamsClub-US'+oGoogleDFP.path, [[728, 90]], 'leaderboardTopAd').addService(googletag.pubads()).setTargeting('page',oGoogleDFP.page).setTargeting('pos','Top').setTargeting('search',oGoogleDFP.search);
googletag.defineSlot('/41919049/SamsClub-US'+oGoogleDFP.path, [[728, 90]], 'leaderboardBottomAd').addService(googletag.pubads()).setTargeting('page',oGoogleDFP.page).setTargeting('pos','Bottom').setTargeting('search',oGoogleDFP.search);
googletag.defineSlot('/41919049/SamsClub-US'+oGoogleDFP.path, [[160, 600]], 'skyscraperAd').addService(googletag.pubads()).setTargeting('page',oGoogleDFP.page).setTargeting('search',oGoogleDFP.search);
googletag.defineSlot('/41919049/SamsClub-US'+oGoogleDFP.path, [[300, 250]], 'rectangleAd').addService(googletag.pubads()).setTargeting('page',oGoogleDFP.page).setTargeting('search',oGoogleDFP.search);
googletag.pubads().enableAsyncRendering();
googletag.pubads().setTargeting("samsVisitor", getSamsVisitorCookie ("samsVisitor"));
googletag.enableServices();
});
});
答案 0 :(得分:0)
将他们的js代码附加到头部,我希望它能工作:)
答案 1 :(得分:0)
除非所有这些都属实,否则广告无法加载:
在调用googletag.enableServices()之后,在googletag.cmd.push中添加以下内容。
googletag.cmd.push(function() {
googletag.display("expandableAd");
googletag.display("leaderboardTopAd");
googletag.display("leaderboardBottomAd");
googletag.display("skyscraperAd");
googletag.display("rectangleAd");
});