45

Is it possible to run google adsense ads on a local server as a try out for a example application? I want to implement Google adsense ads in my web application which is running on a localost befor implementing in a live website. If possible then please suggest me the way.

Thank you.

thomson
  • 465
  • 1
  • 4
  • 12

6 Answers6

33

Yes it is possible (as of 2015). There is a special parameter, to use Adsense on Localhost without risks.

Today the AdSense code is different. If you want to add the adtest-parameter use data-adtest="on" within an ins block. Here is an example code:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- XYZ 336 x 280 -->
<ins class="adsbygoogle"
 style="display:inline-block;width:336px;height:280px"
 data-ad-client="ca-pub-XXXXXXXXXXXXX"
 data-adtest="on"
 data-ad-slot="XXXXXXXXXXX"></ins>
<script>
  (adsbygoogle = window.adsbygoogle || []).push({});
</script>

Found this on Google AdManager Help:

It shows a basic tutorial on how to achieve this.

Test Ad Exchange tags without charging advertisers or recording clicks and impressions

Add the google_adtest = on; parameter to your ad tag to specify that this is a test implementation. Make sure you set this parameter to "on" to ensure that clicks and impressions aren't recorded and advertisers aren't charged. The default value is "off".

basZero
  • 4,129
  • 9
  • 51
  • 89
Alexander Trust
  • 834
  • 7
  • 14
  • 35
    Is this advice still valid today? Tested the code (placed correct pub id and slot id) but it doesn't work - I only see a white space where ads should be displayed. – Mindaugas Li Feb 05 '16 at 16:01
  • 3
    I also just see white space. However you can select a different color to display in the AdSense dashboard in the ad settings under `Backup ads`. This at least shows exactly where the ad will be displayed. – Lorenzo Aug 12 '16 at 05:34
  • using the above code my ads is not displayed in localhost it's displayed blank space. How can i fix this issue? – kuldip Makadiya Jun 06 '17 at 06:28
  • i tried using above code mentioned by you it came first time in localhost but next time i can't see add, it shows blank space – Hemant Nagarkoti Sep 23 '17 at 11:58
  • 1
    @IvanGabriele Next time just update the answer instead of being passive aggressive :) – tyteen4a03 Feb 02 '18 at 02:03
  • 2
    @tyteen4a03 Indeed you're correct, I should have been more participative ! – Ivan Gabriele Feb 02 '18 at 21:40
  • 10
    Didn't work for me too. It just leaves the empty space. Tried both `data-adtest` and `data-ad-test`. – verlok Nov 18 '18 at 07:20
  • 4
    This should not be marked as solution anymore as it simply does not work. The Google Chrome inspector says `failed to load resource, 403`, so a super admin should mark this question as open again with the note: Solution required for 2019! – basZero Dec 12 '19 at 16:26
  • 1
    If this answer didn't work for you: check out my answer below (it is working for me): https://stackoverflow.com/a/64694852/10128619 – cbdeveloper Nov 05 '20 at 09:58
16

After lots of trials and errors, here is what I've found out. At least, those were my results.

It seems that adsbygoogle.js will not render ads on localhost. You'll probably will get something like the following:

enter image description here

So, the first thing you need to do is to set up a local custom domain for your localhost.

Here is what I do on Windows:

  • Run NotePad as Admnistrator.
  • Open C:\Windows\System32\drivers\etc\hosts
  • Add your custom domain to the list

enter image description here

enter image description here

Then you should be able to access your localhost by hitting dev.mydomain.com

After you've set up your local domain, you should go to your AdSense account and add it as a subdomain under the main domain of your registered website.

enter image description here

From the tests I've run. It seems that AdSense will not render real nor test ads if the domain/subdomain has not been added to your AdSense account.

In this example, I've added the main domain without the www and I've added two subdomains. One is the www. variation and the other one is the dev. that I've just added to my localhost.

Then you can render it on your code, just like you would on a real add and use the data-adtest flag set to on to run your tests.

<ins className="adsbygoogle"
  style={{display:"inline-block", width:"100%", height: "100%"}}
  data-ad-client={AD_SENSE_ACCOUNT}
  data-ad-slot={AD_SENSE_SLOT}
  data-adtest={ON_DEV ? "on" : "off"}
>
</ins>

I have an ON_DEV flag that automatically sets it to on, whenever I'm on my dev. domain.

EXTRA

This is out of the scope of this question, but is necessary for everything to work.

You also need to push it to the adsbygoogle array. This is how I do it in React.

useEffect(()=>{
  (window.adsbygoogle = window.adsbygoogle || []).push({});
},[]);

And of course, you need the adsense <script/> tag on your index.html as well.

<script data-ad-client="XXXXXXXX" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
  • 1
    Do I understand this correctly: It is no problem, that test.mydomain.com cannot be resolved by public DNS? Will this also work with fifferent domain names? So if I have called my host mydomain.test I could add this to my Adsense account set the test flag and it should work? – Alexander Dobernig Feb 02 '21 at 06:55
  • 1
    If I'm not mistaken, you need a valid registered/verified domain on your AdSense account. And then you can add subdomain variations of that domain. But I don't think you can add different domain names. Each one would need to be verified. Like `mydomain.com` is a different domain than `mydomain.test`. – cbdeveloper Feb 02 '21 at 08:25
  • 1
    Thank you, this is my answer to what I looking for. I work fine – Hoang Nam Aug 13 '21 at 23:58
  • As of Feb 2023, you can no longer add subdomains in AdSense, so that no longer works. – Certitude Aug 16 '23 at 21:48
9

I wasn't able to get the ads to show up using data-adtest="on" in localhost either. Since there was still an empty div, as a quick work around I added a border to the styles so it looks like display: inline-block; border: 1px solid green;. I was only concerned about placement and how it would interact with my layout and not so much what the ad looked like, so this allowed me to see the dimensions of the ad and make any layout adjustments accordingly.

Trevor Glass
  • 390
  • 5
  • 10
  • yeah same, and just to be safe I added `min-height: 10vh; min-width: 50vw; ` so that chrome won't catch an empty ad which crashes the page. – beeftosino Jul 10 '20 at 22:03
8

I was able to make Adsense ads work on localhost by following the next steps:

1) Add ads.txt file in root of myDomain

https://support.google.com/adsense/answer/7532444?hl=en-GB

The file should be available for GET at myDomain.com/ads.txt

(I am not sure if the ads would work without this step)

2) Edit the hosts system file:

in Linux and macOS you can find it at /etc/hosts

There add a line like this 127.0.0.1 mylocalstuff.myDomain.com

run sudo killall -HUP mDNSResponder

A browser restart might also be needed.

Without this step you will probably get a 403

3) Use the data-adtest="on" in the ins tag so the impressions are not affected.

Access mylocalstuff.myDomain.com in the browser (or mylocalstuff.myDomain.com<:PORT_NUMBER> if your local server is available at a port number different than 80).

The steps where inspired from sources:

https://medium.com/@jeffersonlicet/testing-ads-on-localhost-300215b415d6

https://stackoverflow.com/a/34389120/1979861

Note: pay also attention to the space for ads (styles). If that is too restricted by some width or height you can get an error and you will see a blanc space.

Florin Dobre
  • 9,872
  • 3
  • 59
  • 93
4

You can put this as the first line of the code:

google_adtest = "on";

test-adsense-ads-on-your-site

Note that you must put the semicolon (;) at the end of this line.

Once you add this line to the code, for the adsense unit, it will be displayed as a test unit and its impressions won’t be counted.

Very Important Note - Once you are satisfied with the adsense unit, don't forget to remove this line from the code.

Check out this link: http://www.hacktrix.com/2-ways-to-safely-test-google-adsense-ads-on-your-site

jkdev
  • 11,360
  • 15
  • 54
  • 77
pro
  • 87
  • 4
1

I was able to get ads working in localhost by adding an entry into my hosts file.

sudo vi /etc/hosts

Then add the following at the end of the file:

127.0.0.1 local.your-page.com

You can now reach your localhost by going to http://local.your-page.com:3000 Note that you still need to add the port (in this example :3000)

Also, when working with ads it's always good to do your tests in incognito and without any browser extension (they may be blocking the ads)

dnaranjo
  • 3,647
  • 3
  • 27
  • 41