: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from typing import Optional, cast
from products.warehouse_sources.sources.awin._config import AwinSourceConfig
from products.warehouse_sources.sources.awin.awin import (
AwinResumeConfig,
awin_source,
validate_credentials as validate_awin_credentials,
)
from products.warehouse_sources.sources.awin.settings import (
AWIN_ENDPOINTS,
DEFAULT_REGION,
ENDPOINTS,
INCREMENTAL_FIELDS,
REGION_OPTIONS,
)
from products.warehouse_sources.sources.sdk import (
CanonicalDescriptions,
DataWarehouseSourceCategory,
ExternalDataSourceType,
FieldType,
ReleaseStatus,
ResumableSource,
ResumableSourceManager,
SourceConfig,
SourceFieldInputConfig,
SourceFieldInputConfigType,
SourceFieldSelectConfig,
SourceFieldSelectConfigOption,
SourceInputs,
SourceRegistry,
SourceResponse,
SourceSchema,
detect,
)
@SourceRegistry.register
class AwinSource(ResumableSource[AwinSourceConfig, AwinResumeConfig]):
codebase_detection = detect(env=("^AWIN_",), script_hosts=("www.dwin1.com",))
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
api_docs_url = "https://developer.awin.com/apidocs"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.AWIN
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.AWIN,
category=DataWarehouseSourceCategory.ADVERTISING,
label="Awin",
releaseStatus=ReleaseStatus.GA,
caption="""Enter your Awin API token to pull your Awin affiliate data into the PostHog Data warehouse.
Create a personal OAuth2 token from the [Awin API settings](https://ui.awin.com/awin-api). The same token grants access to every publisher account your user can see.""",
iconPath="/static/services/awin.png",
docsUrl="https://posthog.com/docs/cdp/sources/awin",
fields=cast(
list[FieldType],
[
SourceFieldInputConfig(
name="api_token",
label="API token",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="",
secret=True,
),
SourceFieldSelectConfig(
name="region",
label="Region",
required=True,
defaultValue=DEFAULT_REGION,
caption="Your Awin account's primary region. Used to sync the performance reports.",
options=[
SourceFieldSelectConfigOption(label=label, value=value) for value, label in REGION_OPTIONS
],
),
],
),
)
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.awin.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.awin.com": "Your Awin API token is invalid or has expired. Create a new token in your Awin API settings, then reconnect.",
"403 Client Error: Forbidden for url: https://api.awin.com": "Your Awin API token does not have access to this data. Check the token's account permissions, then reconnect.",
}
def get_schemas(
self,
config: AwinSourceConfig,
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 == "reports_advertiser":
return "Full refresh only. A rolling snapshot of the last 30 days of performance, aggregated per advertiser"
if endpoint == "reports_publisher":
return "Full refresh only. A rolling snapshot of the last 30 days of performance, aggregated per publisher. Needs an advertiser account"
if endpoint == "advertiser_publishers":
return "Full refresh only. Needs an advertiser account"
if endpoint in ("commission_groups", "programme_details"):
return "Full refresh only. Takes one request per joined programme, so it syncs slowly if your publisher accounts are in many programmes"
if endpoint in ("publisher_campaign_reports", "publisher_creative_reports"):
return "Full refresh only. A rolling snapshot of the last 30 days of performance for your publisher accounts"
if endpoint in ("advertiser_campaign_reports", "advertiser_creative_reports"):
return "Full refresh only. A rolling snapshot of the last 30 days of performance. Needs an advertiser account"
if endpoint == "offers":
return "Full refresh only. Active offers and voucher codes from the programmes your publisher accounts have joined"
if endpoint in ("transactions", "transaction_queries"):
return "Only syncs the last 365 days on initial sync"
return None
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=AWIN_ENDPOINTS[endpoint].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: AwinSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
) -> tuple[bool, str | None]:
if validate_awin_credentials(config.api_token):
return True, None
return False, "Invalid Awin API token"
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[AwinResumeConfig]:
return ResumableSourceManager[AwinResumeConfig](inputs, AwinResumeConfig)
def source_for_pipeline(
self,
config: AwinSourceConfig,
resumable_source_manager: ResumableSourceManager[AwinResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
return awin_source(
api_token=config.api_token,
endpoint=inputs.schema_name,
logger=inputs.logger,
resumable_source_manager=resumable_source_manager,
region=config.region,
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,
incremental_field=inputs.incremental_field,
)