# Component 4: Slack Reporter

**Type:** Standalone Service
**Priority:** P2
**Status:** Not Started

## Goal

Automated GA4 reports delivered to Slack channels.

## Features

| Report | Schedule | Content |
|--------|----------|---------|
| Daily Summary | 9:00 AM | Users, sessions, top 5 pages |
| Weekly Digest | Monday 9 AM | WoW comparison, trends |
| Anomaly Alert | Real-time | Traffic ±30% from baseline |

## Structure

```
04-slack-reporter/
├── README.md
├── config.yaml              # Configuration
├── reporter.py              # Main service
├── queries/
│   ├── daily_summary.py
│   ├── weekly_digest.py
│   └── anomaly_check.py
├── templates/
│   └── slack_blocks.py      # Slack Block Kit
├── requirements.txt
├── Dockerfile
└── docker-compose.yml
```

## Configuration

### config.yaml

```yaml
slack:
  bot_token: ${SLACK_BOT_TOKEN}
  default_channel: "#analytics-reports"

ga4:
  property_id: "123456789"
  credentials_path: "/path/to/credentials.json"

reports:
  daily_summary:
    enabled: true
    schedule: "0 9 * * *"  # 9 AM daily
    channel: "#analytics-reports"

  weekly_digest:
    enabled: true
    schedule: "0 9 * * 1"  # 9 AM Monday
    channel: "#analytics-reports"

  anomaly_alert:
    enabled: true
    check_interval: 3600  # Check every hour
    threshold: 0.3        # 30% deviation
    channel: "#analytics-alerts"
```

## Slack App Setup

1. Go to [api.slack.com/apps](https://api.slack.com/apps)
2. Create New App → From scratch
3. Add OAuth scopes:
   - `chat:write`
   - `files:write`
   - `channels:read`
4. Install to workspace
5. Copy Bot Token (`xoxb-...`)

## Dependencies

```
# requirements.txt
google-analytics-data>=0.18.0
google-auth>=2.23.0
slack-sdk>=3.23.0
apscheduler>=3.10.0
pandas>=2.0.0
plotly>=5.18.0
kaleido>=0.2.1
pyyaml>=6.0
```

## Slack Message Format

Using Block Kit for rich formatting:

```python
def daily_summary_blocks(data: dict) -> list:
    return [
        {
            "type": "header",
            "text": {"type": "plain_text", "text": "📊 Daily GA4 Summary"}
        },
        {
            "type": "section",
            "fields": [
                {"type": "mrkdwn", "text": f"*Users:* {data['users']:,}"},
                {"type": "mrkdwn", "text": f"*Sessions:* {data['sessions']:,}"},
                {"type": "mrkdwn", "text": f"*Pageviews:* {data['pageviews']:,}"},
                {"type": "mrkdwn", "text": f"*Bounce Rate:* {data['bounce_rate']:.1%}"},
            ]
        },
        {"type": "divider"},
        {
            "type": "section",
            "text": {"type": "mrkdwn", "text": "*Top Pages:*\n" + data['top_pages']}
        }
    ]
```

## Deployment

### Local Development

```bash
# Install dependencies
pip install -r requirements.txt

# Set environment variables
export SLACK_BOT_TOKEN=xoxb-...
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json

# Run
python reporter.py
```

### Docker

```bash
docker-compose up -d
```

### Cloud Options

- Google Cloud Run (scheduled via Cloud Scheduler)
- AWS Lambda + EventBridge
- Railway / Render

## Checklist

- [ ] Slack App created
- [ ] config.yaml template
- [ ] daily_summary.py
- [ ] weekly_digest.py
- [ ] anomaly_check.py
- [ ] slack_blocks.py templates
- [ ] reporter.py scheduler
- [ ] Dockerfile
- [ ] Tested locally
- [ ] Deployed
