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.

16.9 KB
Raw Blame
from dataclasses import dataclass, field
from typing import Optional

from products.warehouse_sources.sources.sdk import IncrementalField, incremental_field


@dataclass(frozen=False)
class PardotEndpointConfig:
    name: str
    # Object path under /api/v5/objects/.
    path: str
    # Account Engagement v5 returns only the record id unless `fields` names the
    # columns, so every endpoint carries an explicit scalar field list. Relationship
    # fields (campaign, createdBy, folder, ...) are left out to keep rows flat.
    fields: tuple[str, ...]
    primary_key: str = "id"
    # Sort applied on the first request of a page chain. `None` where the object's
    # docs don't advertise a sortable field, so we don't risk a 400 on orderBy.
    order_by: Optional[str] = None
    incremental_fields: list[IncrementalField] = field(default_factory=list)
    # Stable creation-time field for datetime partitioning — never updatedAt, which
    # would rewrite partitions on every sync.
    partition_key: Optional[str] = None


_TIMESTAMPS = ("createdAt", "updatedAt", "createdById", "updatedById")

# Endpoints that expose both createdAtAfterOrEqualTo/updatedAtAfterOrEqualTo filters
# AND createdAt/updatedAt as orderBy values, which is what makes them safely
# incremental: the transport filters and sorts on the same field so the pipeline's
# watermark advances in step with the pages it consumes.
_CREATED_AND_UPDATED = [incremental_field("createdAt"), incremental_field("updatedAt")]


PARDOT_ENDPOINTS: dict[str, PardotEndpointConfig] = {
    "prospects": PardotEndpointConfig(
        name="prospects",
        path="prospects",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        partition_key="createdAt",
        fields=(
            "id",
            "campaignId",
            "salesforceId",
            "salesforceAccountId",
            "salesforceContactId",
            "salesforceLeadId",
            "salesforceOwnerId",
            "salesforceCampaignId",
            "salesforceUrl",
            "salesforceLastSync",
            "email",
            "firstName",
            "lastName",
            "salutation",
            "company",
            "jobTitle",
            "department",
            "industry",
            "website",
            "addressOne",
            "addressTwo",
            "city",
            "state",
            "country",
            "zip",
            "territory",
            "phone",
            "fax",
            "employees",
            "annualRevenue",
            "yearsInBusiness",
            "source",
            "score",
            "grade",
            "notes",
            "comments",
            "isDoNotCall",
            "isDoNotEmail",
            "isReviewed",
            "isStarred",
            "isDeleted",
            "isEmailHardBounced",
            "optedOut",
            "doNotSell",
            "emailBouncedAt",
            "emailBouncedReason",
            "firstActivityAt",
            "firstAssignedAt",
            "lastActivityAt",
            "convertedAt",
            "convertedFromObjectName",
            "convertedFromObjectType",
            "recentInteraction",
            "campaignParameter",
            "mediumParameter",
            "sourceParameter",
            "termParameter",
            "contentParameter",
            "firstReferrerQuery",
            "firstReferrerType",
            "firstReferrerUrl",
            "prospectAccountId",
            "lifecycleStageId",
            "assignedToId",
            "userId",
            "profileId",
            *_TIMESTAMPS,
        ),
    ),
    "prospect_accounts": PardotEndpointConfig(
        name="prospect_accounts",
        path="prospect-accounts",
        # Prospect accounts only sort by id/salesforceId and document no datetime
        # filters, so this table is full refresh.
        order_by="id",
        fields=(
            "id",
            "name",
            "salesforceId",
            "isDeleted",
            "annualRevenue",
            "billingAddressOne",
            "billingAddressTwo",
            "billingCity",
            "billingCountry",
            "billingState",
            "billingZip",
            "description",
            "employees",
            "fax",
            "industry",
            "number",
            "ownership",
            "phone",
            "rating",
            "shippingAddressOne",
            "shippingAddressTwo",
            "shippingCity",
            "shippingCountry",
            "shippingState",
            "shippingZip",
            "sic",
            "site",
            "tickerSymbol",
            "type",
            "website",
            "assignedToId",
            *_TIMESTAMPS,
        ),
    ),
    "lists": PardotEndpointConfig(
        name="lists",
        path="lists",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        fields=(
            "id",
            "name",
            "description",
            "title",
            "isPublic",
            "isDynamic",
            "isDeleted",
            "folderId",
            "campaignId",
            *_TIMESTAMPS,
        ),
    ),
    "list_memberships": PardotEndpointConfig(
        name="list_memberships",
        path="list-memberships",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        partition_key="createdAt",
        fields=(
            "id",
            "listId",
            "prospectId",
            "optedOut",
            "isDeleted",
            *_TIMESTAMPS,
        ),
    ),
    "campaigns": PardotEndpointConfig(
        name="campaigns",
        path="campaigns",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        fields=(
            "id",
            "name",
            "cost",
            "folderId",
            "parentCampaignId",
            "salesforceId",
            "isDeleted",
            *_TIMESTAMPS,
        ),
    ),
    "opportunities": PardotEndpointConfig(
        name="opportunities",
        path="opportunities",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        partition_key="createdAt",
        fields=(
            "id",
            "name",
            "stage",
            "status",
            "probability",
            "value",
            "type",
            "closedAt",
            "campaignId",
            "salesforceId",
            *_TIMESTAMPS,
        ),
    ),
    "users": PardotEndpointConfig(
        name="users",
        path="users",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        fields=(
            "id",
            "email",
            "firstName",
            "lastName",
            "username",
            "jobTitle",
            "role",
            "roleName",
            "salesforceId",
            "isDeleted",
            "tagReplacementLanguage",
            *_TIMESTAMPS,
        ),
    ),
    "visits": PardotEndpointConfig(
        name="visits",
        path="visits",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        partition_key="createdAt",
        fields=(
            "id",
            "visitorId",
            "prospectId",
            "visitorPageViewCount",
            "firstVisitorPageViewAt",
            "lastVisitorPageViewAt",
            "durationInSeconds",
            "campaignParameter",
            "mediumParameter",
            "sourceParameter",
            "termParameter",
            "contentParameter",
            "createdAt",
            "updatedAt",
        ),
    ),
    "visitors": PardotEndpointConfig(
        name="visitors",
        path="visitors",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        partition_key="createdAt",
        fields=(
            "id",
            "prospectId",
            "hostname",
            "ipAddress",
            "pageViewCount",
            "isIdentified",
            "doNotSell",
            "campaignParameter",
            "mediumParameter",
            "sourceParameter",
            "termParameter",
            "contentParameter",
            "createdAt",
            "updatedAt",
        ),
    ),
    "visitor_activities": PardotEndpointConfig(
        name="visitor_activities",
        path="visitor-activities",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        partition_key="createdAt",
        fields=(
            "id",
            "type",
            "typeName",
            "details",
            "prospectId",
            "visitorId",
            "visitId",
            "visitorPageViewId",
            "campaignId",
            "customRedirectId",
            "emailId",
            "emailTemplateId",
            "fileId",
            "formId",
            "formHandlerId",
            "landingPageId",
            "listEmailId",
            "multivariateTestVariationId",
            "opportunityId",
            "paidSearchAdId",
            "siteSearchQueryId",
            "createdAt",
            "updatedAt",
        ),
    ),
    "custom_fields": PardotEndpointConfig(
        name="custom_fields",
        path="custom-fields",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        fields=(
            "id",
            "name",
            "fieldId",
            "type",
            "isRecordMultipleResponses",
            "isUseValues",
            "isRequired",
            "isAnalyticsSynced",
            # valuesPrefill is write-only on create — the v5 API 400s if it's requested
            # in a query's `fields` list.
            "salesforceId",
            *_TIMESTAMPS,
        ),
    ),
    "forms": PardotEndpointConfig(
        name="forms",
        path="forms",
        # Forms document no orderBy or datetime filters — full refresh in the API's
        # own default order.
        fields=(
            "id",
            "name",
            "campaignId",
            "folderId",
            "layoutTemplateId",
            "trackerDomainId",
            "submitButtonText",
            "redirectLocation",
            "isUseRedirectLocation",
            "isAlwaysDisplay",
            "isCaptchaEnabled",
            "isCookieless",
            "showNotProspect",
            "salesforceId",
            "salesforceCmsId",
            "url",
            "embedCode",
            "isDeleted",
            *_TIMESTAMPS,
        ),
    ),
    "list_emails": PardotEndpointConfig(
        name="list_emails",
        path="list-emails",
        # List emails filter on createdAt/updatedAt/sentAt but advertise no orderBy, so
        # page order isn't guaranteed — full refresh rather than an unordered watermark.
        # The message bodies (htmlMessage/textMessage) are skipped to keep rows small.
        fields=(
            "id",
            "name",
            "subject",
            "campaignId",
            "emailTemplateId",
            "trackerDomainId",
            "folderId",
            "clientType",
            "type",
            "isOperational",
            "isPaused",
            "isSent",
            "isDeleted",
            # scheduledTime is write-only on create — the v5 API 400s if it's requested
            # in a query's `fields` list.
            "sentAt",
            *_TIMESTAMPS,
        ),
    ),
    "emails": PardotEndpointConfig(
        name="emails",
        path="emails",
        # Emails have no createdAt/updatedAt; sentAt is the only datetime they filter and
        # sort on. Unsent scheduled emails carry a null sentAt, so it can't partition.
        order_by="id",
        incremental_fields=[incremental_field("sentAt")],
        # textMessage/htmlMessage aren't queryable and scheduledTime is write-only on
        # create — v5 400s if any of them is in a query's `fields` list.
        fields=(
            "id",
            "name",
            "subject",
            "campaignId",
            "prospectId",
            "listEmailId",
            "emailTemplateId",
            "trackerDomainId",
            "folderId",
            "clientType",
            "type",
            "isOperational",
            "salesforceCmsId",
            "sentAt",
            "createdById",
        ),
    ),
    "lifecycle_stages": PardotEndpointConfig(
        name="lifecycle_stages",
        path="lifecycle-stages",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        fields=(
            "id",
            "name",
            "position",
            "matchType",
            "isLocked",
            "isDeleted",
            "createdAt",
            "updatedAt",
        ),
    ),
    "lifecycle_histories": PardotEndpointConfig(
        name="lifecycle_histories",
        path="lifecycle-histories",
        # Lifecycle histories are append-only and only filter/sort on createdAt.
        order_by="id",
        incremental_fields=[incremental_field("createdAt")],
        partition_key="createdAt",
        fields=(
            "id",
            "prospectId",
            "previousStageId",
            "nextStageId",
            "secondsElapsed",
            "createdAt",
        ),
    ),
    "visitor_page_views": PardotEndpointConfig(
        name="visitor_page_views",
        path="visitor-page-views",
        order_by="id",
        incremental_fields=[incremental_field("createdAt")],
        partition_key="createdAt",
        fields=(
            "id",
            "url",
            "title",
            "visitorId",
            "visitId",
            "campaignId",
            "durationInSeconds",
            "salesforceId",
            "createdAt",
        ),
    ),
    "email_templates": PardotEndpointConfig(
        name="email_templates",
        path="email-templates",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        # htmlMessage/textMessage aren't queryable — v5 400s if either is in a query's
        # `fields` list.
        fields=(
            "id",
            "name",
            "subject",
            "type",
            "isOneToOneEmail",
            "isAutoResponderEmail",
            "isDripEmail",
            "isListEmail",
            "campaignId",
            "folderId",
            "trackerDomainId",
            "tagReplacementLanguage",
            "isDeleted",
            *_TIMESTAMPS,
        ),
    ),
    "custom_redirects": PardotEndpointConfig(
        name="custom_redirects",
        path="custom-redirects",
        # Custom redirects only sort and filter on id, so this table is full refresh.
        order_by="id",
        fields=(
            "id",
            "name",
            "campaignId",
            "folderId",
            "trackerDomainId",
            "destinationUrl",
            "url",
            "vanityUrl",
            "vanityUrlPath",
            "trackedUrl",
            "bitlyIsPersonalized",
            "bitlyShortUrl",
            # The ga* fields need a Google Analytics connector; business units without one
            # refuse them, and the transport drops a refused field and retries.
            "gaSource",
            "gaMedium",
            "gaTerm",
            "gaContent",
            "gaCampaign",
            "salesforceId",
            "isDeleted",
            *_TIMESTAMPS,
        ),
    ),
    "form_handlers": PardotEndpointConfig(
        name="form_handlers",
        path="form-handlers",
        # Form handlers have no updatedAt and only filter on createdAt, so an incremental
        # sync would never pick up edits — full refresh.
        order_by="id",
        fields=(
            "id",
            "name",
            "campaignId",
            "folderId",
            "trackerDomainId",
            "isDataForwarded",
            "successLocation",
            "errorLocation",
            "isAlwaysEmail",
            "isCookieless",
            "embedCode",
            "salesforceId",
            "isDeleted",
            "createdAt",
            "createdById",
            "updatedById",
        ),
    ),
    "landing_pages": PardotEndpointConfig(
        name="landing_pages",
        path="landing-pages",
        order_by="id",
        incremental_fields=_CREATED_AND_UPDATED,
        # The page content and CSS fields (content, regionContent, layoutCss, ...) aren't
        # queryable — v5 400s if any of them is in a query's `fields` list.
        fields=(
            "id",
            "name",
            "title",
            "description",
            "campaignId",
            "folderId",
            "formId",
            "layoutTemplateId",
            "trackerDomainId",
            "layoutType",
            "layoutTableBorder",
            "url",
            "vanityUrl",
            "vanityUrlPath",
            "bitlyIsPersonalized",
            "bitlyShortUrl",
            "redirectLocation",
            "isUseRedirectLocation",
            "isDoNotIndex",
            "archiveDate",
            "salesforceId",
            "salesforceCmsId",
            "isDeleted",
            *_TIMESTAMPS,
        ),
    ),
}

ENDPOINTS = tuple(PARDOT_ENDPOINTS.keys())

INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
    name: config.incremental_fields for name, config in PARDOT_ENDPOINTS.items() if config.incremental_fields
}