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
- Fetch campaign performance for the last 30 days (excluding today).
- Identify campaigns with ROAS < £1.
- Pause those campaigns automatically.
- Implement tests (optional) that validate ROAS calculation & logic.
High-Level Design Decisions
-
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 aPUTrequest to pause a specific campaign.main(): Orchestrates the entire workflow: fetching data, computing ROAS, and deciding whether to pause campaigns.
-
Principle: Error Handling
- Uses
try/exceptblocks around network calls to gracefully handle API or connectivity issues. - Handles missing or unexpected response structures with caution
- Uses
-
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.
-
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.
- Only a single API call is made to fetch stats for all campaigns (using
-
Testing Strategy
- Tests located in
tests/test_main.pyuse Python’sunittestframework. - 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.
- Tests located in
How to run it
-
Set Environment Variables
Ensure you have a valid Snapchat Ads API token:
export SNAPCHAT_ACCESS_TOKEN="YOUR_SNAPCHAT_API_TOKEN" -
Install Dependencies
pip install -r requirements.txt -
Run the Script
python main.py -
Run the Tests
python -m unittest discover tests