DM8: Advertising through code: buying, automating and measuring ads
Automating ads through APIs, safely
🎯You'll be able to
Explain what ad platform APIs can do, design a safe automation (reports, rules, bulk changes), and protect credentials.
🏠The everyday picture
A shopkeeper who lets an assistant reorder stock sets limits: "Order up to 50 packs; ask me if it's more; write every order in the book." Code that changes ad budgets needs the same limits.
📘What it really is
- Ad platform APIs (for example the Google Ads API and Meta's Marketing API) let code:
- pull performance reports (daily spend, clicks, conversions by campaign);
- create and update campaigns, ads, keywords, budgets and bids;
-
upload offline conversions (orders confirmed by the shop's own system).
-
Access needs an account, an approved developer access level or app review, and OAuth tokens. Not verified in this kit: these need real accounts.
-
Common automations: a daily report to a sheet or dashboard; rules ("pause ads for products out of stock"); bulk changes (raise budgets for festival campaigns); alerts ("spend 50% above yesterday").
-
Safety for code that spends money:
- dry run first: print the changes without applying them;
- limits: no budget change above a set percentage, a daily cap on total changes;
- a change log of every change, its reason and its author;
- approval for large changes;
- test accounts where the platform offers them;
-
idempotency: running the script twice shouldn't double a change.
-
Credentials: store tokens in a secrets manager or environment variables, not in code or spreadsheets; give the narrowest access.
- Rate limits and errors: APIs limit requests; handle errors and partial failures (Module 24).
📝A worked example
Input: a rule: "Pause ads for any product that's out of stock; resume when it's back."
Steps:
- Each morning, read stock levels from the shop's system and the ads' product IDs from the API.
- Build a list of changes; in dry-run mode, print it ("pause: ad 31 (tawa), stock 0").
- Apply at most 20 changes per run, log each one, and alert a person if more are needed.
Expected output: a design (in your capstone notes, as pseudocode) with a dry run, a change limit, a log and an alert. (Not verified: no real ad account is used.)
🌍Where it's used
- Large advertisers pull API reports into their own data warehouses.
- Retailers pause ads automatically when items sell out.
- Agencies apply bulk budget changes across many accounts before festivals.
📝Case scenario
A travel company's script raised bids by 10% every hour for campaigns below target, with no upper limit. A tracking outage made every campaign look below target overnight; by morning bids were five times higher and the monthly budget was gone. The rewrite added a cap, a dry run and an alert when conversions dropped to zero.
🖱️Try it
Write, in plain words or pseudocode, a script that emails a daily spend report and alerts if any campaign's spend is more than 50% above its seven-day average. Success: it includes what happens if the API call fails.
⚠️Common mistakes
- No limits on spending code. Sign: a script that can raise budgets without a cap. Fix: limits, dry runs and alerts.
- Tokens in code. Sign: an API token pasted into a shared script. Fix: a secrets manager and narrow access.
- No log. Sign: nobody knows why budgets changed. Fix: log every change with its reason.
🛠️For your project
Your capstone's design notes describe one ad automation with its safety controls; it's a design, not something to run against a real account.
✅Quick check
Name two safety controls for a script that changes ad budgets.
Show the answer
For example a dry run, a cap on changes, a change log, approval for large changes, or an alert.
Where should an ads API token be kept?
Show the answer
In a secrets manager or environment variable, not in code or shared sheets.
