我几天来一直在阅读网络并尝试一些东西,寻找一种通过jQuery移动过渡显示谷歌广告的方法,而不会破坏ToS。我有点困了,所以我求助于最明智的社区。
Adsense标签由三个脚本组成:(i)通用脚本,(ii)插槽列表,以及(iii)显示器本身。前两个在<head>
中,后者在<body>
中。
我可以在我的第一页上显示这些广告。页面转换出现问题。
由于jQuery Mobile不重新加载<head>
,因此可以选择在第一次加载<head>
时准备googletag
。这将限制为每页最多三个广告,在整个网站上这并不多。另外,这意味着你将不得不移动广告<div>
,这也不太好。最后,这意味着你可以加载广告,直到用户转到它所属的页面(如果有的话)才显示它们。这也不太兼容ToS。
有没有办法在每次转换时加载一个新的广告?如果是,我应该把谷歌脚本放在哪里,以确保它们正确加载?
我找到了一种方法,让它在谷歌DFP中工作,Adsense插入DFP。DFP更灵活,所以更容易。
这是我使用的:
- 在
<head>
中:我放置了谷歌脚本,并定义了整个网站的所有广告位(您将使用"生成标签"获得它) - 在每一页上:您可以像在其他地方一样放置脚本的
<body>
部分
这样,每次加载新页面时,您都可以获得新的广告。但是,如果您在页面之间浏览,您将永远无法刷新它们。
为此,您可以使用googletag.pubads().refresh()
。但是,您希望只刷新正在加载的页面中的插槽,否则会破坏某些条款和条件。此外,您不能使用尚未显示的refresh
插槽,因此,如果您为整个网站定义了插槽,但尚未加载所有页面,则很可能会失败。
但是您可以将当前页面中的插槽传递给refresh()
函数。以下是我的做法:
function refreshAds() {
// Get all the slots
var allSlots = googletag.pubads().getSlots();
var slotsToRefresh = Array();
// Select the slots that are on the current page based on their dom Id
for (var i=0; i<allslots.length; ++i)
if (isSlotIdOnTheCurrentPage(allSlots[i].getSlotId().getDomId()))
slotsToRefresh.push(allSlots[i]); // I let you implement the logic behind naming ids slots and divs
if (slotsToRefresh.length > 0)
googletag.pubads().refresh(slotsToRefresh);
}
$(document).on("pagechange", function() {refreshAds();})
这样,每次返回页面时,插槽都会刷新,每次返回新页面时,都会创建一个新插槽(前提是它已在<head>
中定义)。
我希望它会有所帮助!也许有一种方法可以让它在Adsense中无缝工作,但我没有尝试。
在这种情况下,使用jQuery的getScript()方法可能会有所帮助。我建议您尝试将其包含在pageinit函数中。让我给你举个简单的例子。
$(document).delegate('[data-role=page]','pageinit',function(){ // this would get executed on page init of every JQM page
$.getScript('path/to/yourlib.js',function(){ // using getScript should help you be able to load scripts since the head doesn't get loaded again
Demo(); //This would be code that your lib uses
});
});