Skip to content

Commit

Permalink
chore: support services.googleAnalytics.ID (#173)
Browse files Browse the repository at this point in the history
* chore: support `services.googleAnalytics.ID`

and deprecate `site.googleAnalytics`

* chore: remove warning
  • Loading branch information
imfing authored Oct 30, 2023
1 parent de1286c commit e3f6069
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions exampleSite/content/docs/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
4 changes: 3 additions & 1 deletion exampleSite/hugo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ enableGitInfo: true
# enableEmoji: false
hasCJKLanguage: true

# googleAnalytics: G-XXXXXXXXXX
# services:
# googleAnalytics:
# ID: G-MEASUREMENT_ID

outputs:
home: [HTML]
Expand Down
10 changes: 8 additions & 2 deletions layouts/partials/google-analytics.html
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -10,4 +16,4 @@

gtag("config", "{{ . }}");
</script>
{{ end }}
{{ end -}}
2 changes: 1 addition & 1 deletion layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down

0 comments on commit e3f6069

Please sign in to comment.