Skip to content

Commit 73024f6

Browse files
Track user preferred theme
1 parent 836f5e3 commit 73024f6

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/components/Analytics/specific.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ export namespace AnalyticEvents {
3131
variationId,
3232
});
3333
}
34+
35+
export function setUserPreferredTheme(
36+
theme: "light" | "dark" | "auto light" | "auto dark",
37+
) {
38+
// https://ithoughthecamewithyou.com/post/user-scoped-custom-dimensions-in-google-analytics-4-using-gtag
39+
// console.log(`Setting user preferred theme to ${theme}`);
40+
window.gtag("set", "user_properties", { theme: theme });
41+
}
3442
}

src/components/Navbar/ThemePicker/themePicker.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import { AnalyticEvents } from "@/components/Analytics/specific";
23

34
export type Theme = "Light" | "Dark";
45
export type ThemeOptions = Theme | "Auto";
@@ -175,6 +176,16 @@ export function ThemeProxy({
175176
}),
176177
);
177178
window.localStorage.setItem("theme", dropdownTheme);
179+
180+
if (dropdownTheme === "Auto") {
181+
AnalyticEvents.setUserPreferredTheme(
182+
osWantsDark.matches ? "auto dark" : "auto light",
183+
);
184+
} else {
185+
AnalyticEvents.setUserPreferredTheme(
186+
dropdownTheme.toLowerCase() as "light" | "dark",
187+
);
188+
}
178189
}
179190
}, [dropdownTheme, loadedPreferredTheme]);
180191

0 commit comments

Comments
 (0)