Hugo 博客 SEO 优化 - 自动调用 Google Search Console API

发布时间: 更新时间: 总字数:617 阅读时间:2m 作者: IP上海 分享 网址

Google Search Console 会为博客推荐 SEO 关键字,gseo 工具能自动调用其API并将对应页面的关键字自动注入由 gohugo 渲染的 markdown 中。


gseo is a golang client to optimize hugo seo by Google Search Console. read gseo spec for more information.

build-test GoDoc Go Report Card


google search console render hugo post


  • source
go install
  • bin
curl -Lfs -o gseo{linux|darwin|windows}
chmod +x gseo


show help

  • root help
$ gseo -h
 golang client to optimize [hugo]( seo by Google Search Console.

  gseo [flags]
  gseo [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  init        init gseo configure
  keyword     show site keywords
  render      render hugo post markdown files
  sites       site list
  version     Print the version number of gseo.

  -h, --help      help for gseo
  -v, --verbose   debug info.

Use "gseo [command] --help" for more information about a command.
  • keywords help
$ gseo help keyword
download hugo post keywords from Google Search Console API, and cache it in `./.gseo/` dir

  gseo keyword [flags]

  -h, --help          help for keyword
  -l, --last int      last days (default 90)
  -s, --site string   site url
  • render help
$ gseo help render
render hugo post markdown files.
default args is:
  gseo render --content PATH_OF_HUGO_CONTENT --position 10 --ctr 0 --impressions 100 --clicks 0.3 --max 8 --dryrun

  gseo render [flags]

  -k, --clicks float        >=clicks to render seo.
      --content string      hugo content path
  -c, --ctr float           ctr = clicks / impressions to render seo, and 0 <= ctr <= 1. (default 0.3)
  -r, --dryrun              dry run mode.
  -h, --help                help for render
  -i, --impressions float   >=impressions to render seo. (default 100)
  -m, --max int             max seo items, -1 is un-limit. (default 8)
  -p, --position float      >=position to render seo. (default 10)
  -s, --skip-err            skip error. (default true)

how to get Google Search Console API token

  • step-1: config google auth secret in file ~/.gseo/client_secret.json (can i build it as constant in go pkg?)
$ cat ~/.gseo/client_secret.json
  • step-2: init google token
$ gseo init
Please enter Google API client_secret.json path (default is /Users/xiexianbin/.gseo/client_secret.json):
init config success!
Go to the following link in your browser then type the authorization code:
---> <Visit the URL above, and paste the (authorization code/授权代码) over here> <---
Saving credential file to: /Users/xiexianbin/.gseo/token.json
init Google API OAuth2.0 token success!


  • list sites
$ gseo sites
PermissionLevel  SiteUrl
  • show special site keywords, while write keywords to cache file ~/.gseo/cache-<date>.json
$ gseo keyword -s ""
==> get 1000 lines Results
==> get 1000 lines Results
Write to file /Users/xiexianbin/.gseo/cache-2022-06-03.json success, bytes 276427
  • render demo, read keywords from cache file ~/.gseo/cache-<date>.json
$ export HUGO_CONTENT="/Users/xiexianbin/workspace/code/"

# default args
$ gseo render --content ${HUGO_CONTENT} --position 10 --ctr 0 --impressions 10 --clicks 0.1 --max 8 --dryrun true

# my args 1 **
$ gseo render --content ${HUGO_CONTENT} --position 10 --ctr 0 --impressions 100 --clicks 0.3 --max 8

# my args 2 *****
$ gseo render --content ${HUGO_CONTENT} --position 0 --ctr 0 --impressions 1 --clicks 0 --max 8 --dryrun true

now, you can find seo keywords has auto update/render to ${HUGO_CONTENT}.


  • keyword can not contain :, occur golang yaml parse err


Hugo 博客 SEO 优化 - 自动调用 Google Search Console API


# run some TestCase
go test -v -run TestClient google/client_test.go

# build
$ make all


Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数