chore: support services.googleAnalytics.ID (#173)

* chore: support `services.googleAnalytics.ID`

and deprecate `site.googleAnalytics`

* chore: remove warning
This commit is contained in:
Xin 2023-10-30 19:48:48 -04:00 committed by GitHub
parent de1286c7fc
commit e3f6069968
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 6 deletions

View File

@ -252,8 +252,10 @@ excludeSearch: true
### Google Analytics ### 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"} ```yaml {filename="hugo.yaml"}
googleAnalytics: G-XXXXXXXXXX services:
googleAnalytics:
ID: G-MEASUREMENT_ID
``` ```

View File

@ -7,7 +7,9 @@ enableGitInfo: true
# enableEmoji: false # enableEmoji: false
hasCJKLanguage: true hasCJKLanguage: true
# googleAnalytics: G-XXXXXXXXXX # services:
# googleAnalytics:
# ID: G-MEASUREMENT_ID
outputs: outputs:
home: [HTML] home: [HTML]

View File

@ -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 }}
<!-- Global site tag (gtag.js) - Google Analytics --> <!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script> <script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
<script> <script>
@ -10,4 +16,4 @@
gtag("config", "{{ . }}"); gtag("config", "{{ . }}");
</script> </script>
{{ end }} {{ end -}}

View File

@ -25,7 +25,7 @@
<!-- Google Analytics --> <!-- Google Analytics -->
{{- if and .Site.GoogleAnalytics (eq hugo.Environment "production") }} {{- if and (eq hugo.Environment "production") (or .Site.GoogleAnalytics .Site.Config.Services.GoogleAnalytics.ID) }}
<link rel="preconnect" href="https://www.googletagmanager.com" crossorigin /> <link rel="preconnect" href="https://www.googletagmanager.com" crossorigin />
{{ partial "google-analytics.html" . }} {{ partial "google-analytics.html" . }}
{{- end }} {{- end }}