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.

6.9 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.impact._config import ImpactSourceConfig
from products.warehouse_sources.sources.impact.impact import (
    ImpactResumeConfig,
    impact_source,
    validate_credentials as validate_impact_credentials,
)
from products.warehouse_sources.sources.impact.settings import (
    DEFAULT_API_VERSION,
    ENDPOINTS,
    IMPACT_ENDPOINTS,
    INCREMENTAL_FIELDS,
    SUPPORTED_API_VERSIONS,
)
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)


@SourceRegistry.register
class ImpactSource(ResumableSource[ImpactSourceConfig, ImpactResumeConfig]):
    codebase_detection = detect(script_hosts=("utt.impactcdn.com",))

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    api_docs_url = "https://integrations.impact.com/brand-api-reference/readme/introduction"

    supported_versions = SUPPORTED_API_VERSIONS
    default_version = DEFAULT_API_VERSION

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.IMPACT,
            category=DataWarehouseSourceCategory.ADVERTISING,
            keywords=["impact.com", "impact radius"],
            label="impact.com",
            releaseStatus=ReleaseStatus.GA,
            caption="""Enter your impact.com Account SID and Auth Token to pull your partnership data into the PostHog Data warehouse.

Find these in impact.com under **Settings > Technical > API**. Create a Read-Only access token, then copy its Account SID and Auth Token below.

If your account is a partner (publisher) account, use the impact.com Partner source instead.""",
            iconPath="/static/services/impact.png",
            docsUrl="https://posthog.com/docs/cdp/sources/impact",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="account_sid",
                        label="Account SID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="auth_token",
                        label="Auth token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url: https://api.impact.com": "Your Impact.com Account SID or Auth Token is invalid. Check your credentials, then reconnect.",
            "403 Client Error: Forbidden for url: https://api.impact.com": "Your Impact.com token does not have access to this data. Check the token's permissions, then reconnect.",
        }

    def get_retryable_errors(self) -> set[str]:
        # `_fetch` reads over the tracked session, whose `DEFAULT_RETRY` adapter already retries a
        # 429 or 5xx three times. A response that still reaches us is an exhausted upstream blip —
        # transient, not a PostHog bug — and Temporal retries the whole activity, so the sync
        # self-recovers. `DEFAULT_RETRY.status_forcelist` classes 429 as transient too, so match it
        # here as well or an exhausted rate limit still pages the team. `raise_for_status` derives
        # the "429 Client Error"/"Server Error" prefixes from the status code alone, so they are
        # stable to match on (see mailchimp/app_store_connect for the same pattern).
        return {"429 Client Error", "Server Error"}

    def get_schemas(
        self,
        config: ImpactSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        def _build_schema(endpoint: str) -> SourceSchema:
            has_incremental = bool(INCREMENTAL_FIELDS.get(endpoint))
            return SourceSchema(
                name=endpoint,
                supports_incremental=has_incremental,
                supports_append=has_incremental,
                incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
                should_sync_default=IMPACT_ENDPOINTS[endpoint].should_sync_default,
                description=IMPACT_ENDPOINTS[endpoint].description,
            )

        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: ImpactSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if validate_impact_credentials(config.account_sid, config.auth_token, self.resolve_api_version(api_version)):
            return True, None

        return False, (
            "Your impact.com Account SID or Auth token isn't valid for a brand account. "
            "Check both values, or use the impact.com Partner source for a partner account."
        )

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

    def source_for_pipeline(
        self,
        config: ImpactSourceConfig,
        resumable_source_manager: ResumableSourceManager[ImpactResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return impact_source(
            account_sid=config.account_sid,
            auth_token=config.auth_token,
            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,
            api_version=self.resolve_api_version(inputs.api_version),
        )