-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 <[email protected]>
- Loading branch information
1 parent
d57b771
commit 7e64d39
Showing
17 changed files
with
558 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
"cSpell.words": [ | ||
"superjson", | ||
"homarr", | ||
"trpc" | ||
"trpc", | ||
"Umami" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <Script src="https://umami.homarr.dev/script.js" data-website-id={UMAMI_WEBSITE_ID} defer />; | ||
} | ||
|
||
return <></>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import SuperJSON from "superjson"; | ||
|
||
import { sendServerAnalyticsAsync } from "@homarr/analytics"; | ||
import { db, eq } from "@homarr/db"; | ||
import { serverSettings } from "@homarr/db/schema/sqlite"; | ||
|
||
import { EVERY_WEEK } from "~/lib/cron-job/constants"; | ||
import { createCronJob } from "~/lib/cron-job/creator"; | ||
import type { defaultServerSettings } from "../../../../packages/server-settings"; | ||
|
||
export const analyticsJob = createCronJob(EVERY_WEEK, { | ||
runOnStart: true, | ||
}).withCallback(async () => { | ||
const analyticSetting = await db.query.serverSettings.findFirst({ | ||
where: eq(serverSettings.settingKey, "analytics"), | ||
}); | ||
|
||
if (!analyticSetting) { | ||
return; | ||
} | ||
|
||
const value = SuperJSON.parse<(typeof defaultServerSettings)["analytics"]>(analyticSetting.value); | ||
|
||
if (!value.enableGeneral) { | ||
return; | ||
} | ||
|
||
await sendServerAnalyticsAsync(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./src"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "@homarr/analytics", | ||
"private": true, | ||
"version": "0.1.0", | ||
"type": "module", | ||
"exports": { | ||
".": "./index.ts" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"src/*" | ||
] | ||
} | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"clean": "rm -rf .turbo node_modules", | ||
"lint": "eslint .", | ||
"format": "prettier --check . --ignore-path ../../.gitignore", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"devDependencies": { | ||
"@homarr/db": "workspace:^0.1.0", | ||
"@homarr/eslint-config": "workspace:^0.2.0", | ||
"@homarr/log": "workspace:^0.1.0", | ||
"@homarr/prettier-config": "workspace:^0.1.0", | ||
"@homarr/server-settings": "workspace:^0.1.0", | ||
"@homarr/tsconfig": "workspace:^0.1.0", | ||
"eslint": "^8.57.0", | ||
"typescript": "^5.4.5" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"@homarr/eslint-config/base" | ||
] | ||
}, | ||
"prettier": "@homarr/prettier-config", | ||
"dependencies": { | ||
"@umami/node": "^0.3.0", | ||
"superjson": "2.2.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const UMAMI_HOST_URL = "https://umami.homarr.dev"; | ||
export const UMAMI_WEBSITE_ID = "ff7dc470-a84f-4779-b1ab-66a5bb16a94b"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./constants"; | ||
export * from "./send-server-analytics"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import type { UmamiEventData } from "@umami/node"; | ||
import { Umami } from "@umami/node"; | ||
import SuperJSON from "superjson"; | ||
|
||
import { count, db, eq } from "@homarr/db"; | ||
import { integrations, items, serverSettings, users } from "@homarr/db/schema/sqlite"; | ||
import { logger } from "@homarr/log"; | ||
import type { defaultServerSettings } from "@homarr/server-settings"; | ||
|
||
import { Stopwatch } from "../../common/src"; | ||
import { UMAMI_HOST_URL, UMAMI_WEBSITE_ID } from "./constants"; | ||
|
||
export const sendServerAnalyticsAsync = async () => { | ||
const stopWatch = new Stopwatch(); | ||
const setting = await db.query.serverSettings.findFirst({ | ||
where: eq(serverSettings.settingKey, "analytics"), | ||
}); | ||
|
||
if (!setting) { | ||
logger.info( | ||
"Server does not know the configured state of analytics. No data will be sent. Enable analytics in the settings", | ||
); | ||
return; | ||
} | ||
|
||
const analyticsSettings = SuperJSON.parse<typeof defaultServerSettings.analytics>(setting.value); | ||
|
||
if (!analyticsSettings.enableGeneral) { | ||
logger.info("Analytics are disabled. No data will be sent. Enable analytics in the settings"); | ||
return; | ||
} | ||
|
||
const umamiInstance = new Umami(); | ||
umamiInstance.init({ | ||
hostUrl: UMAMI_HOST_URL, | ||
websiteId: UMAMI_WEBSITE_ID, | ||
}); | ||
|
||
await sendIntegrationDataAsync(umamiInstance, analyticsSettings); | ||
await sendWidgetDataAsync(umamiInstance, analyticsSettings); | ||
await sendUserDataAsync(umamiInstance, analyticsSettings); | ||
|
||
logger.info(`Sent all analytics in ${stopWatch.getElapsedInHumanWords()}`); | ||
}; | ||
|
||
const sendWidgetDataAsync = async (umamiInstance: Umami, analyticsSettings: typeof defaultServerSettings.analytics) => { | ||
if (!analyticsSettings.enableWidgetData) { | ||
return; | ||
} | ||
const widgetCount = (await db.select({ count: count(items.id) }).from(items))[0]?.count ?? 0; | ||
|
||
const response = await umamiInstance.track("server-widget-data", { | ||
countWidgets: widgetCount, | ||
}); | ||
if (response.ok) { | ||
return; | ||
} | ||
|
||
logger.warn("Unable to send track event data to Umami instance"); | ||
}; | ||
|
||
const sendUserDataAsync = async (umamiInstance: Umami, analyticsSettings: typeof defaultServerSettings.analytics) => { | ||
if (!analyticsSettings.enableUserData) { | ||
return; | ||
} | ||
const userCount = (await db.select({ count: count(users.id) }).from(users))[0]?.count ?? 0; | ||
|
||
const response = await umamiInstance.track("server-user-data", { | ||
countUsers: userCount, | ||
}); | ||
if (response.ok) { | ||
return; | ||
} | ||
|
||
logger.warn("Unable to send track event data to Umami instance"); | ||
}; | ||
|
||
const sendIntegrationDataAsync = async ( | ||
umamiInstance: Umami, | ||
analyticsSettings: typeof defaultServerSettings.analytics, | ||
) => { | ||
if (!analyticsSettings.enableIntegrationData) { | ||
return; | ||
} | ||
const integrationKinds = await db | ||
.select({ kind: integrations.kind, count: count(integrations.id) }) | ||
.from(integrations) | ||
.groupBy(integrations.kind); | ||
|
||
const map: UmamiEventData = {}; | ||
|
||
integrationKinds.forEach((integrationKind) => { | ||
map[integrationKind.kind] = integrationKind.count; | ||
}); | ||
|
||
const response = await umamiInstance.track("server-integration-data-kind", map); | ||
if (response.ok) { | ||
return; | ||
} | ||
|
||
logger.warn("Unable to send track event data to Umami instance"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "@homarr/tsconfig/base.json", | ||
"compilerOptions": { | ||
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json" | ||
}, | ||
"include": ["*.ts", "src"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.