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
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
```

View File

@ -7,7 +7,9 @@ enableGitInfo: true
# enableEmoji: false
hasCJKLanguage: true
# googleAnalytics: G-XXXXXXXXXX
# services:
# googleAnalytics:
# ID: G-MEASUREMENT_ID
outputs:
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 -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
<script>
@ -10,4 +16,4 @@
gtag("config", "{{ . }}");
</script>
{{ end }}
{{ end -}}

View File

@ -25,7 +25,7 @@
<!-- 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 />
{{ partial "google-analytics.html" . }}
{{- end }}