From e3f606996897a0c68359787448cdf7cf03b0f800 Mon Sep 17 00:00:00 2001 From: Xin Date: Mon, 30 Oct 2023 19:48:48 -0400 Subject: [PATCH] chore: support `services.googleAnalytics.ID` (#173) * chore: support `services.googleAnalytics.ID` and deprecate `site.googleAnalytics` * chore: remove warning --- exampleSite/content/docs/guide/configuration.md | 6 ++++-- exampleSite/hugo.yaml | 4 +++- layouts/partials/google-analytics.html | 10 ++++++++-- layouts/partials/head.html | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/exampleSite/content/docs/guide/configuration.md b/exampleSite/content/docs/guide/configuration.md index 8927aa0..25e7000 100644 --- a/exampleSite/content/docs/guide/configuration.md +++ b/exampleSite/content/docs/guide/configuration.md @@ -252,8 +252,10 @@ excludeSearch: true ### Google Analytics -To enable Google Analytics, set the `googleAnalytics` parameter in the config file: +To enable [Google Analytics](https://marketingplatform.google.com/about/analytics/), set `services.googleAnalytics.ID` flag in `hugo.yaml`: ```yaml {filename="hugo.yaml"} -googleAnalytics: G-XXXXXXXXXX +services: + googleAnalytics: + ID: G-MEASUREMENT_ID ``` diff --git a/exampleSite/hugo.yaml b/exampleSite/hugo.yaml index a7c6be8..bf95137 100644 --- a/exampleSite/hugo.yaml +++ b/exampleSite/hugo.yaml @@ -7,7 +7,9 @@ enableGitInfo: true # enableEmoji: false hasCJKLanguage: true -# googleAnalytics: G-XXXXXXXXXX +# services: +# googleAnalytics: +# ID: G-MEASUREMENT_ID outputs: home: [HTML] diff --git a/layouts/partials/google-analytics.html b/layouts/partials/google-analytics.html index 3fd65aa..9219006 100644 --- a/layouts/partials/google-analytics.html +++ b/layouts/partials/google-analytics.html @@ -1,4 +1,10 @@ -{{ with .Site.GoogleAnalytics }} +{{/* site.GoogleAnalytics is deprecated in Hugo v0.120.0 */}} +{{/* it will be removed in a future version */}} +{{- $gtagID := "" -}} +{{- with site.GoogleAnalytics -}}{{ $gtagID = . }}{{- end -}} +{{- with site.Config.Services.GoogleAnalytics.ID -}}{{ $gtagID = . }}{{- end -}} + +{{- with $gtagID }} -{{ end }} +{{ end -}} diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 26f5fb1..d4b2632 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -25,7 +25,7 @@ - {{- if and .Site.GoogleAnalytics (eq hugo.Environment "production") }} + {{- if and (eq hugo.Environment "production") (or .Site.GoogleAnalytics .Site.Config.Services.GoogleAnalytics.ID) }} {{ partial "google-analytics.html" . }} {{- end }}