-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: add several analytics sdk (#244)
* 🚧 wip: add analytics env * ✨ feat: add several analytics sdk * 📝 docs: update document
- Loading branch information
Showing
13 changed files
with
263 additions
and
15 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# 数据统计 | ||
|
||
为更好地帮助分析 LobeChat 的用户使用情况,我们在 LobeChat 中集成了若干免费 / 开源的数据统计服务,用于收集用户的使用情况,你可以按需开启。 | ||
|
||
## Vercel Analytics | ||
|
||
[Vercel Analytics](https://vercel.com/analytics) 是 Vercel 推出的一款数据分析服务,它可以帮助你收集网站的访问情况,包括访问量、访问来源、访问设备等等。 | ||
|
||
由于我们推荐使用 Vercel 一键部署 LobeChat,因此我们在代码中默认集成了 Vercel Analytics,你可以通过自行打开部署项目中 \[Insights] tab,查看你的应用访问情况。 | ||
|
||
Vercel Analytics 提供了 2500 次 / 月的免费 Web Analytics Events (可以理解为 PV),对于个人部署自用的产品来说基本够用。 | ||
|
||
如果你需要了解 Vercel Analytics 的详细使用教程,请查阅[Vercel Web Analytics 快速开始](https://vercel.com/docs/analytics/quickstart) | ||
|
||
如果你不需要 Vercel Analytics,你可以通过设置环境变量 `NEXT_PUBLIC_ANALYTICS_VERCEL=0` 来关闭它。 | ||
|
||
## 🚧 Posthog | ||
|
||
## 🚧 Mixpanel |
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,23 @@ | ||
'use client'; | ||
|
||
import mixpanel from 'mixpanel-browser'; | ||
import { memo, useEffect } from 'react'; | ||
|
||
import { getClientConfig } from '@/config/client'; | ||
|
||
const { MIXPANEL_PROJECT_TOKEN, MIXPANEL_DEBUG } = getClientConfig(); | ||
|
||
const MixpanelAnalytics = memo(() => { | ||
useEffect(() => { | ||
if (!MIXPANEL_PROJECT_TOKEN) return; | ||
|
||
mixpanel.init(MIXPANEL_PROJECT_TOKEN, { | ||
debug: MIXPANEL_DEBUG, | ||
persistence: 'localStorage', | ||
track_pageview: true, | ||
}); | ||
}, []); | ||
return null; | ||
}); | ||
|
||
export default MixpanelAnalytics; |
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,18 @@ | ||
'use client'; | ||
|
||
import Script from 'next/script'; | ||
import { memo } from 'react'; | ||
|
||
import { getClientConfig } from '@/config/client'; | ||
|
||
const { PLAUSIBLE_DOMAIN } = getClientConfig(); | ||
|
||
const PlausibleAnalytics = memo(() => { | ||
return ( | ||
PLAUSIBLE_DOMAIN && ( | ||
<Script data-domain={PLAUSIBLE_DOMAIN} defer src="https://plausible.io/js/script.js" /> | ||
) | ||
); | ||
}); | ||
|
||
export default PlausibleAnalytics; |
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,23 @@ | ||
'use client'; | ||
|
||
import posthog from 'posthog-js'; | ||
import { FC, memo, useEffect } from 'react'; | ||
|
||
import { getClientConfig } from '@/config/client'; | ||
|
||
const { POSTHOG_HOST, POSTHOG_KEY, POSTHOG_DEBUG } = getClientConfig(); | ||
|
||
const PostHog: FC = memo(() => { | ||
useEffect(() => { | ||
if (!POSTHOG_KEY) return; | ||
|
||
posthog.init(POSTHOG_KEY, { | ||
api_host: POSTHOG_HOST ?? 'https://app.posthog.com', | ||
debug: POSTHOG_DEBUG, | ||
}); | ||
}, []); | ||
|
||
return null; | ||
}); | ||
|
||
export default PostHog; |
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,12 @@ | ||
'use client'; | ||
|
||
import { Analytics } from '@vercel/analytics/react'; | ||
import { memo } from 'react'; | ||
|
||
import { getClientConfig } from '@/config/client'; | ||
|
||
const { VERCEL_DEBUG } = getClientConfig(); | ||
|
||
const VercelAnalytics = memo(() => <Analytics debug={VERCEL_DEBUG} />); | ||
|
||
export default VercelAnalytics; |
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,7 +1,24 @@ | ||
import { Analytics as VercelAnalytics } from '@vercel/analytics/react'; | ||
import dynamic from 'next/dynamic'; | ||
|
||
import { getClientConfig } from '@/config/client'; | ||
|
||
const Vercel = dynamic(() => import('./Vercel'), { ssr: false }); | ||
const Mixpanel = dynamic(() => import('./Mixpanel'), { ssr: false }); | ||
const Plausible = dynamic(() => import('./Plausible'), { ssr: false }); | ||
const Posthog = dynamic(() => import('./Posthog'), { ssr: false }); | ||
|
||
const { ANALYTICS_VERCEL, ANALYTICS_POSTHOG, ANALYTICS_MIXPANEL, ANALYTICS_PLAUSIBLE } = | ||
getClientConfig(); | ||
|
||
const Analytics = () => { | ||
return <VercelAnalytics />; | ||
return ( | ||
<> | ||
{ANALYTICS_VERCEL && <Vercel />} | ||
{ANALYTICS_PLAUSIBLE && <Plausible />} | ||
{ANALYTICS_MIXPANEL && <Mixpanel />} | ||
{ANALYTICS_POSTHOG && <Posthog />} | ||
</> | ||
); | ||
}; | ||
|
||
export default 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
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,46 @@ | ||
/* eslint-disable sort-keys-fix/sort-keys-fix , typescript-sort-keys/interface */ | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace NodeJS { | ||
interface ProcessEnv { | ||
AGENTS_INDEX_URL?: string; | ||
PLUGINS_INDEX_URL?: string; | ||
|
||
NEXT_PUBLIC_ANALYTICS_VERCEL?: string; | ||
NEXT_PUBLIC_VERCEL_DEBUG?: string; | ||
|
||
NEXT_PUBLIC_ANALYTICS_MIXPANEL?: string; | ||
NEXT_PUBLIC_MIXPANEL_PROJECT_TOKEN?: string; | ||
NEXT_PUBLIC_MIXPANEL_DEBUG?: string; | ||
|
||
NEXT_PUBLIC_ANALYTICS_PLAUSIBLE?: string; | ||
NEXT_PUBLIC_PLAUSIBLE_DOMAIN?: string; | ||
|
||
NEXT_PUBLIC_ANALYTICS_POSTHOG: string; | ||
NEXT_PUBLIC_POSTHOG_KEY: string; | ||
NEXT_PUBLIC_POSTHOG_HOST: string; | ||
NEXT_PUBLIC_POSTHOG_DEBUG: string; | ||
} | ||
} | ||
} | ||
|
||
export const getClientConfig = () => ({ | ||
AGENTS_INDEX_URL: process.env.AGENTS_INDEX_URL, | ||
PLUGINS_INDEX_URL: process.env.PLUGINS_INDEX_URL, | ||
|
||
ANALYTICS_VERCEL: process.env.NEXT_PUBLIC_ANALYTICS_VERCEL !== '0', | ||
VERCEL_DEBUG: process.env.NEXT_PUBLIC_VERCEL_DEBUG === '1', | ||
|
||
ANALYTICS_PLAUSIBLE: process.env.NEXT_PUBLIC_ANALYTICS_PLAUSIBLE === '1', | ||
PLAUSIBLE_DOMAIN: process.env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN, | ||
|
||
ANALYTICS_MIXPANEL: process.env.NEXT_PUBLIC_ANALYTICS_MIXPANEL === '1', | ||
MIXPANEL_PROJECT_TOKEN: process.env.NEXT_PUBLIC_MIXPANEL_PROJECT_TOKEN, | ||
MIXPANEL_DEBUG: process.env.NEXT_PUBLIC_MIXPANEL_DEBUG === '1', | ||
|
||
ANALYTICS_POSTHOG: process.env.NEXT_PUBLIC_ANALYTICS_POSTHOG === '1', | ||
POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY, | ||
POSTHOG_HOST: process.env.NEXT_PUBLIC_POSTHOG_HOST, | ||
POSTHOG_DEBUG: process.env.NEXT_PUBLIC_POSTHOG_DEBUG === '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
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