Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Universal Analytics and Google Analytics 4 IDs simultaneously #1106

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions layouts/_internal/google_analytics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/* Override of https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/google_analytics.html
At commit https://github.com/gohugoio/hugo/commit/ba16a14c6e884e309380610331aff78213f84751 */ -}}
{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}}
{{- if not $pc.Disable }}{{ with .Site.GoogleAnalytics -}}
{{ $ga_ids := split . "," -}}
{{ if (or (hasPrefix . "G-") (gt (len $ga_ids) 1)) -}}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ index $ga_ids 0 }}"></script>
<script>
{{ template "__ga_js_set_doNotTrack" $ }}
if (!doNotTrack) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
{{ range $ga_ids }}
{{ with trim . " " -}}
gtag('config', '{{ . }}'
{{- if and $pc.AnonymizeIP (hasPrefix . "UA-") -}}
, { 'anonymize_ip': {{- $pc.AnonymizeIP -}} }
{{- end -}}
);
{{- end }}
{{- end }}
}
</script>
{{ else if hasPrefix . "UA-" }}
<script type="application/javascript">
{{ template "__ga_js_set_doNotTrack" $ }}
if (!doNotTrack) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
{{- if $pc.UseSessionStorage }}
if (window.sessionStorage) {
var GA_SESSION_STORAGE_KEY = 'ga:clientId';
ga('create', '{{ . }}', {
'storage': 'none',
'clientId': sessionStorage.getItem(GA_SESSION_STORAGE_KEY)
});
ga(function(tracker) {
sessionStorage.setItem(GA_SESSION_STORAGE_KEY, tracker.get('clientId'));
});
}
{{ else }}
ga('create', '{{ . }}', 'auto');
{{ end -}}
{{ if $pc.AnonymizeIP }}ga('set', 'anonymizeIp', true);{{ end }}
ga('send', 'pageview');
}
</script>
{{- 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 -}}
var doNotTrack = false;
{{- else -}}
var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
var doNotTrack = (dnt == "1" || dnt == "yes");
{{- end -}}
{{- end -}}
3 changes: 2 additions & 1 deletion layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@

{{/* To comply with GDPR, cookie consent scripts places in head-end must execute before Google Analytics is enabled */ -}}
{{ if hugo.IsProduction -}}
{{ if hasPrefix .Site.GoogleAnalytics "G-" -}}
{{ if (or (hasPrefix .Site.GoogleAnalytics "G-")
(in .Site.GoogleAnalytics ",")) -}}
{{ template "_internal/google_analytics.html" . -}}
{{ else -}}
{{ template "_internal/google_analytics_async.html" . -}}
Expand Down