通过jQuery mobile的页面转换来加载adsense广告 [英] Load adsense ads through page transition with jQuery mobile

查看:158
本文介绍了通过jQuery mobile的页面转换来加载adsense广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读网络,并尝试了好几天一种在不破坏ToS的情况下通过jQuery移动转换显示Google Adsense广告的方法.我有些困惑,所以我转向最明智的社区.

Adsense广告代码由三个脚本组成:(i)一个通用脚本,(ii)一系列广告位,以及(iii)展示广告本身.前两个进入<head>,后一个进入<body>.

我可以在我的第一页上展示广告.该问题与页面转换有关.

由于jQuery Mobile不会重新加载<head>,因此可以选择在第一次加载<head>时准备googletag.这将限制设置为每页最多三个广告,这在整个网站上并不多.另外,这意味着您将不得不移动广告<div>,这也不是那么好.最后,这意味着您可以加载广告,直到用户转到广告所属的页面(如果有的话)后才显示.这也不是ToS兼容的.

是否有一种方法可以在每次转换时加载新的新广告?如果是,我应该将Google脚本放在哪里以确保它们正确加载?

解决方案

我找到了一种方法,可以通过将Adsense插入DFP广告管理系统来使其在Google DFP广告管理系统中运行. DFP更具灵活性,因此更容易.

这是我用的:

  • <head>中:我放置了google脚本并定义了整个网站的所有广告位(您将使用生成标记"获得它).
  • 在每个页面上:就像在其他任何地方一样,放置了脚本的<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中无缝运行,但我没有尝试过.

I have been reading the web and trying out things for days looking for a way to show the Google Adsense ads through jQuery mobile transitions without breaking the ToS. I am a bit stuck so I turn to the wisest community.

The Adsense tag is made of three scripts (i) a general one, (ii) a list of slots and (iii) the display itself. The first two go in the <head>, the latter in the <body>.

I can display the ads on my first page just fine. The problem comes with page transition.

Since jQuery Mobile does not reload the <head>, an option is to prepare the googletag while loading the <head> the first time. This sets the limit to a maximum of three ads per page, which, across a whole site is not a lot. Plus it means that you will have to move ad <div> around, which is not so great either. Finally, it means that you may load ads and not display them until the user goes to the page where it belongs, if ever. Which is not so ToS compatible either.

Is there a way to load a fresh new ad on each transition? If yes, where do I put the Google scripts to make sure they load properly?

解决方案

I found a way to get it to work in Google DFP with Adsense plugged into DFP. DFP is more flexible so it was easier.

Here is what I used:

  • In the <head>: I put the google scripts and defined all the adslots for the whole website (you will get it with the "generate tags").
  • On each page: you put the <body> part of the script like you would do anywhere else.

With this, you will be able to get a new ad served each time you load a new page. However, if you browse between pages, you will never get them to refresh.

To make for this, you can use googletag.pubads().refresh(). However, you want to only refresh the slots that are in the page that you are loading, otherwise you break some terms and conditions. Plus, you cannot refresh slots that have not been displayed yet, so it will fail if you defined slots for the whole website but all the pages have not been loaded yet, which is quite likely.

But you can pass the slots that are in the current page to the refresh() function. Here is how I did it:

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();})

There you go, each time you go back to a page, the slots are refreshed, each time you go to a new page, a new slot is created (provided that it was defined in the <head>).

I hope it will help! There might be a way to get it to work seamlessly in Adsense but I did not try.

这篇关于通过jQuery mobile的页面转换来加载adsense广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆