Skip to content

: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.

12.7 KB
Raw Blame
from typing import Optional, cast

import structlog

from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ProductKey,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)
from products.warehouse_sources.sources.snowplow._config import SnowplowSourceConfig
from products.warehouse_sources.sources.snowplow.settings import ENDPOINTS, INCREMENTAL_FIELDS, SNOWPLOW_ENDPOINTS
from products.warehouse_sources.sources.snowplow.snowplow import (
    SnowplowResumeConfig,
    snowplow_source,
    validate_credentials as validate_snowplow_credentials,
)


@SourceRegistry.register
class SnowplowSource(ResumableSource[SnowplowSourceConfig, SnowplowResumeConfig]):
    codebase_detection = detect(
        env=("^SNOWPLOW_", "^NEXT_PUBLIC_SNOWPLOW_", "^MICRO_IGLU_REGISTRY_URL$", "^MICRO_IGLU_API_KEY$"),
        script_hosts=("d1fc8wv8zag5ca.cloudfront.net",),
        npm=(
            "@snowplow/node-tracker",
            "@snowplow/browser-tracker",
            "@snowplow/react-native-tracker",
            "@snowplow/browser-plugin-ad-tracking",
            "@snowplow/browser-plugin-bot-detection",
            "@snowplow/browser-plugin-browser-features",
            "@snowplow/browser-plugin-button-click-tracking",
            "@snowplow/browser-plugin-client-hints",
            "@snowplow/browser-plugin-consent",
            "@snowplow/browser-plugin-debugger",
            "@snowplow/browser-plugin-ecommerce",
            "@snowplow/browser-plugin-element-tracking",
            "@snowplow/browser-plugin-enhanced-consent",
            "@snowplow/browser-plugin-enhanced-ecommerce",
            "@snowplow/browser-plugin-error-tracking",
            "@snowplow/browser-plugin-event-specifications",
            "@snowplow/browser-plugin-focalmeter",
            "@snowplow/browser-plugin-form-tracking",
            "@snowplow/browser-plugin-ga-cookies",
            "@snowplow/browser-plugin-geolocation",
            "@snowplow/browser-plugin-link-click-tracking",
            "@snowplow/browser-plugin-media",
            "@snowplow/browser-plugin-media-tracking",
            "@snowplow/browser-plugin-optimizely",
            "@snowplow/browser-plugin-optimizely-x",
            "@snowplow/browser-plugin-performance-navigation-timing",
            "@snowplow/browser-plugin-performance-timing",
            "@snowplow/browser-plugin-privacy-sandbox",
            "@snowplow/browser-plugin-screen-tracking",
            "@snowplow/browser-plugin-site-tracking",
            "@snowplow/browser-plugin-snowplow-ecommerce",
            "@snowplow/browser-plugin-timezone",
            "@snowplow/browser-plugin-vimeo-tracking",
            "@snowplow/browser-plugin-web-vitals",
            "@snowplow/browser-plugin-webview",
            "@snowplow/browser-plugin-youtube-tracking",
            "@snowplow/browser-tracker-core",
            "@snowplow/javascript-tracker",
            "@snowplow/roku-tracker",
            "@snowplow/signals-browser-plugin",
            "@snowplow/signals-core",
            "@snowplow/signals-node",
            "@snowplow/snowplow-cli",
            "@snowplow/snowtype",
            "@snowplow/tracker-core",
            "@snowplow/webview-tracker",
            "snowplow-analytics-sdk",
            "snowplow-tracker",
            "snowplow-tracker-core",
        ),
        pypi=("snowplow-tracker", "snowplow-analytics-sdk", "snowplow-signals"),
        cargo=("snowplow_tracker",),
        cocoapods=("SnowplowTracker",),
        composer=("snowplow/snowplow-tracker",),
        go=("github.com/snowplow/snowplow-golang-tracker", "github.com/snowplow/snowplow-golang-analytics-sdk"),
        maven=(
            "com.snowplowanalytics:snowplow-android-tracker",
            "com.snowplowanalytics:snowplow-java-tracker",
            "com.snowplowanalytics:snowplow-scala-analytics-sdk_2.10",
            "com.snowplowanalytics:snowplow-scala-analytics-sdk_2.11",
            "com.snowplowanalytics:snowplow-scala-analytics-sdk_2.12",
            "com.snowplowanalytics:snowplow-scala-analytics-sdk_2.13",
            "com.snowplowanalytics:snowplow-scala-analytics-sdk_3",
            "com.snowplowanalytics:snowplow-scala-tracker-core_2.11",
            "com.snowplowanalytics:snowplow-scala-tracker-core_2.12",
            "com.snowplowanalytics:snowplow-scala-tracker-core_2.13",
            "com.snowplowanalytics:snowplow-scala-tracker-core_3",
            "com.snowplowanalytics:snowplow-scala-tracker-emitter-http4s_2.12",
            "com.snowplowanalytics:snowplow-scala-tracker-emitter-http4s_2.13",
            "com.snowplowanalytics:snowplow-scala-tracker-emitter-http4s_3",
            "com.snowplowanalytics:snowplow-scala-tracker-emitter-id_2.11",
            "com.snowplowanalytics:snowplow-scala-tracker-emitter-id_2.12",
            "com.snowplowanalytics:snowplow-scala-tracker-emitter-id_2.13",
            "com.snowplowanalytics:snowplow-scala-tracker-emitter-id_3",
            "com.snowplowanalytics:snowplow-scala-tracker-metadata_2.11",
            "com.snowplowanalytics:snowplow-scala-tracker-metadata_2.12",
            "com.snowplowanalytics:snowplow-scala-tracker-metadata_2.13",
            "com.snowplowanalytics:snowplow-scala-tracker-metadata_3",
            "com.snowplowanalytics:snowplow-scala-tracker_2.10",
            "com.snowplowanalytics:snowplow-scala-tracker_2.11",
            "com.snowplowanalytics:snowplow-scala-tracker_2.12",
        ),
        pub=("snowplow_tracker",),
        rubygems=("snowplow-tracker",),
        swiftpm=("github.com/snowplow/snowplow-ios-tracker", "github.com/snowplow/snowplow-objc-tracker"),
        alternative_to=(ProductKey.PRODUCT_ANALYTICS,),
    )

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.SNOWPLOW

    @property
    def connection_host_fields(self) -> list[str]:
        # `organization_id` selects the org path the stored API key is sent to; a multi-org key
        # could otherwise be retargeted at another org's data without re-entering the secret.
        return ["organization_id"]

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.SNOWPLOW,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="Snowplow Analytics",
            releaseStatus=ReleaseStatus.BETA,
            caption="""Enter your Snowplow BDP Console API credentials to pull your pipeline health and data modeling job data into the PostHog Data warehouse.

Find your **Organization ID** on the Console's *Manage organization* page, then create an API key (a key ID + key secret pair) under **Console settings → API keys**. Note that all Snowplow Console API keys carry admin privileges, so store them carefully.

This connector talks to the standard BDP Console host (`console.snowplowanalytics.com`); privately-hosted Console deployments are not supported yet.""",
            iconPath="/static/services/snowplow.png",
            docsUrl="https://posthog.com/docs/cdp/sources/snowplow",
            keywords=["bdp", "behavioral data", "failed events", "data quality"],
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="organization_id",
                        label="Organization ID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="9e884a10-51c9-4632-9c05-01ba4c2b521a",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="api_key_id",
                        label="API key ID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="a1b2c3d4-0000-0000-0000-000000000000",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.snowplow.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "Snowplow API authentication failed": "Your Snowplow credentials are invalid. Check the organization ID, API key ID, and API key in your Snowplow Console settings, then reconnect.",
            "401 Client Error: Unauthorized for url: https://console.snowplowanalytics.com": "Your Snowplow API key is invalid or has been revoked. Create a new API key in your Snowplow Console settings, then reconnect.",
            "403 Client Error: Forbidden for url: https://console.snowplowanalytics.com": "Your Snowplow API key does not have access to this data. Check the key in your Snowplow Console settings, then reconnect.",
        }

    def get_schemas(
        self,
        config: SnowplowSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        def _description(endpoint: str) -> str | None:
            if endpoint in ("job_runs", "job_run_steps"):
                return (
                    "Snowplow only retains job run history for about the preceding week, "
                    "so historical backfill beyond that is not possible"
                )
            if endpoint == "failed_event_metrics":
                return (
                    "Failed-event counts per pipeline, error, and time bucket. "
                    "Snowplow keeps about a week of these aggregates"
                )
            return None

        def _build_schema(endpoint: str) -> SourceSchema:
            endpoint_config = SNOWPLOW_ENDPOINTS[endpoint]
            has_incremental = bool(INCREMENTAL_FIELDS.get(endpoint))
            return SourceSchema(
                name=endpoint,
                supports_incremental=has_incremental,
                # Rows get revised upstream (run states transition, failed-event buckets keep
                # accumulating) and incremental re-pulls a trailing window that merge dedupes,
                # so append — which would materialize the re-pulls as duplicates — is never offered.
                supports_append=False,
                incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
                should_sync_default=endpoint_config.should_sync_default,
                description=_description(endpoint),
            )

        schemas = [_build_schema(endpoint) 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: SnowplowSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_snowplow_credentials(
            config.organization_id, config.api_key_id, config.api_key, structlog.get_logger(__name__)
        )

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[SnowplowResumeConfig]:
        return ResumableSourceManager[SnowplowResumeConfig](inputs, SnowplowResumeConfig)

    def source_for_pipeline(
        self,
        config: SnowplowSourceConfig,
        resumable_source_manager: ResumableSourceManager[SnowplowResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return snowplow_source(
            organization_id=config.organization_id,
            api_key_id=config.api_key_id,
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            logger=inputs.logger,
            resumable_source_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,
        )