简体   繁体   English

您如何在 localhost 中呈现 Google 广告单元?

[英]How do you render Google ad units in localhost?

Per research there was a previous question from 2014 ( Is Google adsense available for localhost? ) that suggested adding:根据研究,2014 年有一个先前的问题( Google adsense 是否可用于 localhost? ),建议添加:

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.在 Chrome 中测试后,Firefox 和 Safari 呈现一个白框。 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?如何在 localhost 中测试广告单元?

I also tried the data-adtest="on" approach and it didnt work for me either.我还尝试了data-adtest="on"方法,但它对我也不起作用。 I found two different approaches for testing:我发现了两种不同的测试方法:

If you have opened the developer console you would have saw that you got an 403 Error.如果您打开了开发者控制台,您会看到出现 403 错误。 The reasons for this error are further explained here: https://www.shoutmeloud.com/adsense-403-forbidden-error.html .此处进一步解释了此错误的原因: https : //www.shoutmeloud.com/adsense-403-forbidden-error.html To make it short: Adsense Crawlers reject Localhost .简而言之: Adsense 爬虫拒绝 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.但如果有第三种方式,我也会很高兴听到。

As the other answer suggests, using a hostname other than localhost is important.正如另一个答案所暗示的那样,使用 localhost 以外的主机名很重要。 Using 0.0.0.0 can be a simple option.使用0.0.0.0可能是一个简单的选择。

Since early 2021 Google appears to require explicit TCF v2.0 consent to load any ads.自 2021 年初以来,谷歌似乎需要明确的 TCF v2.0 同意才能加载任何广告。 The following snippet can be included in application code or something like Violentmonkey :以下代码段可以包含在应用程序代码或类似Violentmonkey 的代码中:

window.__tcfapi = (command, parameter, callback) => {
    if (command === 'checkConsent') {
        callback(true);
    }
    if (command === 'addEventListener') {
        callback({eventStatus: 'tcloaded', gdprApplies: false}, true);
    }
};

I tried many options but they did not work.我尝试了很多选择,但没有奏效。

I see white screen and 403 error.我看到白屏和 403 错误。

I find that workaround "border:1px solid red;"我发现解决方法“边框:1px 纯红色;” and it works它有效

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

I can see the ads area and I will try production without a border我可以看到广告区域,我将尝试无边框制作

我可以看到广告区

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM