Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>Here is what I used:</p> <ul> <li>In the <code>&lt;head&gt;</code>: I put the google scripts and defined all the adslots for the <strong>whole website</strong> (you will get it with the "generate tags").</li> <li>On each page: you put the <code>&lt;body&gt;</code> part of the script like you would do anywhere else.</li> </ul> <p>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.</p> <p>To make for this, you can use <code>googletag.pubads().refresh()</code>. 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 <code>refresh</code> 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.</p> <p>But you can pass the slots that are in the current page to the <code>refresh()</code> function. Here is how I did it:</p> <pre><code>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&lt;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 &gt; 0) googletag.pubads().refresh(slotsToRefresh); } $(document).on("pagechange", function() {refreshAds();}) </code></pre> <p>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 <code>&lt;head&gt;</code>).</p> <p>I hope it will help! There might be a way to get it to work seamlessly in Adsense but I did not try.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload