Open platform to collect and prioritize feedback
fix(analytics): migrate Google Analytics to GA4
Matt Roberts7d44a34a3d4f
diff --git a/app/cmd/server.go b/app/cmd/server.goindex eaec193df..4ccaa6b8a 100644--- a/app/cmd/server.go+++ b/app/cmd/server.go@@ -5,6 +5,7 @@ import ("os""os/signal""path"+ "strings""syscall""github.com/getfider/fider/app/jobs"@@ -45,6 +46,10 @@ func RunServer() int {})}+ if strings.HasPrefix(env.Config.GoogleAnalytics, "UA-") {+ log.Warn(ctx, "GOOGLE_ANALYTICS is set to a Universal Analytics ID, which Google no longer supports. Replace it with a Google Analytics 4 Measurement ID (G-XXXXXXXXXX).")+ }+copyEtcFiles(ctx)startJobs(ctx)diff --git a/app/middlewares/security_test.go b/app/middlewares/security_test.goindex c7d8ca54e..abd857fde 100644--- a/app/middlewares/security_test.go+++ b/app/middlewares/security_test.go@@ -24,7 +24,7 @@ func TestSecureWithoutCDN(t *testing.T) {return c.NoContent(http.StatusOK)})- expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'nonce-" + ctxID + "' https://www.google-analytics.com; img-src 'self' https: data:; font-src 'self' data:; object-src 'none'; media-src 'none'; connect-src 'self' https://www.google-analytics.com; frame-src 'self'"+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'nonce-" + ctxID + "' https://www.googletagmanager.com; img-src 'self' https: data:; font-src 'self' data:; object-src 'none'; media-src 'none'; connect-src 'self' https://www.googletagmanager.com https://*.google-analytics.com https://*.analytics.google.com; frame-src 'self'"Expect(status).Equals(http.StatusOK)Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)@@ -47,7 +47,7 @@ func TestSecureWithCDN(t *testing.T) {return c.NoContent(http.StatusOK)})- expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' *.test.fider.io; script-src 'self' 'nonce-" + ctxID + "' https://www.google-analytics.com *.test.fider.io; img-src 'self' https: data: *.test.fider.io; font-src 'self' data: *.test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' https://www.google-analytics.com *.test.fider.io; frame-src 'self'"+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' *.test.fider.io; script-src 'self' 'nonce-" + ctxID + "' https://www.googletagmanager.com *.test.fider.io; img-src 'self' https: data: *.test.fider.io; font-src 'self' data: *.test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' https://www.googletagmanager.com https://*.google-analytics.com https://*.analytics.google.com *.test.fider.io; frame-src 'self'"Expect(status).Equals(http.StatusOK)Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)@@ -70,7 +70,7 @@ func TestSecureWithCDN_SingleHost(t *testing.T) {return c.NoContent(http.StatusOK)})- expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' test.fider.io; script-src 'self' 'nonce-" + ctxID + "' https://www.google-analytics.com test.fider.io; img-src 'self' https: data: test.fider.io; font-src 'self' data: test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' https://www.google-analytics.com test.fider.io; frame-src 'self'"+ expectedPolicy := "base-uri 'self'; default-src 'self'; style-src 'self' 'unsafe-inline' test.fider.io; script-src 'self' 'nonce-" + ctxID + "' https://www.googletagmanager.com test.fider.io; img-src 'self' https: data: test.fider.io; font-src 'self' data: test.fider.io; object-src 'none'; media-src 'none'; connect-src 'self' https://www.googletagmanager.com https://*.google-analytics.com https://*.analytics.google.com test.fider.io; frame-src 'self'"Expect(status).Equals(http.StatusOK)Expect(response.Header().Get("Content-Security-Policy")).Equals(expectedPolicy)diff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.goindex 204a13791..7c5190a0c 100644--- a/app/pkg/web/engine.go+++ b/app/pkg/web/engine.go@@ -28,13 +28,13 @@ var (cspBase = "base-uri 'self'"cspDefault = "default-src 'self'"cspStyle = "style-src 'self' 'unsafe-inline'%[2]s"- cspScript = "script-src 'self' 'nonce-%[1]s' https://www.google-analytics.com%[2]s"+ cspScript = "script-src 'self' 'nonce-%[1]s' https://www.googletagmanager.com%[2]s"cspFont = "font-src 'self' data:%[2]s"cspImage = "img-src 'self' https: data:%[2]s"cspObject = "object-src 'none'"cspFrame = "frame-src 'self'"cspMedia = "media-src 'none'"- cspConnect = "connect-src 'self' https://www.google-analytics.com%[2]s"+ cspConnect = "connect-src 'self' https://www.googletagmanager.com https://*.google-analytics.com https://*.analytics.google.com%[2]s"//CspPolicyTemplate is the template used to generate the policyCspPolicyTemplate = fmt.Sprintf("%s; %s; %s; %s; %s; %s; %s; %s; %s; %s", cspBase, cspDefault, cspStyle, cspScript, cspImage, cspFont, cspObject, cspMedia, cspConnect, cspFrame)diff --git a/index.d.ts b/index.d.tsindex d9b4cd615..4101e6a0b 100644--- a/index.d.ts+++ b/index.d.ts@@ -19,7 +19,7 @@ interface PaddleSdk {}}declare interface Window {- ga?: (cmd: string, evt: string, args?: any) => void+ gtag?: (cmd: string, name: string, params?: Record<string, unknown>) => voidset: (key: string, value: any) => voidPaddle: PaddleSdk}diff --git a/public/services/analytics.ts b/public/services/analytics.tsindex 00f1d811c..b64dfc46c 100644--- a/public/services/analytics.ts+++ b/public/services/analytics.ts@@ -1,17 +1,17 @@export const analytics = {event: (eventCategory: string, eventAction: string): void => {- if (window.ga) {- window.ga("send", "event", {- eventCategory,- eventAction,+ if (window.gtag) {+ // GA4 event names may only contain letters, numbers and underscores+ window.gtag("event", `${eventCategory}_${eventAction}`.replace(/-/g, "_"), {+ event_category: eventCategory,})}},error: (err?: Error): void => {- if (window.ga) {- window.ga("send", "exception", {- exDescription: err ? err.stack : "<not available>",- exFatal: false,+ if (window.gtag) {+ window.gtag("event", "exception", {+ description: err ? err.stack : "<not available>",+ fatal: false,})}},diff --git a/views/base.html b/views/base.htmlindex 74f34859e..32d5c3d1f 100644--- a/views/base.html+++ b/views/base.html@@ -33,19 +33,14 @@</script>{{ if ne .public.settings.googleAnalytics "" }}+ <script async src="https://www.googletagmanager.com/gtag/js?id={{ .public.settings.googleAnalytics }}" nonce="{{ .public.contextID }}"></script><script type="text/javascript" nonce="{{ .public.contextID }}">- (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');-- ga('create', '{{ .public.settings.googleAnalytics }}', {- 'storage': 'none',- 'clientId': '{{ .public.sessionID }}'- });- ga('set', 'anonymizeIp', true);- {{ if .public.tenant }}ga('set', 'dimension1', '{{ .public.tenant.Subdomain }}');{{ end }}- ga('send', 'pageview');+ window.dataLayer = window.dataLayer || [];+ function gtag(){dataLayer.push(arguments);}+ gtag('js', new Date());+ gtag('config', '{{ .public.settings.googleAnalytics }}'{{ if .public.tenant }}, {+ 'tenant': '{{ .public.tenant.Subdomain }}'+ }{{ end }});</script>{{ end }}