# 如何在\</head\>中添加 Google Auto Ads 代码

**URL:** https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672
**Category:** Support
**Created:** [2019年二月20日 16:58 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672 "2019-02-20T16:58:28Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Vikas\_Dangi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vikas_dangi/32/131628_2.png) [@Vikas\_Dangi](https://meta.discourse.org/u/Vikas_Dangi)
#### Post date: [2019年二月20日 16:58 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/1 "2019-02-20T16:58:28Z")

</div>

I wanted to add adsense auto ads code to `</head>` of my every topic.

I have tried to add the following auto ads code in themes head section

```
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
</script> 
<script>
 (adsbygoogle = window.adsbygoogle || []).push({
      google_ad_client: "ca-pub-87837185XXXXXXXX",
      enable_page_level_ads: true
 });
</script>

```

But no success.

---

<div class="post-metadata">

### Author: ![SidV](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sidv/32/119460_2.png) [@SidV](https://meta.discourse.org/u/SidV)
#### Post date: [2019年二月20日 18:59 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/2 "2019-02-20T18:59:17Z")

</div>

Hi @Vikas_Dangi

1. Go to admin panel, Customize.
2. Edit the theme.
3. Then `</head>` section.
4. Add that code.
5. Save.
6. Enjoy 😉

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2019年二月20日 19:28 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/3 "2019-02-20T19:28:10Z")

</div>

Note that you still have to whitelist the external domain for CSP if this is a new install of Discourse, as CSP is mandatory on all new installs. CSP will be mandatory on all installs once 2.3 is released as well.

---

<div class="post-metadata">

### Author: ![Vikas\_Dangi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vikas_dangi/32/131628_2.png) [@Vikas\_Dangi](https://meta.discourse.org/u/Vikas_Dangi)
#### Post date: [2019年二月20日 21:57 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/4 "2019-02-20T21:57:54Z")

</div>

I have tried it but only the first script of code is showing in the source not the second.

---

<div class="post-metadata">

### Author: ![Vikas\_Dangi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vikas_dangi/32/131628_2.png) [@Vikas\_Dangi](https://meta.discourse.org/u/Vikas_Dangi)
#### Post date: [2019年二月21日 17:52 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/5 "2019-02-21T17:52:53Z")

</div>

> [@codinghorror](#):
>
> whitelist the external domain for CSP if this is a new install of Discourse

I don’t know how to do this. So please tell me how can I whitelist CSP in my install, to make work this code.my discourse version is 2.3

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2019年二月21日 18:23 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/6 "2019-02-21T18:23:02Z")

</div>

Try searching for it here.

---

<div class="post-metadata">

### Author: ![devzero](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@devzero](https://meta.discourse.org/u/devzero)
#### Post date: [2019年六月25日 17:33 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/7 "2019-06-25T17:33:41Z")

</div>

I mentioned this on another similar post also – wanted to xpost it here to make sure people finding this one in search are also aware.

I was having similar issues, and upon viewing the source of the page served, found that Discourse was stripping out the inline Javascript entirely.

This post mentions it:

> [@EPN Smart Links](https://meta.discourse.org/t/epn-smart-links/116293/4):
>
> That line should be extracted into a theme file, since inline Javascript is not allowed in recent versions of Discourse (for CSP). Did you check all theme JavaScript files too?

but I don’t see a solution there.

---

<div class="post-metadata">

### Author: ![devzero](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@devzero](https://meta.discourse.org/u/devzero)
#### Post date: [2019年六月25日 18:21 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/8 "2019-06-25T18:21:27Z")

</div>

I don’t either. I haven’t found a solution at the moment.

---

<div class="post-metadata">

### Author: ![Vikas\_Dangi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vikas_dangi/32/131628_2.png) [@Vikas\_Dangi](https://meta.discourse.org/u/Vikas_Dangi)
#### Post date: [2019年六月26日 18:00 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/9 "2019-06-26T18:00:55Z")

</div>

It’s a great solution, but not customizable. Every user on every page of the forum will face the code.

I want it to work like Discourse Advertisement Plugin, where you can choose user groups who can see ads and where. I prefers users below trust level 2 and only on topics where the content is enough to serve ads.

@codinghorror It will be great if this auto ads also added to Discourse Ads Plugin.

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [2019年六月26日 18:50 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/10 "2019-06-26T18:50:36Z")

</div>

I’m not convinced that auto ads can work with Discourse. I can’t find any documentation about this, so maybe it’s possible. If you can point us to documentation about how to implement it with single page apps/infinite scroll, then we can try it.

The ad plugin handles AdSense and Ad Manager by using their APIs to request ads be rendered and updated, but I don’t see how that’s done with Auto Ads.

---

<div class="post-metadata">

### Author: ![charleswalter](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/charleswalter/32/116433_2.png) [@charleswalter](https://meta.discourse.org/u/charleswalter)
#### Post date: [2019年十一月14日 04:22 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/11 "2019-11-14T04:22:37Z")

</div>

@neil，有没有办法在每次“页面”加载时强制刷新自动广告 JavaScript？

我猜它在处理滚动加载更多回复时的广告可能会有困难，但除此之外，这看起来确实是一个有趣的解决方案。我在想它能否与传统的 AdSense 广告位结合使用。

---

<div class="post-metadata">

### Author: ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)
#### Post date: [2019年十一月14日 19:08 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/12 "2019-11-14T19:08:11Z")

</div>

我不太清楚自动广告的状态，因为其文档表述含糊……但根据这篇帖子，它现在可能“直接生效”了：

> [@AdSense Auto ads](https://meta.discourse.org/t/adsense-auto-ads/94104/43):
>
> Google has released some kind of an update to Auto Ads in October, but still they don’t seem to provide good demos or screenshot on what to expect. Also according to the release notes the code can now be served via any regular AdSense unit, so integration to Discourse is now seamless? Anyone running this in production? I would be very interested in seeing this live.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2023年八月10日 04:09 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/13 "2023-08-10T04:09:37Z")

</div>



---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [2023年八月10日 07:02 UTC](https://meta.discourse.org/t/how-to-add-google-auto-ads-code-in-head/109672/14 "2023-08-10T07:02:15Z")

</div>


