Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

feat: add Amplitude API key to Docker entrypoint script (#29477)

Coding On Star7344adf65e03
diff --git a/docker/docker-compose-template.yaml b/docker/docker-compose-template.yaml
index b3d5cca2455..c89224fa8a2 100644
--- a/docker/docker-compose-template.yaml
+++ b/docker/docker-compose-template.yaml
@@ -136,6 +136,7 @@ services:
environment:
CONSOLE_API_URL: ${CONSOLE_API_URL:-}
APP_API_URL: ${APP_API_URL:-}
+ AMPLITUDE_API_KEY: ${AMPLITUDE_API_KEY:-}
NEXT_PUBLIC_COOKIE_DOMAIN: ${NEXT_PUBLIC_COOKIE_DOMAIN:-}
SENTRY_DSN: ${WEB_SENTRY_DSN:-}
NEXT_TELEMETRY_DISABLED: ${NEXT_TELEMETRY_DISABLED:-0}
diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml
index b961f6b216d..160dbbec464 100644
--- a/docker/docker-compose.yaml
+++ b/docker/docker-compose.yaml
@@ -773,6 +773,7 @@ services:
environment:
CONSOLE_API_URL: ${CONSOLE_API_URL:-}
APP_API_URL: ${APP_API_URL:-}
+ AMPLITUDE_API_KEY: ${AMPLITUDE_API_KEY:-}
NEXT_PUBLIC_COOKIE_DOMAIN: ${NEXT_PUBLIC_COOKIE_DOMAIN:-}
SENTRY_DSN: ${WEB_SENTRY_DSN:-}
NEXT_TELEMETRY_DISABLED: ${NEXT_TELEMETRY_DISABLED:-0}
diff --git a/web/.env.example b/web/.env.example
index eff6f77fd9c..34ea7de5224 100644
--- a/web/.env.example
+++ b/web/.env.example
@@ -70,3 +70,6 @@ NEXT_PUBLIC_ENABLE_SINGLE_DOLLAR_LATEX=false
# The maximum number of tree node depth for workflow
NEXT_PUBLIC_MAX_TREE_DEPTH=50
+
+# The api key of amplitude
+NEXT_PUBLIC_AMPLITUDE_API_KEY=
diff --git a/web/app/components/base/amplitude/AmplitudeProvider.tsx b/web/app/components/base/amplitude/AmplitudeProvider.tsx
index 6f2f43b6142..c242326c309 100644
--- a/web/app/components/base/amplitude/AmplitudeProvider.tsx
+++ b/web/app/components/base/amplitude/AmplitudeProvider.tsx
@@ -11,13 +11,19 @@ export type IAmplitudeProps = {
sessionReplaySampleRate?: number
}
+// Check if Amplitude should be enabled
+export const isAmplitudeEnabled = () => {
+ const apiKey = process.env.NEXT_PUBLIC_AMPLITUDE_API_KEY
+ return IS_CLOUD_EDITION && !!apiKey
+}
+
const AmplitudeProvider: FC<IAmplitudeProps> = ({
apiKey = process.env.NEXT_PUBLIC_AMPLITUDE_API_KEY ?? '',
sessionReplaySampleRate = 1,
}) => {
useEffect(() => {
- // Only enable in Saas edition
- if (!IS_CLOUD_EDITION)
+ // Only enable in Saas edition with valid API key
+ if (!isAmplitudeEnabled())
return
// Initialize Amplitude
diff --git a/web/app/components/base/amplitude/index.ts b/web/app/components/base/amplitude/index.ts
index e447a0c5e34..acc792339ef 100644
--- a/web/app/components/base/amplitude/index.ts
+++ b/web/app/components/base/amplitude/index.ts
@@ -1,2 +1,2 @@
-export { default } from './AmplitudeProvider'
+export { default, isAmplitudeEnabled } from './AmplitudeProvider'
export { resetUser, setUserId, setUserProperties, trackEvent } from './utils'
diff --git a/web/app/components/base/amplitude/utils.ts b/web/app/components/base/amplitude/utils.ts
index 8423c43bb24..57b96243ec1 100644
--- a/web/app/components/base/amplitude/utils.ts
+++ b/web/app/components/base/amplitude/utils.ts
@@ -1,4 +1,5 @@
import * as amplitude from '@amplitude/analytics-browser'
+import { isAmplitudeEnabled } from './AmplitudeProvider'
/**
* Track custom event
@@ -6,6 +7,8 @@ import * as amplitude from '@amplitude/analytics-browser'
* @param eventProperties Event properties (optional)
*/
export const trackEvent = (eventName: string, eventProperties?: Record<string, any>) => {
+ if (!isAmplitudeEnabled())
+ return
amplitude.track(eventName, eventProperties)
}
@@ -14,6 +17,8 @@ export const trackEvent = (eventName: string, eventProperties?: Record<string, a
* @param userId User ID
*/
export const setUserId = (userId: string) => {
+ if (!isAmplitudeEnabled())
+ return
amplitude.setUserId(userId)
}
@@ -22,6 +27,8 @@ export const setUserId = (userId: string) => {
* @param properties User properties
*/
export const setUserProperties = (properties: Record<string, any>) => {
+ if (!isAmplitudeEnabled())
+ return
const identifyEvent = new amplitude.Identify()
Object.entries(properties).forEach(([key, value]) => {
identifyEvent.set(key, value)
@@ -33,5 +40,7 @@ export const setUserProperties = (properties: Record<string, any>) => {
* Reset user (e.g., when user logs out)
*/
export const resetUser = () => {
+ if (!isAmplitudeEnabled())
+ return
amplitude.reset()
}
diff --git a/web/docker/entrypoint.sh b/web/docker/entrypoint.sh
index 3325690239e..565c9066241 100755
--- a/web/docker/entrypoint.sh
+++ b/web/docker/entrypoint.sh
@@ -25,6 +25,8 @@ export NEXT_PUBLIC_SENTRY_DSN=${SENTRY_DSN}
export NEXT_PUBLIC_SITE_ABOUT=${SITE_ABOUT}
export NEXT_TELEMETRY_DISABLED=${NEXT_TELEMETRY_DISABLED}
+export NEXT_PUBLIC_AMPLITUDE_API_KEY=${AMPLITUDE_API_KEY}
+
export NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS=${TEXT_GENERATION_TIMEOUT_MS}
export NEXT_PUBLIC_CSP_WHITELIST=${CSP_WHITELIST}
export NEXT_PUBLIC_ALLOW_EMBED=${ALLOW_EMBED}