Discourse安装Google Adsense广告的方法

在使用 Discourse 进行服务的时候,很多人可能会希望安装一些广告服务。
对 Discourse 的广告服务来说还是可以的,因为广告服务的位置不是非常影响阅读。

Discourse 的 Google Adsense 广告服务是使用插件进行安装的。

方法如下:

1、编辑 app.yml 文件,添加广告插件代码
sudo -E -u discourse git clone https://github.com/discourse/discourse-adplugin.git
大概是这样子:

## Plugins go here
## see https://meta.discourse.org/t/19157 for details
hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - git clone https://github.com/discourse/docker_manager.git
          - git clone https://github.com/discourse/discourse-adplugin.git

或者:

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - sudo -E -u discourse git clone https://github.com/discourse/docker_manager.git
          - sudo -E -u discourse git clone https://github.com/discourse/discourse-adplugin.git

2、对 Discourse 进行重编译

cd /var/discourse
./launcher rebuild app

这个编译需要一些时间,你需要耐心等待编译的完成。
必要的话,你可以重新启动你的 Docker 的容器。
systemctl restart docker

3、通过管理员控制台查看插件。

以编译确定你的插件已经被安装了。

4、配置插件参数
在插件的配置界面中,需要配置的参数为:
adsense publisher code 和 adsense topic list top code
这 2 个参数可以从你google Adsense 中找到。
在good adsense的广告代码中你可以看到 data-ad-client 和 data-ad-slot 参数,这个 2 个参数就是你需要的值。

Discourse 中的 adsense publisher code 使用的是你代码中 ca-pub 开头的配置,在这里不需要输入 ca-pub-,你只需要输入这个配置后面的数字就可以了。


Discourse 中的 adsense topic list top code 使用的是你代码中 data-ad-slot 中配置参数后面的数字。

5、查看广告是否被屏蔽
广告有可能被 Discourse 的安全配置所屏蔽而导致你没有办法进行查看。
在 Discourse 中搜索关键字 CSP ,然后选择 安全性。
你需要启用 CSP,同时还需要将 https 添加到你的安全选项中。

6、查看广告是否显示
最后使用不同的浏览器或者清理掉当前浏览器的缓存后再次访问你的 Discourse 网站。
如果你能够看到你广告的展现,则说明配置已经成功了。