What you’ll learn: How to build an automated anomaly detection system, generate AI-powered performance summaries, and create reporting that surfaces insights instead of just metrics. Includes working code and Google Sheets integration.
The IAB’s State of Data 2025 report identified a fundamental problem: 49% of marketers find campaign management significantly more difficult than in previous years, and measurement complexity is the primary driver. Traditional dashboards show you what happened. AI-powered measurement tells you why it happened, whether it matters, and what to do about it.
This Python script checks your Google Ads data against statistical baselines and flags anything unusual. It runs daily and writes to a Google Sheet:
import numpy as np
from google.ads.googleads.client import GoogleAdsClient
import gspread
def detect_anomalies(campaign_data, metric, std_multiplier=2):
'''Flag values that deviate more than N standard deviations.'''
values = [row[metric] for row in campaign_data]
mean = np.mean(values)
std = np.std(values)
anomalies = []
for row in campaign_data:
z_score = (row[metric] - mean) / std if std > 0 else 0
if abs(z_score) > std_multiplier:
anomalies.append({
'campaign': row['name'],
'metric': metric,
'value': row[metric],
'expected': f'{mean:.2f} +/- {std:.2f}',
'deviation': f'{z_score:.1f} standard deviations',
'direction': 'above' if z_score > 0 else 'below'
})
return anomalies
# Check multiple metrics
for metric in ['cpa', 'conversion_rate', 'ctr', 'impression_share']:
anomalies = detect_anomalies(data, metric)
for a in anomalies:
# Write to Google Sheet and send alert
sheet.append_row([date, a['campaign'], a['metric'],
a['value'], a['expected'], a['deviation']])
Instead of manually writing weekly reports, feed your data to Claude and get a client-ready summary:
import anthropic
def generate_weekly_summary(this_week, last_week, anomalies):
client = anthropic.Anthropic()
response = client.messages.create(
model='claude-sonnet-4-20250514',
max_tokens=2000,
system='''You are a senior account manager writing a weekly
report for a client. Be concise, lead with insights not data,
and always include recommended next steps. Use plain language,
no jargon. Maximum 400 words.''',
messages=[{
'role': 'user',
'content': f'''Generate a weekly performance summary.
This week: {this_week}
Last week: {last_week}
Anomalies detected: {anomalies}
Structure:
1. Executive headline (one sentence)
2. Three most important changes and what caused them
3. Actions taken this week
4. Recommended actions for next week
5. One insight the client wouldn't see in a dashboard'''
}]
)
return response.content[0].text
import gspread
from google.oauth2.service_account import Credentials
# Setup (one-time):
# 1. Create a Google Cloud service account
# 2. Download the JSON key file
# 3. Share your Google Sheet with the service account email
scopes = ['https://www.googleapis.com/auth/spreadsheets']
creds = Credentials.from_service_account_file('creds.json', scopes=scopes)
gc = gspread.authorize(creds)
# Write AI summary to sheet
sheet = gc.open('Weekly Reports').worksheet('AI Summary')
sheet.update('A1', [[date, summary]])
# Write anomalies to separate tab
anomaly_sheet = gc.open('Weekly Reports').worksheet('Anomalies')
for a in anomalies:
anomaly_sheet.append_row([a['date'], a['campaign'],
a['metric'], a['deviation']])
GoogleAdsAgent.ai’s Performance Reporting sub-agent runs this exact stack continuously—anomaly detection, AI-powered interpretation, and proactive alerting—but with 15 years of pattern recognition encoded in its evaluation logic. It knows that a 20% CPA spike on a Monday after a holiday weekend is normal. It knows that a sudden impression share drop in a brand campaign means a competitor launched a conquest campaign. It knows the difference between noise and signal because its creator has seen both, thousands of times.
📦 GitHub: https://github.com/itallstartedwithaidea/google_ads_anomoly_detection_script — The anomaly detection Google Ads Script that monitors accounts 24/7
📦 GitHub: https://github.com/itallstartedwithaidea/google_ads_impressionshare_script — Impression share tracking for competitive monitoring
Website: https://googleadsagent.ai | GitHub: https://github.com/itallstartedwithaidea | Tools: https://googleadsagent.ai/tools
About the Author
John Williams is a Senior Paid Media Specialist at Seer Interactive with 15+ years managing $48M+ in digital ad spend across Google, Microsoft, Meta, and Amazon. Founder of It All Started With A Idea and creator of GoogleAdsAgent.ai. Speaker at Hero Conf on AI in advertising. Former WSU football player and current assistant football coach at Casteel High School, AZ.
Get a free 30-day audit of your advertising accounts. John will personally review your setup and provide actionable recommendations.
John will review your account and reach out within 24 hours.