# 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)
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.