Is Google adsense available for localhost?

邮差的信 提交于 2019-11-29 02:31: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.


回答1:


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".




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/26864860/is-google-adsense-available-for-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!