:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from typing import Optional, cast
from products.warehouse_sources.sources.mixpanel._config import MixpanelSourceConfig
from products.warehouse_sources.sources.mixpanel.mixpanel import (
EXPORT_TRUNCATED_ERROR,
MixpanelResumeConfig,
mixpanel_source,
validate_credentials as validate_mixpanel_credentials,
)
from products.warehouse_sources.sources.mixpanel.settings import (
DEFAULT_API_VERSION,
ENDPOINTS,
INCREMENTAL_FIELDS,
MIXPANEL_ENDPOINTS,
SUPPORTED_API_VERSIONS,
SUPPORTS_INCREMENTAL,
)
from products.warehouse_sources.sources.sdk import (
CanonicalDescriptions,
DataWarehouseSourceCategory,
ExternalDataSourceType,
FieldType,
ProductKey,
ReleaseStatus,
ResumableSource,
ResumableSourceManager,
SourceConfig,
SourceFieldInputConfig,
SourceFieldInputConfigType,
SourceFieldSelectConfig,
SourceFieldSelectConfigOption,
SourceInputs,
SourceRegistry,
SourceResponse,
SourceSchema,
detect,
)
@SourceRegistry.register
class MixpanelSource(ResumableSource[MixpanelSourceConfig, MixpanelResumeConfig]):
codebase_detection = detect(
env=("^MIXPANEL_", "^NEXT_PUBLIC_MIXPANEL_TOKEN$", "^NEXT_PUBLIC_MIXPANEL_API_HOST$"),
script_hosts=("mxpnl.com",),
npm=(
"mixpanel",
"mixpanel-browser",
"mixpanel-react-native",
"@mixpanel/react-native-session-replay",
"@mixpanel/openfeature-web-provider",
"@mixpanel/openfeature-server-provider",
"@mixpanel/mixpanel-wizard",
"@types/mixpanel-browser",
"@types/mixpanel",
"@analytics/mixpanel",
),
pypi=("mixpanel", "mixpanel-openfeature", "mixpanel-headless", "mixpanel-utils", "mixpanel-api", "mixpanel-py"),
cargo=("mixpanel-rs",),
cocoapods=("Mixpanel-swift", "Mixpanel", "MixpanelSessionReplay", "MixpanelSwiftCommon"),
composer=(
"mixpanel/mixpanel-php",
"genealabs/laravel-mixpanel",
"mike-bronner/laravel-mixpanel",
"gordalina/mixpanel-bundle",
),
go=(
"github.com/mixpanel/mixpanel-go",
"github.com/mixpanel/mixpanel-go/openfeature",
"github.com/dukex/mixpanel",
),
hex=("mixpanel_api_ex",),
maven=(
"com.mixpanel.android:mixpanel-android",
"com.mixpanel:mixpanel-java",
"com.mixpanel.android:mixpanel-android-session-replay",
"com.mixpanel.android:mixpanel-android-openfeature",
"com.mixpanel.android:mixpanel-android-common",
"com.mixpanel.android:mixpanel-android-experimental",
"com.mixpanel.android:mparticle-android-integration-mixpanel",
"com.mixpanel:mixpanel-java-openfeature",
"com.mixpanel:mixpanel-java-extension-jackson",
),
pub=("mixpanel_flutter", "mixpanel_flutter_session_replay", "mixpanel_flutter_common"),
rubygems=("mixpanel-ruby", "mixpanel-ruby-openfeature", "mixpanel_client"),
swiftpm=(
"github.com/mixpanel/mixpanel-swift",
"github.com/mixpanel/mixpanel-iphone",
"github.com/mixpanel/mixpanel-swift-openfeature",
"github.com/mixpanel/mixpanel-ios-session-replay-package",
"github.com/mixpanel/mixpanel-swift-common",
),
alternative_to=(ProductKey.PRODUCT_ANALYTICS,),
)
api_docs_url = "https://developer.mixpanel.com/reference/raw-data-export-api"
supported_versions = SUPPORTED_API_VERSIONS
default_version = DEFAULT_API_VERSION
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.MIXPANEL
@property
def connection_host_fields(self) -> list[str]:
# `region` picks the host and `project_id` the project the stored service-account secret is
# sent to; retargeting either must re-require the secret so it can't be aimed elsewhere.
return ["region", "project_id"]
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.MIXPANEL,
category=DataWarehouseSourceCategory.ANALYTICS,
label="Mixpanel",
caption="""Connect your Mixpanel project to pull events, user profiles, cohorts, annotations, funnels and retention into the PostHog Data warehouse.
Authenticate with a [Mixpanel Service Account](https://developer.mixpanel.com/reference/service-accounts). In Mixpanel, go to **Organization Settings → Service Accounts → Create Service Account** and grant it access to the project you want to sync. You'll need:
- The service account **username** and **secret**
- Your numeric **Project ID** (found under **Project Settings**)
- The **data residency region** your project lives in (US, EU or India)
""",
iconPath="/static/services/mixpanel.png",
docsUrl="https://posthog.com/docs/cdp/sources/mixpanel",
releaseStatus=ReleaseStatus.GA,
fields=cast(
list[FieldType],
[
SourceFieldSelectConfig(
name="region",
label="Data residency region",
required=True,
defaultValue="us",
options=[
SourceFieldSelectConfigOption(label="US (mixpanel.com)", value="us"),
SourceFieldSelectConfigOption(label="EU (eu.mixpanel.com)", value="eu"),
SourceFieldSelectConfigOption(label="India (in.mixpanel.com)", value="in"),
],
),
SourceFieldInputConfig(
name="project_id",
label="Project ID",
type=SourceFieldInputConfigType.TEXT,
required=True,
placeholder="1234567",
secret=False,
),
SourceFieldInputConfig(
name="service_account_username",
label="Service account username",
type=SourceFieldInputConfigType.TEXT,
required=True,
placeholder="my-service-account.abc123.mp-service-account",
secret=False,
),
SourceFieldInputConfig(
name="service_account_secret",
label="Service account secret",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="",
secret=True,
),
],
),
)
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.mixpanel.canonical_descriptions import CANONICAL_DESCRIPTIONS
return CANONICAL_DESCRIPTIONS
def get_schemas(
self,
config: MixpanelSourceConfig,
team_id: int,
with_counts: bool = False,
names: list[str] | None = None,
force_refresh: bool = False,
api_version: str | None = None,
) -> list[SourceSchema]:
schemas = [
SourceSchema(
name=endpoint,
supports_incremental=endpoint in SUPPORTS_INCREMENTAL,
supports_append=endpoint in SUPPORTS_INCREMENTAL,
incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
description=MIXPANEL_ENDPOINTS[endpoint].description,
)
for endpoint in ENDPOINTS
]
if names is not None:
names_set = set(names)
schemas = [s for s in schemas if s.name in names_set]
return schemas
def validate_credentials(
self,
config: MixpanelSourceConfig,
team_id: int,
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
return validate_mixpanel_credentials(
region=config.region,
username=config.service_account_username,
secret=config.service_account_secret,
project_id=config.project_id,
schema_name=schema_name,
)
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
"401 Client Error: Unauthorized": (
"Mixpanel rejected the service account credentials. Create a new service account in "
"Organization Settings and reconnect."
),
"403 Client Error: Forbidden": (
"The Mixpanel service account does not have access to this project or resource. Grant it "
"access to the project and reconnect."
),
# Mixpanel returns 402 when the project's plan does not include the data export API or the
# account is in a payment-overdue state. Retrying cannot resolve a billing issue.
"402 Client Error: Payment Required": (
"Your Mixpanel plan does not include data export, or its billing is overdue. Upgrade your "
"Mixpanel plan or settle its billing, then resync."
),
}
def get_retryable_errors(self) -> set[str]:
# A 429 (rate limit) or 5xx is retried internally honoring Retry-After; if those retries
# still exhaust, the failure is transient and self-recovering, so let Temporal retry the
# activity without surfacing it as tracked exception noise. An export Mixpanel aborts
# mid-body reaches us on the same contract, after `_stream_export_day` re-fetches the day.
return {"Mixpanel API error (retryable)", EXPORT_TRUNCATED_ERROR}
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[MixpanelResumeConfig]:
return ResumableSourceManager[MixpanelResumeConfig](inputs, MixpanelResumeConfig)
def source_for_pipeline(
self,
config: MixpanelSourceConfig,
resumable_source_manager: ResumableSourceManager[MixpanelResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
return mixpanel_source(
region=config.region,
username=config.service_account_username,
secret=config.service_account_secret,
project_id=config.project_id,
endpoint=inputs.schema_name,
logger=inputs.logger,
manager=resumable_source_manager,
should_use_incremental_field=inputs.should_use_incremental_field,
db_incremental_field_last_value=inputs.db_incremental_field_last_value
if inputs.should_use_incremental_field
else None,
api_version=self.resolve_api_version(inputs.api_version),
)