Hexo Next 主题添加 Google Adsense 和 Google Analytics 方法

项目背景

最近启动了一个新项目:电影台词AI文学创作系统
该系统旨在将电影台词转化为AI文学创作的灵感源泉,通过以下三个核心模块实现:

  1. 字幕抓取与清洗 → 获得纯净文本;
  2. 台词分段与解析 → 理解电影语境;
  3. AI识别与创作 → 输出微小说。

最终实现从影视语言到文学作品的智能转换。

项目内容逐渐丰富,便想到利用手头闲置的域名搭建展示页面,顺便记录一下 Hexo Next 主题添加 Google 服务的方法。


1. 添加 Google Analytics

环境信息

  • Hexo 版本:8.1.1
  • 主题:Next
  • 部署方式:本地部署

配置步骤

  1. 将主题文件夹下的 _config.yml 复制到项目根目录,并重命名为 _config.next.yml
  2. _config.next.yml 中找到以下配置段:
1
2
3
4
5
6
# Google Analytics
# See: https://analytics.google.com
google_analytics:
tracking_id: G-WJ48W3LM1R
# By default, NexT will load an external gtag.js script on your site.
# If you only need the pageview feature, set the following option to true to get a better performance.

将 tracking_id 替换为你自己的 Google Analytics 测量 ID(如 G-XXXXXXXXXX)。

无需手动插入 Google 提供的 JavaScript 代码,NexT 主题会自动加载 gtag.js。

2. 添加 Google Adsense

步骤一:放置 ads.txt

在 Hexo 项目根目录的 source 文件夹下,新建 ads.txt 文件,内容粘贴 Google Adsense 提供的验证信息。

步骤二:插入 Adsense 代码

打开主题布局文件:

next/layout/_partials/head/head.njk

在文件底部插入 Google Adsense 提供的 JavaScript 代码片段。

3. 生成与部署

配置完成后,在项目根目录执行以下命令:

1
hexo clean && hexo g && hexo d