Skip to content

Commit abb70f7

Browse files
chore: add google analytics
1 parent 50cb817 commit abb70f7

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

hugo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ weight = 30
3838
[module.hugoVersion]
3939
extended = false
4040
min = "0.116.0"
41+
42+
[services]
43+
[services.googleAnalytics]
44+
id = 'G-29B1VSEP30'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{ if not site.Config.Privacy.GoogleAnalytics.Disable }}
2+
{{- with site.Config.Services.GoogleAnalytics.ID }}
3+
{{- if strings.HasPrefix (lower .) "ua-" }}
4+
{{- warnf "Google Analytics 4 (GA4) replaced Google Universal Analytics (UA) effective 1 July 2023. See https://support.google.com/analytics/answer/11583528. Create a GA4 property and data stream, then replace the Google Analytics ID in your site configuration with the new value." }}
5+
{{- else }}
6+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
7+
<script>
8+
var doNotTrack = false;
9+
if ({{ site.Config.Privacy.GoogleAnalytics.RespectDoNotTrack }}) {
10+
var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
11+
var doNotTrack = (dnt == "1" || dnt == "yes");
12+
}
13+
if (!doNotTrack) {
14+
window.dataLayer = window.dataLayer || [];
15+
function gtag(){dataLayer.push(arguments);}
16+
gtag('js', new Date());
17+
gtag('config', '{{ . }}');
18+
}
19+
</script>
20+
{{- end }}
21+
{{- end }}
22+
{{- end -}}

layouts/partials/head.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
<title>{{ if .IsHome }}{{ site.Title }}{{ else }}{{ printf "%s | %s" .Title site.Title }}{{ end }}</title>
44
{{ partialCached "head/css.html" . }}
55
{{ partialCached "head/js.html" . }}
6+
{{ partial "google_analytics.html" . }}

0 commit comments

Comments
 (0)