diff --git a/docs/content/en/about/hugo-and-gdpr.md b/docs/content/en/about/hugo-and-gdpr.md
index df0234a3b71..269c5d43255 100644
--- a/docs/content/en/about/hugo-and-gdpr.md
+++ b/docs/content/en/about/hugo-and-gdpr.md
@@ -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
diff --git a/docs/content/en/templates/internal.md b/docs/content/en/templates/internal.md
index e3d7e09c3be..13d951aa141 100644
--- a/docs/content/en/templates/internal.md
+++ b/docs/content/en/templates/internal.md
@@ -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
@@ -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
diff --git a/hugolib/embedded_templates_test.go b/hugolib/embedded_templates_test.go
index ab0b7b97fe0..c62d59b1de7 100644
--- a/hugolib/embedded_templates_test.go
+++ b/hugolib/embedded_templates_test.go
@@ -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')",
"
+
+{{ else if hasPrefix . "UA-" }}
-{{ 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 -}}
diff --git a/tpl/tplimpl/embedded/templates/google_analytics.html b/tpl/tplimpl/embedded/templates/google_analytics.html
index 97588113e39..f518b150c1a 100644
--- a/tpl/tplimpl/embedded/templates/google_analytics.html
+++ b/tpl/tplimpl/embedded/templates/google_analytics.html
@@ -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-"}}
+
+
+{{ else if hasPrefix . "UA-" }}
-{{ 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 -}}