Sub topic 4
Data sources: ad platforms, analytics, CRM and the web
This chapter assumes you know basic marketing metrics (impressions, clicks, conversions, spend), can read short Python, and are comfortable with JSON and simple APIs. It covers the four kinds of data an agent reads, how each one goes wrong, and how to put them in a shape the agent's tools can use. Later chapters cover the tools themselves, retrieval, and privacy in more detail.
Why the data source comes first
An agent's report is only as good as the data its tools return. The model does not know your campaign results. It sees only what a tool hands back. If a tool returns spend in the wrong unit, or conversions that are still being counted, the report will state the wrong number confidently.
So before building tools, decide which systems the agent may read, what each one measures, and how fresh it is.
The four source families
| Source | Typical examples | What it tells you | Common access method |
|---|---|---|---|
| Ad platforms | Google Ads, Meta Ads, LinkedIn Ads | Impressions, clicks, spend, platform-reported conversions | Reporting API or scheduled export |
| Web analytics | Google Analytics 4, Adobe Analytics | Sessions, events, on-site conversions, traffic source | Reporting API or warehouse export |
| CRM | Salesforce, HubSpot | Leads, opportunities, revenue, customer attributes | REST API or warehouse sync |
| The web | Competitor sites, news, review pages, public filings | Market context, pricing, messaging | Search API, fetching pages, licensed feeds |
Each family answers a different question. Ad platforms tell you what you paid and what the platform claims it produced. Analytics tells you what visitors did on your site. The CRM tells you which leads became revenue. The web tells you what is happening outside your own systems.
Ad platform data
Ad platform APIs return per-campaign, per-day rows. They are the most structured source, which makes them the easiest to automate. They also have quirks:
- Units differ. Google Ads reports cost in micros, which are millionths of the account currency. A
cost_microsof 150000000 means 150.00. Meta's Insights API returnsspendandimpressionsas strings. - Conversions are platform-defined. Each platform counts conversions using its own attribution window and its own definition of a conversion action. Two platforms can both claim credit for the same sale.
- Recent days change. Conversions are often reported late. Yesterday's numbers can rise for days afterward. A weekly report that includes the last two days will understate conversions.
- Currency and time zone are account settings. Two accounts in one report may use different currencies and different day boundaries.
Web analytics data
Analytics measures behavior on your own site, so it is the closest thing to a neutral check on platform claims. It rarely agrees with them exactly. Analytics counts sessions, not clicks. It depends on tags firing and on users accepting cookies, and it applies its own attribution model. A gap between platform clicks and analytics sessions is normal. A sudden change in that gap often means a broken tag or a tracking change.
Large analytics queries may also return sampled or aggregated results, depending on the product and the size of the query. A tool should record whether the result was sampled, so the report can say so.
CRM data
The CRM connects marketing activity to revenue, which makes it the source that matters most for return on spend. It is also the riskiest:
- It holds personal data such as names, emails and phone numbers. An agent that only needs counts and totals should never receive those fields.
- It reflects human data entry. Lead source fields are often blank, inconsistent, or overwritten by sales staff.
- Revenue arrives weeks or months after the click. A report on this month's campaigns will show incomplete revenue.
The usual fix is a read-only view or a warehouse table that already contains only aggregated, non-identifying columns. The agent's tool queries that view, not the raw CRM.
Web research data
The open web is the least structured source. An agent can search it and read pages, but the content is uneven. Treat it differently from your own data:
- Record the URL and the retrieval date with every fact. Pages change and disappear.
- Respect each site's terms of use and
robots.txt. Prefer official APIs and licensed feeds over scraping. - Treat page text as untrusted. A page can contain text that looks like instructions to the model. The agent should extract facts from it, not obey it.
- Prefer primary sources (a company's own filing or pricing page) over summaries of them.
Normalizing into one shape
Because each source uses different names and units, add a connector layer that converts everything to one schema before the agent sees it. The metrics tool from a later chapter then works on any source without special cases.
from datetime import date, datetime, timezone
def normalize(source, row):
"""Convert one raw platform row into a common schema."""
if source == "google_ads":
spend = row["cost_micros"] / 1_000_000
elif source == "meta":
spend = float(row["spend"])
else:
raise ValueError("unknown source: " + source)
return {
"source": source,
"date": row["date"],
"campaign": row["campaign"],
"impressions": int(row["impressions"]),
"clicks": int(row["clicks"]),
"spend": round(spend, 2),
"conversions": float(row["conversions"]),
"pulled_at": datetime.now(timezone.utc).isoformat(timespec="seconds"),
}
google_row = {
"date": "2026-11-20", "campaign": "brand-search",
"impressions": 10000, "clicks": 200,
"cost_micros": 150_000_000, "conversions": 10.0,
}
meta_row = {
"date": "2026-11-20", "campaign": "retargeting",
"impressions": "5000", "clicks": "50",
"spend": "60.00", "conversions": "2",
}
for source, raw in [("google_ads", google_row), ("meta", meta_row)]:
print(normalize(source, raw))
print("report window ends", date(2026, 11, 20))Three design choices in this code are worth copying:
- Unknown sources fail loudly. A
ValueErroris better than silently treating an unknown source like a known one. - Types are converted once, here. Strings from Meta become numbers before any arithmetic happens.
- Every row carries
sourceandpulled_at. Later, when someone asks where a number came from or how old it is, the answer is in the data. This is the foundation for grounding claims in retrieved sources.
Deciding how the agent reaches the data
There are three common patterns, each with trade-offs.
- Direct API tool. The agent calls a function that queries a platform live. The data is fresh, but you pay for rate limits, slow calls and credentials held by the agent's runtime.
- Warehouse or cache. A scheduled job pulls and normalizes data, and the agent queries the stored table. This is faster, cheaper and easier to audit. The data is only as fresh as the last job. Show the last refresh time in every report.
- File export. A person drops a CSV into a folder. This is the lowest effort and the weakest on freshness and consistency. It suits one-off research.
For recurring reporting, the warehouse pattern is usually the best default. It keeps credentials out of the agent, makes runs repeatable, and lets you re-run last month's report and get the same numbers.
Access and credentials
Give the agent the minimum access it needs:
- Use read-only credentials. A reporting agent has no reason to edit campaigns or change budgets.
- Keep credentials in environment variables or a secrets manager. Never put them in prompts, code, or logs.
- Scope access by account or client, so a report for one client cannot read another's data.
- Log which source, query and time range each tool call used.
Common errors
- Comparing a full week of ad data with a partial week of CRM revenue.
- Mixing currencies or time zones in one table.
- Treating a blank CRM lead source as a real channel called "blank".
- Letting the model read raw CRM records when a summary view would do.
- Reporting web research without a URL or date.
Exercise
A weekly report compares Google Ads spend (account currency USD, reported in micros) with Meta spend (a string such as "60.00") and CRM revenue from the same week. List three problems that could make the totals misleading, and say where in the pipeline you would fix each one.
Show answer
(1) Unit mismatch: Google's costmicros must be divided by 1,000,000 and Meta's string converted to a number. Fix this in the connector's normalize step. (2) Conversion lag and revenue lag: recent days are still being updated and CRM revenue arrives weeks after the click, so the week looks worse than it will end up. Fix this by excluding or labeling the most recent days and recording pulledat so the report states data freshness. (3) Double-counted conversions: each platform attributes the same sale on its own terms, so summing them overstates results. Fix this in the metrics and reporting step by reporting conversions per platform and using CRM or analytics as the independent total. Mixed currencies or time zones, if present, are also fixed in the connector.