- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2020-01-26T14:44:27+09:00","default:honma","honma")
#author("2020-01-26T14:48:44+09:00","default:honma","honma")
* PukiwikiでもGoogle AdSense [#yb3ad1e7]
Google AdSense に対応する際、htmlinsert を利用させてもらっていたが、編集を繰り返した際に PV が途方もない数になってしまい広告が出ないようになってしまった。~
どこかで、プラグインを作っている人はいないかと探してみると、
参考:[[PukiWikiのMenuBarにGoogle AdSenseをつけるプラグイン:http://www.ohneta.net/wiki/index.php?PukiWiki%E3%81%AEMenuBar%E3%81%ABGoogle%20AdSense%E3%82%92%E3%81%A4%E3%81%91%E3%82%8B%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3]]
ところが、コンテンツの生成パターンが自分と異なったので、ちょっとアレンジをさせてもらった。~
ついでに、ログイン中は広告生成を抑止することで、変な PV が増えることも抑止してみた。
** ソースコード [#ea1c9d2e]
#highlight(php){{
<?php
define('PLUGIN_GOOGLE_ADSENSE_USAGE', '#google_AdSense(data_ad_client, data_ad_slot)');
define('PLUGIN_GOOGLE_ADSENSE_LOGIN', 'ログイン中');
function plugin_google_AdSense_convert()
{
$google_ad_client = '';
global $auth_user;
if (func_num_args()) {
$args = func_get_args();
if (!isset($args[0])) {
return PLUGIN_GOOGLE_ADSENSE_USAGE . '<br>';
} else {
$data_ad_client = $args[0];
}
$data_ad_slot = $args[1];
}
if ($auth_user) {
$ret_string = PLUGIN_GOOGLE_ADSENSE_LOGIN . '<br>';
} else {
$ret_string = '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>' . "\n";
$ret_string .= '<!-- ディスプレイ広告 -->' . "\n";
$ret_string .= '<ins class="adsbygoogle"' . "\n";
$ret_string .= ' style="display:block"' . "\n";
$ret_string .= ' data-ad-client="' . $data_ad_client . '"' . "\n";
$ret_string .= ' data-ad-slot="' . $data_ad_slot . '"' . "\n";
$ret_string .= ' data-ad-format="auto"' . "\n";
$ret_string .= ' data-full-width-responsive="true"></ins>' . "\n";
$ret_string .= '<script>' . "\n";
$ret_string .= ' (adsbygoogle = window.adsbygoogle || []).push({});' . "\n";
$ret_string .= '</script>' . "\n";
}
return $ret_string;
}
?>
}}
#highlight(end)
#ref(google_adsense.inc.php)
これを
[PukiWiki-DIR]/plugin/google_adsense.inc.php
として保存して、メニューバーの中で
#google_AdSense(data_ad_client値,data_ad_slot値)
とすることで、未ログイン時のみ広告が表示される。
~
~
#google_AdSense(ca-pub-9715522069222300,2620341980)