Google AdSense官网
提交申请后,Google给了我一段验证代码,要求我将其添加到网站的<head>
部分。
首先,我更新了config.toml
文件,添加AdSense ID:
[extra]
# 已有配置
google_analytics_id = "G-8JD13N7PHS"
# 新增AdSense ID
google_adsense_id = "ca-pub-XXXXXXXXXXXXXXXX" # 替换为你的实际ID
然后,在base.html
模板文件的<head>
部分添加验证代码:
<!-- Google AdSense -->
{% if config.extra.google_adsense_id %}
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client={{ config.extra.google_adsense_id }}" crossorigin="anonymous"></script>
{% endif %}
添加后重新构建站点并部署,然后在AdSense控制面板点击"验证"按钮。
添加验证代码后,需要等待Google审核我的网站。审核过程大约持续了5天,期间Google检查了我的网站内容是否符合其政策要求。幸运的是,我的博客获得了批准。
审核通过后,我收到了邮件通知,AdSense账户正式激活。
账户激活后,我开始创建广告单元。AdSense提供了多种广告格式,我选择了以下几种:
我创建了一个名为templates/components/adsense-in-article.html
的组件:
<div class="adsense-container adsense-in-article">
<ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="{{ config.extra.google_adsense_id }}"
data-ad-slot="1234567890"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
注意:这里的data-ad-slot
需要替换为AdSense控制面板中创建广告单元时获得的实际ID。
类似地,我创建了templates/components/adsense-sidebar.html
:
<div class="adsense-container adsense-sidebar">
<ins class="adsbygoogle"
style="display:block"
data-ad-client="{{ config.extra.google_adsense_id }}"
data-ad-slot="0987654321"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
接下来,我将广告组件整合到我的博客模板中。首先,我修改博文模板blog.html
:
<div class="blog-post">
<h1>{{ page.title }}</h1>
<!-- 文章前半部分 -->
{% if page.content | length > 1500 %}
{{ page.content | safe | slice(end=page.content | length / 2) | safe }}
<!-- 插入文章内广告 -->
{% include "components/adsense-in-article.html" %}
<!-- 文章后半部分 -->
{{ page.content | safe | slice(start=page.content | length / 2) | safe }}
{% else %}
{{ page.content | safe }}
{% endif %}
</div>
这段代码确保只有在文章内容足够长时才插入广告,避免短文章中出现过多广告。
然后,我将侧边栏广告添加到base.html
中:
<div class="sidebar pure-u-1 pure-u-md-1-5">
<!-- 个人介绍等内容 -->
<!-- 添加侧边栏广告 -->
{% include "components/adsense-sidebar.html" %}
<!-- 导航链接 -->
</div>
为了让广告看起来更整洁,我在static/css/style_new.css
中添加了一些样式:
/* 广告容器基本样式 */
.adsense-container {
margin: 2em 0;
text-align: center;
overflow: hidden;
width: 100%;
clear: both;
}
/* 文章内嵌广告 */
.adsense-in-article {
padding: 1em 0;
border-top: 1px solid #eaeaea;
border-bottom: 1px solid #eaeaea;
background-color: rgba(250, 250, 250, 0.5);
}
/* 侧边栏广告 */
.adsense-sidebar {
margin-top: 2em;
}
/* 响应式调整 */
@media (max-width: 768px) {
.adsense-sidebar {
margin: 1em 0; /* 移动端缩小边距 */
}
}
在运行一段时间后,我发现了一些优化广告表现的技巧:
经过测试,我发现以下位置点击率最高:
一开始我放置了较多广告,但发现这影响了用户体验。最终我采取了以下策略:
我创建了不同风格的广告单元,并轮换测试它们的表现:
实施AdSense大约3个月后,我开始看到稳定的收益。虽然收入不足以让我辞职(哈哈),但足以支付博客的服务器费用和一些咖啡钱。
我注意到以下有趣现象:
在配置过程中,我遇到了一些问题,分享解决方案:
最初我的广告没有显示,原因是JavaScript加载顺序问题。确保AdSense脚本在<head>
中加载,而广告单元的激活代码在对应位置。
一些广告在移动设备上显示异常,解决方法是添加响应式CSS并使用data-full-width-responsive="true"
属性。
某些广告与我的内容样式冲突,我通过增加CSS特异性解决了这个问题:
/* 增加CSS特异性解决样式冲突 */
body .blog-post .adsense-container {
/* 覆盖样式 */
}
从我的经验来看,使用AdSense时有几点需特别注意:
将Google AdSense添加到Zola博客是一个相对简单的过程,但要获得良好收益需要不断优化和测试。最重要的是,保持内容质量和用户体验始终是第一位的,广告只是锦上添花。
通过本文的步骤,你也可以为自己的博客添加AdSense,开始探索博客变现的道路。记住,内容为王,优质内容才是吸引读者和获得收益的根本。
如果你有任何关于博客变现的经验或问题,欢迎在评论区分享!