10

Per research there was a previous question from 2014 (Is Google adsense available for localhost?) that suggested adding:

data-adtest="on"

and after testing and reading the comments the solution no longer works. In an ad unit:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <!-- responsive_ad -->
    <ins class="adsbygoogle"
        style="display:block"
        data-ad-client="ca-pub-####"
        data-ad-slot="######"
        data-ad-format="auto"
        data-adtest="on"
        data-full-width-responsive="true">
    </ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

After testing in Chrome, Firefox and Safari a white box is rendered. After searching I'm not able to locate any solution in product forums or on any site. How can I test an ad unit in localhost?

tschomacker
  • 631
  • 10
  • 18
DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127

3 Answers3

12

I also tried the data-adtest="on" approach and it didnt work for me either. I found two different approaches for testing:

If you have opened the developer console you would have saw that you got an 403 Error. The reasons for this error are further explained here: https://www.shoutmeloud.com/adsense-403-forbidden-error.html. To make it short: Adsense Crawlers reject Localhost. Thats why you got a white space instead of ads.

But if there is a third way I would also be glad to hear it.

tschomacker
  • 631
  • 10
  • 18
3

I tried many options but they did not work.

I see white screen and 403 error.

I find that workaround "border:1px solid red;" and it works

<ins style=".....   border:1px solid red;"></ins>

I can see the ads area and I will try production without a border

I can see ads area

Ergin Çelik
  • 719
  • 7
  • 14
1

As the other answer suggests, using a hostname other than localhost is important. Using 0.0.0.0 can be a simple option.

Since early 2021 Google appears to require explicit TCF v2.0 consent to load any ads. The following snippet can be included in application code or something like Violentmonkey:

window.__tcfapi = (command, parameter, callback) => {
    if (command === 'checkConsent') {
        callback(true);
    }
    if (command === 'addEventListener') {
        callback({eventStatus: 'tcloaded', gdprApplies: false}, true);
    }
};
Dave
  • 479
  • 3
  • 13
  • I tried both switching to `0.0.0.0` ad adding that `__tcfapi` variable, but in 2022 neither seems to make ads appear locally :( – machineghost May 16 '22 at 16:54
  • @machineghost can you provide an example that is not working? Defining the `__tcfapi` variable still works for me. – Dave May 16 '22 at 20:04
  • It turns out there are so many different causes of this problem that Google made an entire series of pages to troubleshoot it: https://support.google.com/adsense/workflow/10383839?hl=en (I think mine is that they only approved my account recently, but I won't know until I wait and see, because everything in the site indicates I should be seeing ads already). – machineghost May 18 '22 at 01:20