Mezzanine に Google Adsense の広告を組み込むためにやったことを備忘として残します。

[TOC]


前提

Google Adsense でアカウントを登録し、広告を作成する必要があります。
アカウント作成方法については、以下の記事が参考になります。
2017年最新】Google AdSenseの申請手順(保存版) - JALマイル参戦navi


組み込み方法

Google Adsense から取得したコードを元に HTML を作成する

Google Adsense から取得したコードを コピー&ペーストして、拡張子 html のファイルを作成します。 自分は、google_adsense.html として 作成しました。

  • google_adsense.html
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <!-- blog上下の広告 -->
    <ins class="adsbygoogle"
         style="display:block"
         data-ad-client="ca-pub-xxxxxxxxxxxxxxx"
         data-ad-slot="xxxxxxxxxxxxxxx"
         data-ad-format="auto"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    

HTML を blog/includes 配下に配置

自分は、{mezzanine_home}/templates/blog/includes 配下に配置しました。 {mezzanine_home} ですので、Mezzanine のインストールディレクトリですので、環境ごとに読み替えてください。

blog_post_detail.html と blog_post_list.html に include の記述を追加

{mezzanine_home}/templates/blog/blog_post_detail.html{mezzanine_home}/templates/blog/blog_post_list.html以下の記述を追加します。

  • 追加する記述

<div class="blog-list-detail">
{% include "blog/includes/google_adsense.html" %}       
</div>    

補足.右側メニュー部に組み込む場合

{mezzanine_home}/templates/blog/includes/filter_panel.html右側メニュー部の HTML になります。
広告サイズは、右側メニュー部向けに調整したほうがいいかと思います。


ライブラリがあるか確認した

django に Goolge Adsense を組み込む際に使えるライブラリがないか調べたところ、以下が見つかりました。
razisayyed/django-ads: Ads Management System for Django Framework
README.md見る限り、使いやすそうなので今度置き換えてみようかと思います。

以上です。

コメント