The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
import { useEffect } from "react";
import { useIsTrackingEnabled } from "embedding-sdk-bundle/analytics/component-events";
import type { SdkAuthMethod } from "embedding-sdk-bundle/analytics/snowplow";
import {
getHostReactVersion,
getSdkAuthMethod,
getSdkClient,
getSdkLocaleUsed,
initSdkTracker,
trackSdkSimpleEvent,
} from "embedding-sdk-bundle/analytics/snowplow";
import { setSdkTrackerReady } from "embedding-sdk-bundle/store/reducer";
import type { SdkStore } from "embedding-sdk-bundle/store/types";
import { getSdkPackageVersion } from "embedding-sdk-shared/lib/get-build-info";
import type { MetabaseAuthConfig } from "embedding-sdk-shared/types/auth-config";
import { isEmbeddingEajs } from "metabase/embedding-sdk/config";
import { getSettings } from "metabase/settings";
import { initMetaplow } from "metabase/utils/metaplow";
const METAPLOW_RELAY_PATH = "/api/metaplow-proxy";
function getMetaplowHostUrl(store: SdkStore, metabaseInstanceUrl: string) {
const isEnabled = !!getSettings(store.getState())?.[
"metaplow-tracking-enabled"
];
return isEnabled
? `${metabaseInstanceUrl.replace(/\/+$/, "")}${METAPLOW_RELAY_PATH}`
: null;
}
export function deriveAuthMethod(
authConfig: MetabaseAuthConfig,
): SdkAuthMethod {
if (authConfig.isGuest) {
return "guest";
}
if ("apiKey" in authConfig && authConfig.apiKey) {
return "api_key";
}
return "sso";
}
// Initialize the SDK Snowplow tracker and fire the provider-init adoption beacon.
// Waits for anon-tracking-enabled to be loaded from the instance settings so the
// opt-out gate is respected. Fires once per JS load; idempotent under re-renders.
export function useInitSdkTracker(
authConfig: MetabaseAuthConfig,
reduxStore: SdkStore,
localeUsed: boolean,
) {
const isTrackingEnabled = useIsTrackingEnabled();
useEffect(() => {
if (isEmbeddingEajs() || !isTrackingEnabled) {
return;
}
initMetaplow({
hostUrl: getMetaplowHostUrl(reduxStore, authConfig.metabaseInstanceUrl),
// Via frontend/src/embedding-sdk-bundle/analytics/snowplow.ts
// Omits userId — unlike the main-app tracker, SDK component usage is tracked at instance granularity;
// the analytics-uuid already identifies the account.
getUserId: () => undefined,
});
const authMethod = deriveAuthMethod(authConfig);
const wasJustInitialized = initSdkTracker({
metabaseInstanceUrl: authConfig.metabaseInstanceUrl,
authMethod,
localeUsed,
store: reduxStore,
});
// setSdkTrackerReady unblocks per-mount hooks in child components. Set it
// even when wasJustInitialized=false (e.g. multiple providers) so children
// in a nested provider context can also fire.
reduxStore.dispatch(setSdkTrackerReady(true));
if (wasJustInitialized) {
const sdkVersion = getSdkPackageVersion();
trackSdkSimpleEvent({
event: "embedding_sdk_initialized",
event_detail: JSON.stringify({
global: {
auth_method: getSdkAuthMethod(),
sdk_version: sdkVersion,
locale_used: getSdkLocaleUsed(),
react_version: getHostReactVersion(),
client: getSdkClient(),
},
}),
});
}
// isTrackingEnabled is the only dep: fire once when the opt-out gate becomes
// known. authConfig and reduxStore are stable across the provider lifetime.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isTrackingEnabled]);
}