Skip to content

Commit

Permalink
Add support for Google Analytics v4
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Atwood authored Mar 3, 2021
1 parent 782c79a commit ba16a14
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
3 changes: 3 additions & 0 deletions docs/content/en/about/hugo-and-gdpr.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ respectDoNotTrack
useSessionStorage
: Enabling this will disable the use of Cookies and use Session Storage to Store the GA Client ID.

{{% warning %}}
`useSessionStorage` is not supported when using Google Analytics v4 (gtag.js).
{{% /warning %}}
### Instagram

simple
Expand Down
12 changes: 10 additions & 2 deletions docs/content/en/templates/internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ While the following internal templates are called similar to partials, they do *

## Google Analytics

Hugo ships with internal templates for Google Analytics tracking, including both synchronous and asynchronous tracking codes.
Hugo ships with internal templates for Google Analytics tracking, including both synchronous and asynchronous tracking codes. As well as support for both v3 and v4 of Google Analytics.

### Configure Google Analytics

Provide your tracking id in your configuration file:

**Google Analytics v3 (analytics.js)**
{{< code-toggle file="config" >}}
googleAnalytics = "UA-123-45"
googleAnalytics = "UA-PROPERTY_ID"
{{</ code-toggle >}}

**Google Analytics v4 (gtag.js)**
{{< code-toggle file="config" >}}
googleAnalytics = "G-MEASUREMENT_ID"
{{</ code-toggle >}}

### Use the Google Analytics Template
Expand All @@ -50,6 +56,8 @@ You can then include the Google Analytics internal template:
{{ template "_internal/google_analytics_async.html" . }}
```

When using Google Analytics v4 use `_internal/google_analytics.html`.

A `.Site.GoogleAnalytics` variable is also exposed from the config.

## Disqus
Expand Down
2 changes: 1 addition & 1 deletion hugolib/embedded_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Disqus:
// Gheck GA regular and async
b.AssertFileContent("public/index.html",
"'anonymizeIp', true",
"'script','https://www.google-analytics.com/analytics.js','ga');\n\tga('create', 'ga_id', 'auto')",
"'script','https://www.google-analytics.com/analytics.js','ga');\n\tga('create', 'UA-ga_id', 'auto')",
"<script async src='https://www.google-analytics.com/analytics.js'>")

// Disqus
Expand Down
2 changes: 1 addition & 1 deletion hugolib/testhelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ const commonConfigSections = `
[services.disqus]
shortname = "disqus_shortname"
[services.googleAnalytics]
id = "ga_id"
id = "UA-ga_id"
[privacy]
[privacy.disqus]
Expand Down
18 changes: 15 additions & 3 deletions tpl/tplimpl/embedded/templates.autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions tpl/tplimpl/embedded/templates/google_analytics.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
{{- if not $pc.Disable -}}
{{ with .Site.GoogleAnalytics }}
{{- if not $pc.Disable }}{{ with .Site.GoogleAnalytics -}}
{{ if hasPrefix . "G-"}}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
<script>
{{ template "__ga_js_set_doNotTrack" $ }}
if (!doNotTrack) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ . }}', { 'anonymize_ip': {{- $pc.AnonymizeIP -}} });
}
</script>
{{ else if hasPrefix . "UA-" }}
<script type="application/javascript">
{{ template "__ga_js_set_doNotTrack" $ }}
if (!doNotTrack) {
Expand All @@ -26,8 +37,9 @@
ga('send', 'pageview');
}
</script>
{{ end }}
{{- end -}}
{{- end }}{{ end -}}

{{- define "__ga_js_set_doNotTrack" -}}{{/* This is also used in the async version. */}}
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
{{- if not $pc.RespectDoNotTrack -}}
Expand Down

0 comments on commit ba16a14

Please sign in to comment.