# Ads Analytics Service Architecture

## Overview

```mermaid
graph TB
    subgraph "Data Sources"
        SERV[ads-serving]
        TRACK[ads-tracking]
    end
    
    subgraph "ads-analytics-service"
        INGEST[Ingestion]
        AGG[Aggregation]
        STORE[(ClickHouse)]
    end
    
    SERV -->|Events| INGEST
    TRACK -->|Conversions| INGEST
    INGEST --> STORE --> AGG
```

## Metrics

| Metric | Formula |
|--------|---------|
| **CTR** | Clicks / Impressions |
| **CPC** | Spend / Clicks |
| **CPM** | (Spend / Impressions) × 1000 |
| **ROAS** | Revenue / Spend |

## Database (ClickHouse)

```sql
CREATE TABLE ad_events (
    event_type Enum8('impression', 'click', 'conversion'),
    ad_id UUID,
    campaign_id UUID,
    cost Decimal(18, 6),
    event_time DateTime
) ENGINE = MergeTree()
ORDER BY (campaign_id, event_time);
```

## Integration

| Service | Type | Purpose |
|---------|------|---------|
| **ads-serving** | RabbitMQ | Events |
| **ads-tracking** | RabbitMQ | Conversions |
