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.

7.7 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.pendo._config import PendoSourceConfig
from products.warehouse_sources.sources.pendo.pendo import (
    PendoResumeConfig,
    pendo_source,
    validate_credentials as validate_pendo_credentials,
)
from products.warehouse_sources.sources.pendo.settings import (
    ENDPOINTS,
    EVENTS_DEFAULT_LOOKBACK_DAYS,
    INCREMENTAL_FIELDS,
    PENDO_ENDPOINTS,
)
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,
)


def _schema_description(endpoint: str) -> str | None:
    config = PENDO_ENDPOINTS[endpoint]
    if not config.is_events:
        return None
    lookback = f"Only syncs the last {EVENTS_DEFAULT_LOOKBACK_DAYS} days on initial sync"
    return f"Daily event buckets. {lookback}" if config.grouped_events else lookback


@SourceRegistry.register
class PendoSource(ResumableSource[PendoSourceConfig, PendoResumeConfig]):
    codebase_detection = detect(
        env=("^PENDO_", "^REACT_APP_PENDO_KEY$"),
        script_hosts=(
            "cdn.pendo.io",
            "cdn.eu.pendo.io",
            "us1.cdn.pendo.io",
            "cdn.jpn.pendo.io",
            "cdn.au.pendo.io",
            "pendo-io-static.storage.googleapis.com",
            "pendo-us1-static.storage.googleapis.com",
            "pendo-eu-static.storage.googleapis.com",
            "pendo-jp-prod-static.storage.googleapis.com",
        ),
        npm=(
            "@pendo/agent",
            "rn-pendo-sdk",
            "@pendo/web-sdk",
            "@pendo/openfeature-web-provider",
            "@pendo/openfeature-server-provider",
            "pendo-server-sdk",
            "@pendo/novus-cli",
            "@types/pendo-io-browser",
            "ngx-pendo",
        ),
        pypi=("pendo-server-sdk",),
        cocoapods=("Pendo", "PendoLegacy", "PendoSwiftUI", "PendoSDKLib", "Insert"),
        maven=("sdk.pendo.io:pendoIO", "com.pendo:pendo-cmp"),
        pub=("pendo_sdk",),
        swiftpm=("github.com/pendo-io/pendo-mobile-sdk",),
        alternative_to=(ProductKey.PRODUCT_ANALYTICS, ProductKey.PRODUCT_TOURS, ProductKey.SURVEYS),
    )

    supported_versions = ("v1",)
    default_version = "v1"
    api_docs_url = "https://engageapi.pendo.io"

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

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.PENDO,
            category=DataWarehouseSourceCategory.ANALYTICS,
            label="Pendo",
            caption="""Enter your Pendo integration key to pull your Pendo data into the PostHog Data warehouse.

Create an integration key in Pendo under **Settings > Integrations > Integration Keys** (only Pendo admins can view these). The key is specific to your subscription's data region, so make sure the region below matches the domain you log in with.
""",
            iconPath="/static/services/pendo.png",
            docsUrl="https://posthog.com/docs/cdp/sources/pendo",
            releaseStatus=ReleaseStatus.GA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="integration_key",
                        label="Integration key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="Pendo integration key",
                        secret=True,
                    ),
                    SourceFieldSelectConfig(
                        name="region",
                        label="Data region",
                        required=True,
                        defaultValue="us",
                        options=[
                            SourceFieldSelectConfigOption(label="US (app.pendo.io)", value="us"),
                            SourceFieldSelectConfigOption(label="US1 (us1.app.pendo.io)", value="us1"),
                            SourceFieldSelectConfigOption(label="EU (app.eu.pendo.io)", value="eu"),
                            SourceFieldSelectConfigOption(label="Japan (app.jpn.pendo.io)", value="jp"),
                            SourceFieldSelectConfigOption(label="Australia (app.au.pendo.io)", value="au"),
                        ],
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: PendoSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Metadata endpoints are full refresh: Pendo exposes no server-side timestamp filter for
        # them. Event endpoints sync incrementally by day, but each run re-reads the days Pendo may
        # still reprocess, so append would duplicate their rows — merge only.
        schemas = [
            SourceSchema(
                name=endpoint,
                supports_incremental=PENDO_ENDPOINTS[endpoint].is_events,
                supports_append=False,
                incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
                description=_schema_description(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: PendoSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
    ) -> tuple[bool, str | None]:
        return validate_pendo_credentials(config.integration_key, config.region)

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error": (
                "Your Pendo integration key is invalid or has been revoked. Generate a new key in Pendo "
                "(Settings > Integrations > Integration Keys) and reconnect."
            ),
            "403 Client Error": (
                "Your Pendo integration key is missing the required permissions. Make sure it has read "
                "access (write access is needed for the aggregation endpoint) and reconnect."
            ),
        }

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

    def source_for_pipeline(
        self,
        config: PendoSourceConfig,
        resumable_source_manager: ResumableSourceManager[PendoResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return pendo_source(
            integration_key=config.integration_key,
            region=config.region,
            endpoint=inputs.schema_name,
            logger=inputs.logger,
            resumable_source_manager=resumable_source_manager,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
        )