From 7c0f2c87d1d215cd9fab11d3950073d70e0c412b Mon Sep 17 00:00:00 2001 From: JovannMC Date: Fri, 17 Jan 2025 21:17:48 +0300 Subject: [PATCH] fix plausible stats i think --- src/routes/+layout.svelte | 13 ++++++++++++- src/routes/+layout.ts | 5 ----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index d6a28f1..e7d71ca 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -115,8 +115,19 @@ // Apply theme before DOM is loaded let theme = localStorage.getItem("theme"); if (theme !== "light" && theme !== "dark") { - const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; + const prefersDark = window.matchMedia( + "(prefers-color-scheme: dark)", + ).matches; theme = prefersDark ? "dark" : "light"; + + if (!theme) { + // first time visitor + window.addEventListener("load", () => { + window.plausible("Theme set", { + props: { theme: theme.dark ? "dark" : "light" }, + }); + }); + } } document.documentElement.classList.add(theme); diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts index 2210615..73e4c9c 100644 --- a/src/routes/+layout.ts +++ b/src/routes/+layout.ts @@ -1,5 +1,4 @@ import { browser } from "$app/environment"; -import { theme } from "$lib/store/index.svelte"; export const load = ({ data }) => { if (!browser) return; @@ -10,10 +9,6 @@ export const load = ({ data }) => { status: 200, }); }); - window.plausible("Theme set", { - props: { theme: theme.dark ? "dark" : "light" }, - }); - theme.dark = localStorage.getItem("theme") === "dark"; return data; };