GitHunt
TA

Task Overview and explnantion on how it all works

This follwoing project integrates with the Snapchat Ads API to automate campaign management.

It identifies and pauses any campaign in the specified Snapchat ad account if its Return on Ad Spend (ROAS) for short , is below £1 over the past 30 days (excluding today).

Requirements Recap

  1. Fetch campaign performance for the last 30 days (excluding today).
  2. Identify campaigns with ROAS < £1.
  3. Pause those campaigns automatically.
  4. Implement tests (optional) that validate ROAS calculation & logic.

High-Level Design Decisions

  1. Important fucntion breakdowns...

    • get_30day_date_range(): Computes the ISO-formatted date range for the last 30 days.
    • fetch_campaign_stats(): Calls the Snapchat Ads API to fetch campaign performance.
    • calculate_roas(): Dedicated function to compute ROAS from micro-values of spend and purchases.
    • pause_campaign(): Issues a PUT request to pause a specific campaign.
    • main(): Orchestrates the entire workflow: fetching data, computing ROAS, and deciding whether to pause campaigns.
  2. Principle: Error Handling

    • Uses try/except blocks around network calls to gracefully handle API or connectivity issues.
    • Handles missing or unexpected response structures with caution
  3. Principle: Readability & Modularity

    • Code is separated into logical functions, each with descriptive naming.
    • This modular design allows for targeted testing and simplifies future code changes.
  4. Principle: Minimizing External Calls

    • Only a single API call is made to fetch stats for all campaigns (using breakdown=campaign), preventing multiple calls per campaign.
    • Reduces unnecessary network overhead and potential rate-limit issues.
  5. Testing Strategy

    • Tests located in tests/test_main.py use Python’s unittest framework.
    • Focus on verifying the ROAS calculation with different spend/purchase values (including zero spend).
    • While these tests do not mock the full API, they ensure core business logic (ROAS) is correct.

How to run it

  1. Set Environment Variables
    Ensure you have a valid Snapchat Ads API token:
    export SNAPCHAT_ACCESS_TOKEN="YOUR_SNAPCHAT_API_TOKEN"

  2. Install Dependencies
    pip install -r requirements.txt

  3. Run the Script
    python main.py

  4. Run the Tests
    python -m unittest discover tests

Languages

Python100.0%

Contributors

Created March 13, 2025
Updated March 13, 2025
tadiwa-aizen/media | GitHunt