Google Analytics 4 から採用された Measurement ID(e.g. G-00000XXXXX) を Hugo で設定する

1.この記事で達成したいこと Google Analytics 4から採用された Measurement ID(e.g. G-00000XXXXX) の設定をHugoでしたい Tracking ID(e.g. UA-000000-2)を使ってGoogle Analyticsの設定をする方法は多く紹介されている Measurement IDの解説記事はあっても修正するファイルのパスが間違っていたりしていたので書こうと思った ※Google Analytics v4を使っている場合でも、Measurement IDとTracking ID(e.g. UA-000000-2)を併用することはできる。 ※詳細は「おまけ.Google Search Consoleを使うためにTracking IDも設定する」で書いているので最後まで読んでほしい。 2.前提 Measurement IDの取得方法については解説せず、以下の記事に譲りたい What happened to my Tracking ID? - Analytics Help 3.設定方法 以下を参考に進める Installation · adityatelange/hugo-PaperMod Wiki Google Analytics 4 Implementation in Hugo config.ymlを修正 パスは/path/to/${BLOG_DIR}/config.yml params: # Hugoにおける"production"の意味合いは、Hugoで立ち上げているウェブサイトがググって見つけることができるぐらいのニュアンス # 余談だが、ローカルで"Hugo server -D"を実行した場合のenvは"development" env: production googleAnalyticsID: <G-00000XXXXX> analytics-gtag.htmlを以下のパスに新たに作成 /path/to/${BLOG_DIR}/layouts/partials/analytics-gtag.html $ mkdir -p layouts/partials $ touch layouts/partials/analytics-gtag.html <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id={{ .Site.Params.GoogleAnalyticsID }}"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '{{ .Site.Params.GoogleAnalyticsID }}'); </script> PaperModが用意しているbaseof.htmlを以下のパスにコピーして修正 /path/to/${BLOG_DIR}/layouts/_default/baseof.html $ mkdir -p layouts/_default $ cp themes/PaperMod/layouts/_default/baseof.html layouts/_default/ $ find ./ -type f -name 'baseof.html' ./themes/PaperMod/layouts/_default/baseof.html ./layouts/_default/baseof.html <head> <!-- ここから追記 --> {{ if .Site.Params.GoogleAnalyticsID }} {{ partial "analytics-gtag.html" . }} {{ end }} <!-- ここまで追記 --> {{- partial "head.html" . }} </head> 4.設定ができたか確認する デプロイしてからGoogle Analyticsでアクセスがとれているか確認する以外の方法を考えてみたので書き残しておく。 ...

August 22, 2021 · 2 min · gkzz

Firebase Hosting と Google Domains で購入したカスタムドメインを接続する

この記事で達成したいこと 購入した独自ドメインのサブドメインをFirebase Hostingで使いたい 前提 カスタムドメインはgkzz.devとし、購入先はGoogle Domains Firebase Hostingではgkzz.devのサブドメインであるblogs.gkzz.devを指定する 設定がうまくできず、Firebase Hostingではgkzz.devを指定するように変更 blog.gkzz.devにアクセスが飛んできた場合はgkzz.devへリダイレクトするようにした 以下のFirebaseのドキュメントを参考に進める。 Connect a custom domain | Firebase Firebase Hostingの画面でカスタムドメインを登録 https://console.firebase.google.com からFirebaseのコンソール画面へログイン Firebase Hostingで使うプロジェクトを選択するか、新規に作成 ここでは既存のプロジェクトを選択している 画面左の Hosting をクリック Add custom domain をクリックするとポップアップ画面が表示される ポップアップ画面から Qucik setup 、 Aレコード を選択したら、デプロイ先となるカスタムドメイン名を入力し、IPアドレス をコピーしておく https://domains.google.com の画面左側の DNS をクリック Custom recordsにて左からデプロイしたい カスタムドメイン名 、A 、先ほどコピーした IPアドレス を入力していく 以上で、Firebase HostingとGoogle Domainで購入したカスタムドメインとの設定は完了した。 ※接続が確認できた画面は、自分のブログの画面しか確認できていない。Firebase HostingとGoogle Domainで購入したカスタムドメインとの設定のみした場合、どういう画面になるのかサンプルの資料はない。。 P.S. TXTレコードはGoogle Domainsで登録することなく終わったけど。。 TXTレコードはGoogle Domainsで登録することなく、Firebase HostingとGoogle Domainsで購入したカスタムドメインとの設定が終わってしまった。 ...

August 20, 2021 · 1 min · gkzz

Firebase HostingとGithub Actionsで独自ドメインのHugoプロジェクトのブログを自動デプロイする

1.この記事で達成したいこと 独自ドメインのHugoプロジェクトのブログを作ってみたい そのブログがこれ! –> https://gkzz.dev ブログはHugo + Firebase Hostingで構成し、デプロイはGithub Actionsで自動化したい これまでローカルからデプロイという運用対処としていた、、 ※Hugoのインストール手順はこちらに書いてあるのでどうぞ。 最新のHugoをインストールする方法とHugoの設定ファイルをtomlフォーマット以外にする方法 | gkzz.dev 2.前提 HugoのテーマはPaperMod Firebase HostingとGoogle Domainsで購入したカスタムドメインを接続する方法は以下のとおり実施している Firebase HostingとGoogle Domainsで購入したカスタムドメインを接続する | gkzz.dev ドメインはgkzz.devとする 3.環境情報 $ grep Ubuntu /etc/os-release NAME="Ubuntu" PRETTY_NAME="Ubuntu 20.04.2 LTS" $ hugo version hugo v0.87.0-B0C541E4 linux/amd64 BuildDate=2021-08-03T10:57:28Z VendorInfo=gohugoio $ 4.PaperModの導入 以下のPaperModのインストール手順を参考に進める https://github.com/adityatelange/hugo-PaperMod/wiki/Installation#method-2 ※ https://github.com/adityatelange/hugo-PaperMod をサブモジュールとする点がミソ! 「6.GitHub Actionsの設定ファイル(.github/workflows/*.yml)を用意」で活きてくる! $ hugo new site ${BLOG_DIR} -f yml $ cd ${BLOG_DIR} $ git init $ git submodule add --depth=1 https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod config.ymlのbaseURLにFirebase Hostingでホストしているカスタムドメインを指定 $ vi config.yml # https://github.com/adityatelange/hugo-PaperMod/wiki/Installation baseURL: https://gkzz.dev/ #languageCode: en-us title: gkzz.dev paginate: 5 theme: PaperMod 記事を書いてローカルサーバーにデプロイしてみる http://localhost:1313で接続できる $ hugo new posts/<記事のファイル名>.md $ hugo server -D 5.Firebaseのインストールと初期設定 以下のFirebase CLIのインストール手順を参考に進める Firebase CLI reference $ curl -sL https://firebase.tools | bash $ firebase login 略 Visit this URL on this device to log in: ******** <--- 表示されるURLからログイン認証を終える Waiting for authentication... ✔ Success! Logged in as ******** Firebase CLIからGithubのログイン認証を終え、またfirebaseServiceAccountというサービスアカウントを作成 対話形式で進んでいく firebaseServiceAccountはGithub Actions上でFirebase CLIが使うもの ## 改めて設定し直したい場合 $ firebase init hosting:github $ firebase init hosting $ firebase init hosting You're about to initialize a Firebase project in this directory: /path/to/${BLOG_DIR} === Project Setup First, let's associate this project directory with a Firebase project. You can create multiple project aliases by running firebase use --add, but for now we'll just set up a default project. ? Please select an option: Use an existing project ? Select a default Firebase project for this directory: i Using project ${GCP_PROJECT_ID} === Hosting Setup Your public directory is the folder (relative to your project directory) that will contain Hosting assets to be uploaded with firebase deploy. If you have a build process for your assets, use your build's output directory. ## ${BLOG_DIR}からみた相対パス。PaperModの場合、public ? What do you want to use as your public directory? public ## シングルページではないのでNo ? Configure as a single-page app (rewrite all urls to /index.html)? No ## Yes ? Set up automatic builds and deploys with GitHub? Yes ✔ Wrote public/404.html ✔ Wrote public/index.html i Detected a .git folder at /path/to/${BLOG_DIR} i Authorizing with GitHub to upload your service account to a GitHub repository's secrets store. Visit this URL on this device to log in: ******** <--- 表示されるURLからFirebase CLIからGithubのログイン認証を終える Waiting for authentication... ✔ Success! Logged into GitHub as ${GITHUB_USERNAME} ## ${GITHUB_USERNAME}/${BLOG_DIR} ? For which GitHub repository would you like to set up a GitHub workflow? (format: user/repository) ${GITHUB_USERNAME}/${BLOG_DIR} ✔ Created service account ******** with Firebase Hosting admin permissions. ✔ Uploaded service account JSON to GitHub as secret ******** i You can manage your secrets at https://github.com/${GITHUB_USERNAME}/${BLOG_DIR}/settings/secrets. ## 後ほど自分で書くのでNo ? Set up the workflow to run a build script before every deploy? No ✔ Created workflow file /path/to/${BLOG_DIR}/.github/workflows/firebase-hosting-pull-request.yml ## Yes ? Set up automatic deployment to your site's live channel when a PR is merged? Yes ## main (Firebase Hostingへデプロイするときのブランチ) ? What is the name of the GitHub branch associated with your site's live channel? main ✔ Created workflow file /path/to/${BLOG_DIR}/.github/workflows/firebase-hosting-merge.yml i Action required: Visit this URL to revoke authorization for the Firebase CLI GitHub OAuth App: ******** i Action required: Push any new workflow file(s) to your repo i Writing configuration info to firebase.json... i Writing project information to .firebaserc... ✔ Firebase initialization complete! ここまでくると、Github Actions上でFirebase CLIが使うfirebaseServiceAccountというサービスアカウントがご自身のブログのリポジトリの「settings > secrets」のページから登録されていることが確認できる。 ...

August 20, 2021 · 4 min · gkzz

最新のHugoをインストールする方法とHugoの設定ファイルをtomlフォーマット以外にする方法

この記事で解決したい課題 Ubuntu上で最新のHugoをインストールしたい Hugoの設定ファイルをtomlフォーマット以外にしたい 環境情報 $ grep Ubuntu /etc/os-release NAME="Ubuntu" PRETTY_NAME="Ubuntu 20.04.2 LTS" $ hugo version hugo v0.87.0-B0C541E4 linux/amd64 BuildDate=2021-08-03T10:57:28Z VendorInfo=gohugoio $ 課題1. Ubuntu上で最新のHugoをインストールしたい 以下のHugoの初期設定手順に従ってapt-getでインストールをおこなうと、最新版ではないHugoがインストールされてしまう https://gohugo.io/getting-started/installing#debian-and-ubuntu apt-getでインストールしたHugoのバージョン $ hugo version Hugo Static Site Generator v0.68.3/extended linux/amd64 BuildDate: 2020-03-25T06:15:45Z どうやったか? 以下のHugoのリリースページからHugoのパッケージをダウンロードし、dpkgコマンドでインストール https://github.com/gohugoio/hugo/releases 今回は現時点で最新のv0.87.0をインストールする unameコマンドで64bitか32bitか確認もしておく $ sudo uname -m x86_64 $ wget https://github.com/gohugoio/hugo/releases/download/v0.87.0/hugo_0.87.0_Linux-64bit.deb $ sudo dpkg -i hugo_0.87.0_Linux-64bit.deb Hugoのバージョンはv0.87.0であることが確認できた $ hugo version hugo v0.87.0-B0C541E4 linux/amd64 BuildDate=2021-08-03T10:57:28Z VendorInfo=gohugoio Hugoのバージョンアップグレード方法 引き上げたいバージョンのパッケージをwgetで取ってきてからdpkgコマンドでインストールするだけ! $ wget https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_extended_0.88.1_Linux-64bit.deb $ sudo dpkg -i hugo_extended_0.88.1_Linux-64bit.deb $ hugo version hugo v0.88.1-5BC54738+extended linux/amd64 BuildDate=2021-09-04T09:39:19Z VendorInfo=gohugoio $ 課題2. Hugoの設定ファイルをtomlフォーマット以外にしたい 無事Hugoがインストールできたら、いよいよウェブサイトの雛形を作るのだが、課題1で紹介したHugoの初期設定手順のページに記載されているコマンドを実行すると、tomlフォーマットの設定ファイルが生成される https://gohugo.io/getting-started/quick-start/#step-2-create-a-new-site $ hugo new site <directory-name> tomlフォーマットの場合、どうも見にくくなってしまう印象がある Gitlab Runnerの設定ファイルはtomlフォーマットで書くけど、読みづらかった 例:https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnersdockerservices-section どうやったか? -f オプションをつけて yml , json を指定するだけ $ hugo new site --help | grep "format" -f, --format string config & frontmatter format (default "toml") $ hugo new site toml-start $ hugo new site yml-start -f yml $ hugo new site json-start -f json 以下のページではあるフォーマットでの書き方を他のフォーマットにコンバートしてくれるので、サンプルコードを好みのフォーマットに読み替えたいときに使えそう https://gohugo.io/getting-started/configuration/ 参考 Hugoの公式サイトで紹介されているHugoのインストール手順 https://gohugo.io/getting-started/installing debパッケージのインストール方法 Linuxでの32bit/64bitの確認方法(CPU、カーネルのBit数) | ぱーくん plus idea 【 debパッケージをインストールする 】 | 日経クロステック(xTECH)

August 20, 2021 · 1 min · gkzz