2014-01-30 45 views
3

使用Ajax調用將動態旋轉器類型的動態HTML樣式添加到主體中。 ADs DIV ID將以摺疊模式動態添加到Rotator的主體中。DFP廣告管理系統標記不使用動態DIV注入呈現

使用DFP iframe廣告,大小爲180x150

在初始頁面加載沒有在網頁源的DIV ID。

我有一個通過Ajax調用將DIV ID的動態添加到網頁主體的方案。

代碼流程:

了添加的網頁的head標籤內GPT庫入選。

<head> 
<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> 
</head> 

以及定義SLOT在頭標記,這將使呼叫轉移到DFP服務器cmd.push()方法。禁用初始加載,因爲定義插槽會在初始頁面加載期間檢查DIV ID,從而導致錯誤。

<head> 
<script type='text/javascript'> 
var slot1 = ''; 
googletag.cmd.push(function() { 
slot1 = googletag.defineSlot('/12638013/Connect_180x150', [180, 150], 'div-gpt-ad- 1389885200465-0').addService(googletag.pubads()); 
googletag.pubads().enableSingleRequest(); 
googletag.pubads().disableInitialLoad(); 
googletag.enableServices(); 
}); 
</script> 
</head> 

一旦頁面獲得在頭部Tag.Through AJAX調用上述腳本追加DIV ID到該網頁的身體負荷。

jQuery('body').append('<div id="div-gpt-ad-1389885200465-0" style="width:180px; height:150px;"><script type="text/javascript">googletag.cmd.push(function() { googletag.display("div-gpt-ad-1389885200465-0"); });</script></div>'); 

將DIV ID動態呈現到網頁中後。刷新HEAD標籤中定義的廣告位。

googletag.pubads().refresh([slot1]); 

最終結果是:

驗證以上頁面與谷歌調試控制檯。

頁面請求沒有任何錯誤,表明頁面標記正確。

852毫秒來渲染頁面 491毫秒來獲取廣告 0毫秒來呈現廣告

Status Time Description 
Information Start Created slot: /12638013/Connect_180x150_0 
Information 1 ms Google service JS loaded 
Information 1 ms Created service: publisher_ads 
Information 1 ms Associated service "publisher_ads" with slot "/12638013/Connect_180x150". 
Information 2 ms Using single request mode to fetch ads. 
Information 2 ms Fetching GPT PubAds implementation 
Information 2 ms Invoked queued function. Total: 1. Errors: 0. 
Information 57 ms GPT implementation fetched. 
Information 759 ms Calling fillslot. 
Information 773 ms Invoked queued function. Total: 2. Errors: 0. 
Information 852 ms Page load complete 
Information 7090 ms Refreshing ads. 
Information 7135 ms Fetching ad for slot: /12638013/Connect_180x150 
Information 7626 ms Receiving ad for slot: /12638013/Connect_180x150 
Information 7626 ms Rendering ad for slot: /12638013/Connect_180x150 
Information 7626 ms Completed rendering ad for slot: /12638013/Connect_180x150 

但廣告不會呈現到其中的DIV動態地被注入轉子內。

我錯過了什麼?建議對我有用。

回答

3

也有同樣的問題我自己的年齡....然後我發現這個:https://plugins.jquery.com/dfp/

基本上你可以放下所有的谷歌JS和使用所提供的2-3線的解決方案。 我遇到的唯一障礙是缺少「enableSingleRequest」選項......如果您在頁面上多次加載廣告,則需要將其設置爲false。

+0

你是否也發現你必須打開*異步渲染才能使refresh()正常工作?我無法讓它與同步加載一起工作,除非基於模板使用的插頁式廣告表示它不適用於異步渲染,否則這不會造成問題,這很奇怪。 – SeanKendle

相關問題