How to Use BuySellAds with Google Adsense Backfill Correctly

BuySellAds (BSA) is a platform that connects web publishers and web advertisers. The BSA platform is great if you want to monetize your website. It allows you to pick ads that correspond to your target audience. So far, so good. However, BSA does not guarantee that there will always be an ad to display. It means that when your ad zone is not sold out there will be no ad displayed to your visitor. BSA offers ads for unsold ad zones, but this is only available to websites with larger traffic (to apply to BSA Unreserved program you need at least 400,000 impressions per month). To avoid losing money, you can set up so called backfill, which is an HTML code that will be displayed in the ad zone whenever there is no ad provided by BSA. You can use JavaScript in the backfill code.

Google AdSense is the most popular service for serving ads and you may want to know if it is possible to increase your revenues by combining BSA ads and Google AdSense ads as backfills. If it is so, then continue reading.

If you just put your Google AdSense JavaScript code in a BSA backfill, you might receive an ad request error from Google AdSense. Google AdSense may detect that your ad code is used on a wrong URL. BSA uses an iframe pointing to a backfill page stored in their Content Delivery Network (CDN), and hence the Google AdSense robot will see URL of BSA’s CDN server instead of your webpage’s URL. BSA is aware of this and warns about the issue:

Important
[…]
If you are using Google Adsense as a backfill, make sure you add the following code below the google_ad_height portion in your Adsense code to guarantee all ad requests are filled (replace domain.com with your site’s URL):

google_page_url = “http://domain.com/”

The result would look like this:

<script type="text/javascript">
google_ad_client = "ca-pub-XXXXXXXXXXXX";
google_ad_slot = "XXXXXXXXXXXX";
google_ad_width = 728;
google_ad_height = 90;
google_page_url = "http://domain.com/";
</script>
<script type="text/javascript"
 src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

This recommendation is usable only if you use synchronous Google AdSense ad code, which is also what BSA recommends. If you use asynchronous Google AdSense ad code, you can use data-page-url attribute to do the same thing:

<script async src="http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
</script>
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-xxxxxxxxxxxxxxxxxx"
     data-ad-slot="xxxxxxxxxx"
     data-override-format="true"
     data-page-url="http://domain.com/"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

However, there is no explanation specifying what is the URL for google_page_url in the BSA support section. Your project’s homepage URL is not the correct URL because, in most cases, that’s not the URL where a Google AdSense ad is displayed. The Google AdSense bot crawls the target page and delivers ads related to the content on that page. If you specify different URL, such as your homepage URL, the ad might not be relevant to the content on the real target page. For example, you may have many articles on your blog and Google AdSense needs to know the URL of currently opened blog post to display ads that are relevant to your post. Moreover, if you specify other than the real target page URL (document.location of the main content window), you violate Google AdSense policy and your account can be suspended. So, not only that BSA’s recommendation may cause Google AdSense to display irrelevant ads, it may also make you violate Google AdSense policy.

The following modification that relies on document.referrer trick seems to be the best option to provide Google AdSense with a correct URL each time. Note that although altering the Google AdSense code is not allowed, there are certain scenarios in which modifying is allowed. Since using google_page_url and data-page-url are documented by Google for this purpose, we believe the following modification is not against the policy.

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
</script>
<!-- FIRST CHANGE: NOTICE THE NEWLY ADDED ID ATTRIBUTTE IN THE <INS> TAG. -->
<ins id="adsense-ad" class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-xxxxxxxxxxxxxxxxxx"
     data-ad-slot="xxxxxxxxxx"
     data-override-format="true"></ins>
<script>
    // SECOND CHANGE: 
    // START OF THE NEW CODE
    var url = document.referrer;
 
    if (!(typeof url === 'string' || url instanceof String)) {
            url = '';
    }
    // Change this to your domain!
    if (url.toLowerCase().indexOf('domain.com') == -1) { 
            // Change this to your homepage URL!
            url = 'http://domain.com/'; 
    }
    document.getElementById('adsense-ad').setAttribute("data-page-url", url);
    // END OF THE NEW CODE

    (adsbygoogle = window.adsbygoogle || []).push({});
</script>