LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
UN-3543 [FIX] Use per-user distinct_id and deployment tagging for PostHog (#2049)
Chandrasekharan M310ff8d5a078
diff --git a/frontend/generate-runtime-config.sh b/frontend/generate-runtime-config.shindex d79fa3c00..12d5f1442 100755--- a/frontend/generate-runtime-config.sh+++ b/frontend/generate-runtime-config.sh@@ -9,11 +9,12 @@ cat > /usr/share/nginx/html/config/runtime-config.js << EOF// This file is auto-generated at runtime. Do not modify manually.window.RUNTIME_CONFIG = {faviconPath: "${VITE_FAVICON_PATH:-${REACT_APP_FAVICON_PATH}}",- logoUrl: "${VITE_CUSTOM_LOGO_URL:-${REACT_APP_CUSTOM_LOGO_URL}}"+ logoUrl: "${VITE_CUSTOM_LOGO_URL:-${REACT_APP_CUSTOM_LOGO_URL}}",+ enablePosthog: "${VITE_ENABLE_POSTHOG:-${REACT_APP_ENABLE_POSTHOG}}"};EOF# Make sure nginx can read the filechmod 755 /usr/share/nginx/html/config/runtime-config.js-echo "Runtime configuration generated successfully with logo URL: ${VITE_CUSTOM_LOGO_URL:-${REACT_APP_CUSTOM_LOGO_URL:-<not set>}}"+echo "Runtime configuration generated successfully with logo URL: ${VITE_CUSTOM_LOGO_URL:-${REACT_APP_CUSTOM_LOGO_URL:-<not set>}}, enablePosthog: ${VITE_ENABLE_POSTHOG:-${REACT_APP_ENABLE_POSTHOG:-<not set>}}"diff --git a/frontend/src/helpers/PostHogDeployment.js b/frontend/src/helpers/PostHogDeployment.jsnew file mode 100644index 000000000..ab950193a--- /dev/null+++ b/frontend/src/helpers/PostHogDeployment.js@@ -0,0 +1,15 @@+// All deployments report to a single PostHog project; tag events with+// their origin so they can be segmented. Non-prod envs are expected to+// disable PostHog via VITE_ENABLE_POSTHOG instead of being mapped here.+const DEPLOYMENT_BY_HOST = {+ "us-central.unstract.com": "us-prod",+ "eu-west.unstract.com": "eu-prod",+};++const getDeployment = () =>+ DEPLOYMENT_BY_HOST[window?.location?.hostname] || "self-hosted";++// PII (email, name) may only leave Unstract-managed SaaS deployments+const isSaasProdDeployment = () => getDeployment() !== "self-hosted";++export { getDeployment, isSaasProdDeployment };diff --git a/frontend/src/hooks/usePostHogEvents.js b/frontend/src/hooks/usePostHogEvents.jsindex 6eae45b00..cd06afb64 100644--- a/frontend/src/hooks/usePostHogEvents.js+++ b/frontend/src/hooks/usePostHogEvents.js@@ -1,5 +1,6 @@import { usePostHog } from "posthog-js/react";+import { isSaasProdDeployment } from "../helpers/PostHogDeployment";import { useSessionStore } from "../store/session-store";const usePostHogEvents = () => {@@ -57,27 +58,50 @@ const usePostHogEvents = () => {};const setPostHogIdentity = () => {- const distinctId = `${sessionDetails?.orgId}:${sessionDetails?.orgName}`;- const orgName = sessionDetails?.orgName;- const isOpenSource = orgName === "mock_org";-- const name = `${sessionDetails?.name} ${sessionDetails?.family_name || ""}`;- const optionalParams = {- name,- org: orgName,- software: isOpenSource ? "OSS" : "SAAS",- };-- const email = sessionDetails?.email;- const userId = sessionDetails?.id;-- if (email) {- optionalParams["email"] = email;- } else {- optionalParams["userId"] = userId;+ try {+ const orgId = sessionDetails?.orgId;+ const orgName = sessionDetails?.orgName;+ const isOpenSource = orgName === "mock_org";+ const software = isOpenSource ? "OSS" : "SAAS";+ const email = sessionDetails?.email;+ const userId = sessionDetails?.id;++ // PII (email, name) may only leave Unstract-managed SaaS; self-hosted+ // and OSS installs identify with an anonymous org-scoped id instead.+ // Email as distinct_id since bare user ids collide across+ // regions/self-hosts — every deployment reports to one PostHog project+ const canSendPii = isSaasProdDeployment() && !isOpenSource;+ const fallbackId = orgId ? `${orgId}:${userId}` : userId?.toString();+ const distinctId = canSendPii && email ? email : fallbackId;++ const personProperties = {+ org: orgName,+ software,+ };++ if (canSendPii) {+ personProperties.name =+ `${sessionDetails?.name} ${sessionDetails?.family_name || ""}`.trim();+ if (email) {+ personProperties.email = email;+ }+ }+ if (userId) {+ personProperties.userId = userId;+ }++ posthog.identify(distinctId, personProperties);++ // Super-properties ride on every captured event, enabling org-level+ // breakdowns without the group analytics add-on+ posthog.register({+ org_id: orgId,+ org_name: orgName,+ software,+ });+ } catch (error) {+ console.error("PostHog identify failed:", error);}-- posthog.identify(distinctId, optionalParams);};const setPostHogCustomEvent = (eventName, properties) => {diff --git a/frontend/src/index.jsx b/frontend/src/index.jsxindex 9a5a3edba..66079513d 100644--- a/frontend/src/index.jsx+++ b/frontend/src/index.jsx@@ -6,21 +6,38 @@ import ReactDOM from "react-dom/client";import { GenericLoader } from "./components/generic-loader/GenericLoader";import { LazyLoader } from "./components/widgets/lazy-loader/LazyLoader.jsx";import config from "./config.js";+import { getDeployment } from "./helpers/PostHogDeployment.js";import { SocketProvider } from "./helpers/SocketContext.js";import "./index.css";-const enablePosthog = import.meta.env.VITE_ENABLE_POSTHOG;+// Runtime config (containerized deployments) wins when it carries a+// non-empty value; the entrypoint emits "" for unset env vars, which+// deliberately falls through to the build-time env+const runtimeConfig =+ typeof window !== "undefined" ? window.RUNTIME_CONFIG || {} : {};+const enablePosthog =+ runtimeConfig.enablePosthog || import.meta.env.VITE_ENABLE_POSTHOG;if (enablePosthog !== "false") {- // Define the PostHog API key and host URL- const API_KEY = "phc_PTafesyRuRB5hceRILaNPeyu2IDuzPshyjIPYGvgoBd"; // gitleaks:allow- const API_HOST = "https://eu.i.posthog.com/";+ // Analytics failures (blocked storage, ad-blockers, CSP) must never+ // abort module evaluation and block app bootstrap+ try {+ // Define the PostHog API key and host URL+ const API_KEY = "phc_PTafesyRuRB5hceRILaNPeyu2IDuzPshyjIPYGvgoBd"; // gitleaks:allow+ const API_HOST = "https://eu.i.posthog.com/";- // Initialize PostHog with the specified API key and host- posthog.init(API_KEY, {- api_host: API_HOST,- capture_pageview: false,- autocapture: false,- });+ // Initialize PostHog with the specified API key and host+ posthog.init(API_KEY, {+ api_host: API_HOST,+ capture_pageview: false,+ autocapture: false,+ // Pre-login events stay anonymous (billed cheaper, no person created)+ person_profiles: "identified_only",+ respect_dnt: true,+ });+ posthog.register({ deployment: getDeployment() });+ } catch (error) {+ console.error("PostHog initialization failed:", error);+ }}// Utility to set favicon