# 在哪里放置广告脚本？

**URL:** https://meta.discourse.org/t/where-to-place-ad-script/125640
**Category:** Support
**Created:** [2019年八月13日 08:01 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640 "2019-08-13T08:01:43Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![calebs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/calebs/32/148838_2.png) [@calebs](https://meta.discourse.org/u/calebs)
#### Post date: [2019年八月13日 08:01 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/1 "2019-08-13T08:01:43Z")

</div>

我们对 Discourse 支持的所有广告网络都不太满意，但我们发现了一个在我们博客上运行完美的广告网络，我们很乐意在论坛上也采用它。不过，这需要我们将一段代码复制并粘贴到网站的 HTML 中。具体应该在哪里操作？这样能行吗？

---

<div class="post-metadata">

### Author: ![dax](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dax/32/244677_2.png) [@dax](https://meta.discourse.org/u/dax)
#### Post date: [2019年八月13日 09:59 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/2 "2019-08-13T09:59:28Z")

</div>

只需使用主题组件即可实现

> [@Developing Discourse Themes & Theme Components](https://meta.discourse.org/t/developer-s-guide-to-discourse-themes/93648):
>
> Discourse Themes and Theme Components can be used to customize the look, feel and functionality of Discourse’s frontend. This section of the developer guides aims to provide all the reference materials you need to develop simple themes for a single site, right up to complex open-source theme components. This introduction aims to provide a map of all the tools and APIs for theme development. If you prefer a step-by-step tutorial for theme development, jump straight to: Themes vs. Theme Compon…

---

<div class="post-metadata">

### Author: ![calebs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/calebs/32/148838_2.png) [@calebs](https://meta.discourse.org/u/calebs)
#### Post date: [2019年八月13日 10:10 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/3 "2019-08-13T10:10:52Z")

</div>

谢谢，但我不是开发者。我的知识仅限于搭建 Discord（花了我一周时间）和修改网站设置。

---

<div class="post-metadata">

### Author: ![dax](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dax/32/244677_2.png) [@dax](https://meta.discourse.org/u/dax)
#### Post date: [2019年八月13日 10:24 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/4 "2019-08-13T10:24:33Z")

</div>

那么你可以从这里开始

> [@Beginner's guide to using Discourse Themes](https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966):
>
> This is a crash course in Discourse theme basics. The target audience is everyone who is not familiar with Discourse themes. If you’ve already used Discourse theme / theme components, this guide is probably not something you need to read. What are themes and theme components? A theme or theme component is a set of files packaged together designed to either modify Discourse visually or to add new features. Let’s start with themes. Themes In general, themes are not supposed to be compatible …

你的目标是创建一个主题组件，将 `<script>` 标签添加到 `/head` 选项卡，并在你的主主题中激活该主题组件。

激活主题组件后，你需要按照以下步骤白名单化你的脚本 URL：

> [@Mitigate XSS Attacks with Content Security Policy](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243):
>
> bookmark This guide explains how to use Content Security Policy (CSP) to mitigate Cross-Site Scripting (XSS) attacks in Discourse. It covers CSP basics, configuration, and best practices. person_raising_hand Required user level: Administrator Summary Content Security Policy (CSP) is a crucial security feature in Discourse that helps protect against Cross-Site Scripting (XSS) and other injection attacks. This guide covers the basics of CSP, how it’s implemented in Discourse, and how to c…

---

<div class="post-metadata">

### Author: ![sfoster](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@sfoster](https://meta.discourse.org/u/sfoster)
#### Post date: [2019年八月13日 11:04 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/5 "2019-08-13T11:04:09Z")

</div>

欢迎来到这个圈子，我们处境相同。但先提个醒：此方法不适用于主题组件，因此建议的解决方案行不通。我们和许多其他人（参见相关主题）都尝试过这条路径。在使用主题组件时，广告脚本无法在预期位置显示。那位建议使用在 /head 中插入脚本的评论者可能并不了解广告脚本的实际部署方式。我们尝试过主题组件，但由于其功能限制，最终无法实现。

归根结底，你需要一个插件来实现在其他广告网络中插入 标签的功能。遗憾的是，目前尚无此类插件。唯一的希望是 Discourse 能在 Ads 插件中添加通用的广告脚本支持，以兼容需要注入到广告区域的第三方广告脚本。目前你基本上只能使用 Google DFP；如果使用其他广告提供商，那就束手无策，无法实现论坛变现。这令人遗憾，因为还有许多优秀的广告网络（例如 Adbutler、Openx 等），你不应该被迫使用 Google DFP 来投放广告。

我们衷心希望 Discourse 能在 Ads 插件中添加通用的广告脚本功能。我不认为这有多难实现，但老实说，我完全不懂 Ruby 和 Ember 编程，所以不知道具体该怎么做。理论上，他们只需在插件中添加一个文本框，用于接受 标签，然后将这些标签注入到插件已指定的广告区域即可。拥有一个通用的脚本输入框，而不是只能接受纯 HTML 的输入框，基本上就能兼容所有广告网络，而无需为每个广告网络单独编写代码。

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2019年八月13日 14:30 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/6 "2019-08-13T14:30:14Z")

</div>

Caleb，出于好奇，你的博客上使用的是哪项服务？

---

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [2019年八月14日 03:14 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/7 "2019-08-14T03:14:22Z")

</div>

在不了解具体背景的情况下，要回应您的观点确实有些困难。如果您能提供更多具体信息，说明您指的是哪个广告网络，以及它具体是如何无法为您正常工作的，那将非常有帮助。

> [@sfoster](#):
>
> 但提醒一下，这不适用于主题组件，因此建议的解决方案行不通

这并不完全正确，实际情况取决于您尝试使用的广告网络。如果确实严格要求广告网络的脚本必须放置在 `</body>` 标签旁边——这种情况不太可能——那么问题其实出在广告网络自身，因为它们设定了如此严格且不合理的限制。

在主题中添加的脚本会被整合到一个 `theme.js` 文件中，然后注入到文档的 `<head>` 标签中。

> <https://github.com/discourse/discourse/commit/6acdea37c4e77723c774152e9076dfedda99493a>
>
> \* extract inline js when baking theme fields
> \* destroy javascript cache when de…stroying theme fields
> 
> This work is needed to support CSP work

这为我们提供了一定程度的控制能力，使我们能够实现一些有益的功能，例如防止损坏的主题导致整个网站瘫痪。

> <https://github.com/discourse/discourse/pull/7589/files>

> [@sfoster](#):
>
> 我们尝试过使用主题组件，但无法实现，因为主题组件的功能存在限制。

这也只是部分正确。主题可以修改客户端上的_任何内容_。这意味着在 Discourse 中用 JavaScript 编写的任何内容都可以被主题访问。

事实上，整个 [官方广告插件](https://github.com/discourse/discourse-adplugin) 都可以转换为一个主题组件，并且能够正常工作。唯一缺失的功能是无法添加 `ads.txt` 文件，因为这需要访问后端。我们尚未这样做的原因是，插件是更干净的实现方式——特别是因为它支持多个广告提供商。所以，主题确实存在一些限制，但这并不是这里的关键问题。

> [@sfoster](#):
>
> 这令人遗憾，因为还有许多其他优秀的广告网络（例如 Adbutler、Openx 等），您不应该被迫使用 Google DFP 来投放广告。

这里有一个非常重要的说明：Discourse 是一个单页应用（SPA）。这意味着一旦您在初始页面加载时启动了应用，之后发生的所有导航都由 Discourse 处理，而不是由您的浏览器处理。

这一点很重要，因为广告网络必须提供必要的钩子（hooks），以便单页应用能够更新页面信息并投放新广告。我对 Adbutler 或 Openx 不太了解，但我找不到任何关于这两个提供商支持单页应用的文档。

因此，如果广告网络没有提供必要的钩子，这真的是 Discourse 的错吗？

> [@sfoster](#):
>
> […] 您不应该被迫使用 Google DFP 来投放广告。

您并没有真正被迫使用 DFP。它只是官方广告插件中可用的六个选项之一，我在这里列出供您参考：

1. DFP
2. AdSense
3. Google ads
4. Amazon 广告
5. Codefund
6. Carbon Ads

> [@sfoster](#):
>
> 好吧，希望 Discourse 能在广告插件中添加一个通用的广告脚本。我不太明白为什么这很难做到。

再次强调，由于 Discourse 是单页应用，“通用”方案根本无法奏效。

当然，您可以在主题中加载任何您想要的广告脚本，但它们只会在初始页面加载时触发。之后发生的任何页面变化都会被广告网络脚本忽略——除非该广告网络提供了必要的钩子。

以下是支持单页应用的广告网络集成示例：

[https://github.com/discourse/discourse-adplugin/blob/master/assets/javascripts/discourse/components/google-dfp-ad.js.es6#L191-L230](https://github.com/discourse/discourse-adplugin/blob/master/assets/javascripts/discourse/components/google-dfp-ad.js.es6#L191-L230)

> [@sfoster](#):
>
> 提供一个通用的脚本框，而不是只能接受纯 HTML 的框，基本上可以适配所有广告网络，而无需为每个广告网络编写特定代码。

实际上，您确实需要为每个广告网络编写特定的代码实现。

此外，我认为您混淆了自家广告（house ads）与广告网络投放的广告。该输入框仅支持 HTML 的原因在于，它旨在让您创建自己的自家广告。这是一个非常简单的实现，插件中已经完成了相关配置，因此如果您只想投放 **自己的** 广告，就无需折腾单页应用的钩子。

此外，如果您真的希望将另一个广告网络纳入广告插件，我们已有基于社区贡献添加新广告网络的成功先例。

最近的例子是 Carbon Ads 的集成：

> <https://github.com/discourse/discourse-adplugin/pull/73/files>
>
> This PR adds Carbon Ads support to Discourse-AdPlugin  
> 
> \`\`\`
> CARBON ADS GUIDE…LINES
> There can be only one ad per page.
> The ad should be visible above-the-fold (upper 800 pixels) for desktop browser.
> The ad should be visible within 3x of the mobile viewport height, starting from the top of the mobile browser.
> The ad tag should be placed exactly where you'd like the ad to display.
> The ad image must remain 130 × 100 pixels, unmodified and unobscured.
> The ad text is up to 80 characters in length and must be clearly visible.
> \`\`\`
> 
> To comply with this, there are only two possible locations for the ad: above topic lists and above posts stream.
> 
> \---
> 
> !\[image\](https://user-images.githubusercontent.com/19251112/55683310-85413b80-593e-11e9-97d9-1ca9d9b2ab68.png)
> 
> !\[image\](https://user-images.githubusercontent.com/19251112/55683265-28de1c00-593e-11e9-8e7c-4edc1fe0e7da.png)
> !\[image\](https://user-images.githubusercontent.com/19251112/55683269-2f6c9380-593e-11e9-9b19-a95de94aa4be.png)
> !\[image\](https://user-images.githubusercontent.com/19251112/55683274-34c9de00-593e-11e9-8d30-53df666506ca.png)

不过，正确完成所有集成的责任在于您自己。

如果您觉得这样的实现过于棘手，那也无妨。我很擅长给汽车换轮胎或更换机油，但如果需要检修变速箱，我就会找一位比我更专业的人，并付费请他们完成。

我想表达的是，如果您真的想使用另一个广告网络，但自己无法完成，我建议您先在 #Marketplace 板块创建一个主题。如果您有合适的预算，我相信一定会有开发者注意到并愿意提供帮助。

---

<div class="post-metadata">

### Author: ![sfoster](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@sfoster](https://meta.discourse.org/u/sfoster)
#### Post date: [2019年八月14日 11:23 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/8 "2019-08-14T11:23:25Z")

</div>

感谢您详细的回复和帮助。这里有很多内容需要消化。

不过，我认为这里存在误解。AdButler 是一种广告服务器（Ad Server），类似于 Google DFP。这类平台帮助您管理自有广告并与广告主对接。而像 Adsense（以及据称的 Carbon）则属于广告网络（Ad Network）。广告网络的运作方式与广告服务器有所不同。我们的问题出在广告服务器，而非广告网络。为此造成的混淆，深表歉意。

我们需要通过网站盈利以维持运营，为此必须使用广告服务器来管理广告主。我们使用的是 Ad Butler。Discourse 自带的“内部广告”功能远不能满足需求。据我所知，Discourse 官方广告插件目前仅支持 Google DFP 这一种广告服务器。在我看来，Google DFP 因多种原因表现不佳，不过这与此讨论无关。当然，如果由于广告插件仅支持 Google DFP 而迫使我们不得不使用它，我们也会接受，因为 Discourse 的其他诸多优势在很大程度上抵消了使用 Google DFP 带来的极大不便。

我完全理解，开发支持 Google DFP 之外其他广告服务器的插件是我们的责任，Discourse 无法满足每一个需求，尤其是它本身已提供了大量开箱即用且极其出色的功能。我们曾将此需求提交至市场，一些有经验的 Discourse 开发者告诉我们，若不开发插件则无法实现，且相关工作成本高昂、耗时费力，相对于看似简单的问题而言，投入显得不成比例。

您说得对，我目前对 Discourse 的运作机制尚不了解，因为我们才刚开始使用它，并在学习 Ruby 和 Ember。然而，以 WordPress 为例，我们只需将 Ad Butler 的广告脚本放入小部件（Widget）区域即可直接使用，无需任何编程。因此，我有些惊讶地发现，让 Ad Butler 在 Discourse 上运行竟如此麻烦。我理解 Discourse 显然与 WordPress 截然不同，也复杂得多，但我仍不明白为何此类功能需要如此大量的编码工作。

总之，我们将继续尝试，并联系 Ad Butler 方面，看他们能否提供帮助。协助开发 Discourse 集成无疑符合他们的利益，因为他们可能因此获得大量客户（一旦使用过 Ad Butler，就再也回不去 DFP 了）。我们也会研究 Carbon Ads 的集成方案，了解其工作原理，看看是否能根据我们的需求进行调整。

再次感谢。

---

<div class="post-metadata">

### Author: ![sfoster](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@sfoster](https://meta.discourse.org/u/sfoster)
#### Post date: [2019年八月14日 11:49 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/9 "2019-08-14T11:49:31Z")

</div>

快速跟进一下，我查看了广告插件中 Carbon Ads 的代码，对实现方式有了概念性的理解，因此希望能以此为基础来实施 Ad Butler。感谢提醒关于 Carbon Ads 的事宜。

---

<div class="post-metadata">

### Author: ![Jeremie\_Leroy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jeremie_leroy/32/84427_2.png) [@Jeremie\_Leroy](https://meta.discourse.org/u/Jeremie_Leroy)
#### Post date: [2019年八月19日 19:47 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/10 "2019-08-19T19:47:49Z")

</div>

你好 @sfoster，关于 AdButler 集成有什么最新消息吗？

---

<div class="post-metadata">

### Author: ![sfoster](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@sfoster](https://meta.discourse.org/u/sfoster)
#### Post date: [2019年八月20日 09:20 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/11 "2019-08-20T09:20:20Z")

</div>

不。我们已经联系了几位程序员，但到目前为止，还没有人给我们提供确切的报价和时间表。在 WordPress 中，我们将代码放入小部件即可搞定。让我们感到惊讶的是，Discourse 中的编程竟然如此复杂。无论如何，我们会再仔细看看，所以我会随时向您更新。不过，我们倾向于直接迁移到 DFP，因为核心插件已支持该功能，尽管我们真的不太乐意不得不这样做。

---

<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年九月9日 15:08 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/12 "2019-09-09T15:08:46Z")

</div>

4 篇帖子已拆分至新主题：[官方广告插件中的 AdButler](https://meta.discourse.org/t/adbutler-in-the-official-ad-plugin/128050)

---

<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年四月4日 11:54 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/13 "2023-04-04T11:54:08Z")

</div>



---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [2023年四月4日 12:52 UTC](https://meta.discourse.org/t/where-to-place-ad-script/125640/14 "2023-04-04T12:52:35Z")

</div>


