From 7e64d39693438cc52f37c8aab4fb9b04c851e4b9 Mon Sep 17 00:00:00 2001 From: Manuel <30572287+manuel-rw@users.noreply.github.com> Date: Sun, 26 May 2024 17:18:25 +0200 Subject: [PATCH] feat: add analytics (#528) * feat: add analytics * feat: send data to umami * refactor: fix format * fix: click behavior of switches * refactor: format * chore: rerun ci --------- Co-authored-by: Meier Lukas --- .vscode/settings.json | 3 +- README.md | 2 + apps/nextjs/package.json | 1 + apps/nextjs/src/app/[locale]/layout.tsx | 2 + .../_components/analytics.settings.tsx | 17 +- .../src/components/layout/analytics.tsx | 14 + apps/tasks/package.json | 1 + apps/tasks/src/jobs.ts | 2 + apps/tasks/src/jobs/analytics.ts | 29 ++ packages/analytics/index.ts | 1 + packages/analytics/package.json | 43 ++ packages/analytics/src/constants.ts | 2 + packages/analytics/src/index.ts | 2 + .../analytics/src/send-server-analytics.ts | 102 ++++ packages/analytics/tsconfig.json | 8 + packages/api/src/router/serverSettings.ts | 25 +- pnpm-lock.yaml | 483 ++++++++++++------ 17 files changed, 558 insertions(+), 179 deletions(-) create mode 100644 apps/nextjs/src/components/layout/analytics.tsx create mode 100644 apps/tasks/src/jobs/analytics.ts create mode 100644 packages/analytics/index.ts create mode 100644 packages/analytics/package.json create mode 100644 packages/analytics/src/constants.ts create mode 100644 packages/analytics/src/index.ts create mode 100644 packages/analytics/src/send-server-analytics.ts create mode 100644 packages/analytics/tsconfig.json diff --git a/.vscode/settings.json b/.vscode/settings.json index 53c760db0..b9a963aad 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,7 @@ "cSpell.words": [ "superjson", "homarr", - "trpc" + "trpc", + "Umami" ] } \ No newline at end of file diff --git a/README.md b/README.md index 2eb9f8222..3a88af411 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # THIS PROJECT IS STILL UNSTABLE AND WE DO NOT PROVIDE ANY SUPPORT FOR ISSUES THAT OCCURE. + ## PLEASE DO NOT OPEN ANY ISSUES OR DISCUSSIONS + ### EVERYTHING IS SUBJECT TO CHANGE Please use [this](https://github.com/ajnart/homarr) version of Homarr when you want to use it diff --git a/apps/nextjs/package.json b/apps/nextjs/package.json index 529e259ca..cb3f9b643 100644 --- a/apps/nextjs/package.json +++ b/apps/nextjs/package.json @@ -14,6 +14,7 @@ "with-env": "dotenv -e ../../.env --" }, "dependencies": { + "@homarr/analytics": "workspace:^0.1.0", "@homarr/api": "workspace:^0.1.0", "@homarr/auth": "workspace:^0.1.0", "@homarr/common": "workspace:^0.1.0", diff --git a/apps/nextjs/src/app/[locale]/layout.tsx b/apps/nextjs/src/app/[locale]/layout.tsx index 35414e252..86db1c66c 100644 --- a/apps/nextjs/src/app/[locale]/layout.tsx +++ b/apps/nextjs/src/app/[locale]/layout.tsx @@ -11,6 +11,7 @@ import { auth } from "@homarr/auth/next"; import { ModalProvider } from "@homarr/modals"; import { Notifications } from "@homarr/notifications"; +import { Analytics } from "~/components/layout/analytics"; import { JotaiProvider } from "./_client-providers/jotai"; import { NextInternationalProvider } from "./_client-providers/next-international"; import { AuthProvider } from "./_client-providers/session"; @@ -76,6 +77,7 @@ export default function Layout(props: { children: React.ReactNode; params: { loc + diff --git a/apps/nextjs/src/app/[locale]/manage/settings/_components/analytics.settings.tsx b/apps/nextjs/src/app/[locale]/manage/settings/_components/analytics.settings.tsx index dfe86b363..7ac93b917 100644 --- a/apps/nextjs/src/app/[locale]/manage/settings/_components/analytics.settings.tsx +++ b/apps/nextjs/src/app/[locale]/manage/settings/_components/analytics.settings.tsx @@ -96,18 +96,23 @@ const SwitchSetting = ({ title: string; text: ReactNode; }) => { + const disabled = formKey !== "enableGeneral" && !form.values.enableGeneral; const handleClick = React.useCallback(() => { + if (disabled) { + return; + } form.setFieldValue(formKey, !form.values[formKey]); - }, [form, formKey]); + }, [form, formKey, disabled]); + return ( - - + + {title} {text} - - - + + + ); }; diff --git a/apps/nextjs/src/components/layout/analytics.tsx b/apps/nextjs/src/components/layout/analytics.tsx new file mode 100644 index 000000000..76e76e2d2 --- /dev/null +++ b/apps/nextjs/src/components/layout/analytics.tsx @@ -0,0 +1,14 @@ +import Script from "next/script"; + +import { UMAMI_WEBSITE_ID } from "@homarr/analytics"; +import { api } from "@homarr/api/server"; + +export const Analytics = async () => { + const analytics = await api.serverSettings.getAnalytics(); + + if (analytics.enableGeneral) { + return