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.1 KB
Raw Blame
from dataclasses import field
from typing import Literal, Optional

from products.warehouse_sources.sources.sdk import (
    IncrementalField,
    PartitionFormat,
    SortMode,
    frozen,
    incremental_field,
)

DUB_BASE_URL = "https://api.dub.co"


@frozen
class DubEndpointConfig:
    name: str
    path: str
    # "cursor" = startingAfter cursor pagination; "page" = 1-based page-number pagination;
    # "single" = the whole table arrives in one response, as the aggregate endpoints do.
    pagination: Literal["cursor", "page", "single"]
    page_size: int = 100
    page_size_param: str = "pageSize"
    primary_keys: tuple[str, ...] = ("id",)
    partition_key: Optional[str] = "createdAt"
    partition_format: PartitionFormat = "month"
    sort_mode: SortMode = "desc"
    params: dict[str, str] = field(default_factory=dict)
    # Set for the /events streams; selects which event type the shared endpoint returns.
    event_type: Optional[str] = None
    # /links returns only the links sitting outside a folder unless `folderId` is passed, so
    # this endpoint has to be walked once per folder as well to import the whole workspace.
    folder_scoped: bool = False
    # Walked once per enrolled partner by a custom iterator; see _partner_analytics_rows.
    partner_scoped: bool = False


def _event_endpoint(name: str, event_type: str, primary_key: str) -> DubEndpointConfig:
    return DubEndpointConfig(
        name=name,
        path="/events",
        pagination="page",
        # /events allows up to 1000 rows per page; larger pages keep us well inside
        # Dub's stricter per-second analytics rate limits.
        page_size=500,
        page_size_param="limit",
        primary_keys=(primary_key,),
        partition_key="timestamp",
        partition_format="week",
        sort_mode="asc",
        params={"event": event_type, "sortBy": "timestamp", "sortOrder": "asc"},
        event_type=event_type,
    )


# /analytics answers a 24h window of clicks only unless both defaults are widened, and each
# groupBy returns a different row shape, so every breakdown is imported as its own table.
def _analytics_endpoint(name: str, group_by: str, primary_keys: tuple[str, ...]) -> DubEndpointConfig:
    return DubEndpointConfig(
        name=name,
        path="/analytics",
        pagination="single",
        primary_keys=primary_keys,
        partition_key=None,
        params={"groupBy": group_by, "event": "composite", "interval": "all"},
    )


DUB_ENDPOINTS: dict[str, DubEndpointConfig] = {
    "links": DubEndpointConfig(name="links", path="/links", pagination="cursor", folder_scoped=True),
    "click_events": _event_endpoint("click_events", "clicks", primary_key="click_id"),
    "lead_events": _event_endpoint("lead_events", "leads", primary_key="eventId"),
    "sale_events": _event_endpoint("sale_events", "sales", primary_key="eventId"),
    "customers": DubEndpointConfig(name="customers", path="/customers", pagination="cursor"),
    "tags": DubEndpointConfig(
        name="tags",
        path="/tags",
        pagination="page",
        partition_key=None,
        # Explicit stable sort so rows don't shift across page boundaries mid-sync.
        params={"sortBy": "createdAt", "sortOrder": "asc"},
        sort_mode="asc",
    ),
    "domains": DubEndpointConfig(name="domains", path="/domains", pagination="page", page_size=50),
    "folders": DubEndpointConfig(name="folders", path="/folders", pagination="page", page_size=50),
    "partners": DubEndpointConfig(
        name="partners",
        path="/partners",
        pagination="page",
        params={"sortBy": "createdAt", "sortOrder": "asc"},
        sort_mode="asc",
    ),
    "commissions": DubEndpointConfig(name="commissions", path="/commissions", pagination="cursor"),
    "payouts": DubEndpointConfig(name="payouts", path="/payouts", pagination="page"),
    "partner_applications": DubEndpointConfig(
        name="partner_applications", path="/partners/applications", pagination="page"
    ),
    # Dub rejects a /partners/analytics request that names no partner, so there is no
    # program-wide series to read: the table is the union of each partner's series. It carries
    # earnings per bucket, which neither /partners nor the workspace-level /analytics reports.
    "partner_analytics_timeseries": DubEndpointConfig(
        name="partner_analytics_timeseries",
        path="/partners/analytics",
        pagination="single",
        primary_keys=("partnerId", "start"),
        partition_key=None,
        params={"groupBy": "timeseries", "interval": "all"},
        partner_scoped=True,
    ),
    "analytics_timeseries": _analytics_endpoint("analytics_timeseries", "timeseries", ("start",)),
    "analytics_continents": _analytics_endpoint("analytics_continents", "continents", ("continent",)),
    "analytics_countries": _analytics_endpoint("analytics_countries", "countries", ("country",)),
    # Dub repeats the whole geo path on every row, and region and city names recur across
    # countries, so the narrower cuts need the parent columns in the key to stay unique.
    "analytics_regions": _analytics_endpoint("analytics_regions", "regions", ("country", "region")),
    "analytics_cities": _analytics_endpoint("analytics_cities", "cities", ("country", "region", "city")),
    "analytics_devices": _analytics_endpoint("analytics_devices", "devices", ("device",)),
    "analytics_browsers": _analytics_endpoint("analytics_browsers", "browsers", ("browser",)),
    "analytics_os": _analytics_endpoint("analytics_os", "os", ("os",)),
    "analytics_referers": _analytics_endpoint("analytics_referers", "referers", ("referer",)),
    "analytics_triggers": _analytics_endpoint("analytics_triggers", "triggers", ("trigger",)),
}

ENDPOINTS = tuple(DUB_ENDPOINTS.keys())

# Only the /events streams expose a genuine server-side timestamp filter (start/end) with a
# stable ascending sort, so they're the only incremental candidates. Commissions document a
# start/end filter too, but it keys on createdAt while commission status keeps mutating after
# creation (pending -> processed -> paid), so an incremental sync would freeze stale statuses —
# those stay full refresh.
INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
    "click_events": [incremental_field("timestamp")],
    "lead_events": [incremental_field("timestamp")],
    "sale_events": [incremental_field("timestamp")],
}

# Endpoints gated by Dub plan or product setup: /events needs a Business plan or higher,
# /payouts needs a Business partner-program plan, and /partners + /commissions require a
# partner program. Used by get_endpoint_permissions to surface per-table reachability.
PLAN_GATED_ENDPOINTS = (
    "click_events",
    "lead_events",
    "sale_events",
    "partners",
    "commissions",
    "payouts",
    "partner_applications",
    "partner_analytics_timeseries",
)

# These resolve the workspace's default partner program before they read anything, so a
# workspace without Dub Partners gets 404 not_found on the list endpoint itself rather than a
# plan error. Their paths are static and always valid, so a 404 here can only mean "no program".
PARTNER_PROGRAM_ENDPOINTS = (
    "partners",
    "commissions",
    "payouts",
    "partner_applications",
    "partner_analytics_timeseries",
)