AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { isNil, tryCatchSync } from '@activepieces/core-utils';
import {
ApEdition,
ApEnvironment,
ApFlagId,
DeploymentKind,
isCloudOnlyTelemetryEvent,
pickTelemetryPii,
TelemetryEvent,
} from '@activepieces/shared';
import posthog from 'posthog-js';
import React, { useEffect, useRef } from 'react';
import { useDeepCompareEffect } from 'react-use';
import { useEmbedding } from '@/components/providers/embed-provider';
import { flagsHooks } from '@/hooks/flags-hooks';
import { platformConfigurationHooks } from '@/hooks/platform-configuration-hooks';
import { userHooks } from '@/hooks/user-hooks';
import { CLOUD_HOSTNAME, isRunningCloudInDevMode } from '@/lib/api';
import { errorReporting } from '@/lib/error-reporting';
interface TelemetryProviderProps {
children: React.ReactNode;
}
const TelemetryProvider = ({ children }: TelemetryProviderProps) => {
const { data: currentUser } = userHooks.useCurrentUser();
const identifiedKey = useRef<string | null>(null);
const { data: configuration } =
platformConfigurationHooks.useCurrentPlatformConfiguration();
const { data: flagCurrentVersion } = flagsHooks.useFlag<string>(
ApFlagId.CURRENT_VERSION,
);
const { data: flagEnvironment } = flagsHooks.useFlag<ApEnvironment>(
ApFlagId.ENVIRONMENT,
);
const { data: edition } = flagsHooks.useFlag<ApEdition>(ApFlagId.EDITION);
const { embedState } = useEmbedding();
const isCloud = edition === ApEdition.CLOUD;
const isDevEnvironment = flagEnvironment === ApEnvironment.DEVELOPMENT;
const isPreLoginCloudFunnel =
isNil(currentUser) &&
isCloud &&
window.location.hostname === CLOUD_HOSTNAME;
const isSignedInWithAnalyticsOn =
!isNil(currentUser) &&
(isCloud || configuration?.isProductTelemetryEnabled === true);
const devTelemetryAllowed =
isPosthogDevOptIn() || (!isRunningCloudInDevMode && !isDevEnvironment);
const telemetryEnabled =
(isPreLoginCloudFunnel || isSignedInWithAnalyticsOn) && devTelemetryAllowed;
const deployment = resolveDeploymentKind({
edition,
environment: flagEnvironment,
});
const posthogInitialized = useRef(false);
useEffect(() => {
if (posthogInitialized.current) {
return;
}
const isEmbedded =
embedState.isEmbedded || window.location.pathname.startsWith('/embed');
if (!telemetryEnabled || isEmbedded || isNil(edition)) {
return;
}
posthogInitialized.current = true;
posthog.init('phc_7F92HoXJPeGnTKmYv0eOw62FurPMRW9Aqr0TPrDzvHh', {
// Same-origin reverse proxy (/ingest) so ad blockers don't drop ingestion.
api_host: '/ingest',
ui_host: 'https://us.posthog.com',
// Adopt the identity cookie the marketing site sets on `.activepieces.com`.
cross_subdomain_cookie: true,
// Limit autocapture to the auth funnel; the rest of the app doesn't need it.
autocapture: isCloud
? {
url_allowlist: [
/\/sign-up/,
/\/sign-in/,
/\/verify-email/,
/\/forget-password/,
/\/reset-password/,
/\/invitation/,
/\/authenticate/,
],
}
: false,
capture_pageview: 'history_change',
capture_pageleave: true,
capture_dead_clicks: isCloud,
rageclick: isCloud,
enable_heatmaps: isCloud,
person_profiles: 'identified_only',
persistence: 'localStorage+cookie',
disable_session_recording: true,
enable_recording_console_log: false,
session_recording: {
maskAllInputs: true,
},
});
// Tag events so the shared project separates product from marketing traffic.
posthog.register({ source_site: 'product', deployment });
if (isCloud && isInRecordingSample(posthog.get_distinct_id())) {
posthog.startSessionRecording();
}
}, [telemetryEnabled, embedState.isEmbedded, edition, isCloud, deployment]);
useEffect(() => {
if (!posthogInitialized.current) {
return;
}
posthog.register({
activepiecesEdition: edition ?? ApEdition.COMMUNITY,
activepiecesVersion: flagCurrentVersion ?? UNKNOWN_FLAG_VALUE,
activepiecesEnvironment: flagEnvironment ?? UNKNOWN_FLAG_VALUE,
deployment,
});
}, [
telemetryEnabled,
edition,
embedState.isEmbedded,
flagCurrentVersion,
flagEnvironment,
deployment,
]);
useEffect(() => {
errorReporting.init();
errorReporting.flushBuffered();
}, []);
useDeepCompareEffect(() => {
if (isNil(currentUser) || !telemetryEnabled) {
return;
}
const identityKey = `${currentUser.id}:${edition ?? ''}`;
if (identityKey === identifiedKey.current) {
return;
}
identifiedKey.current = identityKey;
initTelemetry();
}, [telemetryEnabled, currentUser, edition]);
const initTelemetry = () => {
if (isNil(currentUser)) {
return;
}
const currentVersion = flagCurrentVersion || UNKNOWN_FLAG_VALUE;
const environment = flagEnvironment || UNKNOWN_FLAG_VALUE;
posthog.identify(currentUser.id, {
...pickTelemetryPii({
edition: edition ?? ApEdition.COMMUNITY,
email: currentUser.email,
firstName: currentUser.firstName,
lastName: currentUser.lastName,
}),
activepiecesVersion: currentVersion,
activepiecesEnvironment: environment,
deployment,
});
if (currentUser.platformId) {
posthog.group('platform', currentUser.platformId);
}
};
const capture = (event: TelemetryEvent) => {
if (
!telemetryEnabled ||
(!isCloud && isCloudOnlyTelemetryEvent(event.name))
) {
return;
}
posthog.capture(event.name, event.payload);
};
return (
<TelemetryContext.Provider value={{ capture }}>
{children}
</TelemetryContext.Provider>
);
};
const UNKNOWN_FLAG_VALUE = '0.0.0';
const RECORDING_SAMPLE_RATE = 0.1;
const POSTHOG_DEV_OPT_IN_KEY = 'ap_posthog_dev';
function isInRecordingSample(distinctId: string): boolean {
let hash = 5381;
for (let i = 0; i < distinctId.length; i++) {
hash = (hash * 33) ^ distinctId.charCodeAt(i);
}
return (hash >>> 0) / 0xffffffff < RECORDING_SAMPLE_RATE;
}
function isPosthogDevOptIn(): boolean {
const { data } = tryCatchSync(
() => localStorage.getItem(POSTHOG_DEV_OPT_IN_KEY) === '1',
);
return data === true;
}
function resolveDeploymentKind({
edition,
environment,
}: {
edition: ApEdition | null | undefined;
environment: ApEnvironment | null | undefined;
}): DeploymentKind {
if (edition === ApEdition.CLOUD) {
return DeploymentKind.CLOUD;
}
if (environment === ApEnvironment.DEVELOPMENT) {
return DeploymentKind.DEV;
}
return DeploymentKind.SELF_HOSTED;
}
interface TelemetryContextType {
capture: (event: TelemetryEvent) => void;
}
const TelemetryContext = React.createContext<TelemetryContextType>({
capture: () => {},
});
export const useTelemetry = () => React.useContext(TelemetryContext);
export default TelemetryProvider;