我使用谷歌Adsense和無限的負載。無論何時我通過AJAX在頁面上「加載更多」數據,它都會自動從Google取出另一個廣告,這顯然不會顯示,因爲Google不支持通過AJAX引入廣告。如何在使用AJAX數據時忽略Adsense?
我需要的邏輯幫助下,我怎麼能只顯示在頁面的加載,每當1個廣告負載更多按鈕被按下時,如何忽略@include ('components.responsive_ad_display_only')
目前我想過使用會話。我在包含廣告後立即使用session()->put('retrieve', '1');
。然後,一旦「加載更多」被按下,我檢查會話是否設置。如果已設置,則在提取更多數據時我不包含廣告。這種情況下的問題是,我最終需要「忘記」會話,因此當訪問者刷新主頁時,會出現一個新廣告。
這是我使用的代碼:
@php
if (! session()->has('retrieve')) {
$i = 1;
$rand = rand(1, 10);
session()->put('retrieve', '1');
} else {
$i = 1;
$rand = 200; // any integer higher than media items displayed
session()->forget('retrieve');
}
@endphp
@foreach ($media as $media_item)
@if ($i == $rand)
<div class="ad_item">
<!-- Ad Displayed here -->
@include('components.responsive_ad_display_only')
</div>
@endif
<div class="media_item">
<h1>Media Item Name</h1>
</div>
@endforeach
這段代碼適用於不顯示每個其他時間的廣告,因爲會話狀態越來越設置和取消各一次。
我將不勝感激任何幫助和指導解決我的困境。
非常感謝!
我建議增加一個'?廣告= FALSE'查詢字符串的所有AJAX請求的URL從正規區分它們加載請求,然後使用服務器端邏輯切換包含基於該參數的廣告。 – gyre