/ Blog
Blog Contact Buddy Ads Builder Audit Engine GitHub

How-To Guide 6: Connect GA4 to AI for Automated Insights

John Williams · Senior Paid Media Specialist · $48M+ Managed · Feb 2026

What will I learn about how-to guide 6: connect ga4 to ai for automated in?
This covers everything you need to know about how-to guide 6: connect ga4 to ai for automated insights.
# pip install google-analytics-data anthropic

from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import RunReportRequest
import anthropic

# Setup: Create service account in Google Cloud Console
# Enable Google Analytics Data API
# Add service account email as viewer in GA4 property

client = BetaAnalyticsDataClient()

request = RunReportRequest(
property=f'properties/YOUR_GA4_PROPERTY_ID',
date_ranges=[{'start_date': '30daysAgo', 'end_date': 'today'}],
metrics=[
{'name': 'sessions'},
{'name': 'conversions'},
{'name': 'totalRevenue'},
{'name': 'bounceRate'},
],
dimensions=[
{'name': 'sessionSourceMedium'},
]
)
response = client.run_report(request)

# Format for AI analysis
data = 'Source/Medium | Sessions | Conversions | Revenue | Bounce Rate\n'
for row in response.rows:
data += ' | '.join([d.value for d in row.dimension_values] +
[m.value for m in row.metric_values]) + '\n'

# Send to Claude
ai = anthropic.Anthropic()
insight = ai.messages.create(model='claude-sonnet-4-20250514',max_tokens=1500,
messages=[{'role':'user','content':f'Analyze GA4 traffic sources. Identify top
opportunities and underperforming channels:\n{data}'}])
print(insight.content[0].text)

Ready to Put This Into Practice?

Get a free 30-day audit of your advertising accounts. John will personally review your setup and provide actionable recommendations.

No credit card · No contract · John will personally reach out within 24 hours

Thank You!

John will review your account and reach out within 24 hours.