Learning PathLesson 4 of 7 · Claude Code for Analysts
Claude Code for Analysts · Lesson 4 of 7intermediate12 min read

Connecting to Data Sources (APIs, Databases, CSVs)

Move beyond sample data. Use Claude Code to connect to GA4 APIs, read from databases, and process CSV files programmatically — turning your dashboard into a live data product.

Sample Data Was Training Wheels. Now We Go Live.

In the last lesson, you built a dashboard with realistic-looking sample data. That's great for prototyping. But the real power comes when your dashboard shows actual numbers — live data from GA4, your CRM, your ad platforms, or the CSV your finance team sends every week. Claude Code can connect to all of these.

Connecting to the GA4 API

Google Analytics 4 has an API that lets you pull data programmatically — the same data you see in the GA4 dashboard, but available for your scripts and tools to use. Here's how you'd ask Claude Code to set this up:

Prompt Example
claude

Claude Code will create the script, set up the authentication flow, and handle the GA4 API's specific query format — all things that would normally require reading Google's documentation for hours.

Set up a Python script that connects to the GA4 API using a service account. Pull sessions, users, bounce rate, and conversion rate broken down by source/medium for the last 30 days. Store the credentials path in an environment variable, not hardcoded. Output the results as a pandas DataFrame and save to a CSV.
.envbash
# .env file — Claude Code will create this for you
GA4_PROPERTY_ID=properties/123456789
GOOGLE_APPLICATION_CREDENTIALS=./credentials/ga4-service-account.json

# NEVER commit this file to git
# Claude Code will add it to .gitignore automatically
Watch Out
Never commit API keys, service account files, or .env files to git. This is the number one security mistake beginners make. Claude Code knows this and will set up .gitignore correctly, but always double-check. If a credential ever accidentally gets committed, rotate it immediately — treat it as compromised.

Reading from Databases

If your company stores data in a database (PostgreSQL, MySQL, BigQuery), Claude Code can write scripts to query it directly. You don't need to know SQL perfectly — describe what data you want and Claude Code writes the query:

Prompt Example
claude

Claude Code writes the SQL, handles the database connection, and structures the output — you just describe the data you need.

Write a Python script that connects to our PostgreSQL database and pulls all orders from the last 90 days with customer email, order total, UTM source, and product name. Group the results by UTM source and calculate total revenue, average order value, and customer count for each source. Use environment variables for the database connection string.

Processing CSV Files Like a Pro

Let's be real: a huge amount of marketing data still lives in CSV exports. The finance team sends a spreadsheet. Your ad platform exports a CSV. Your survey tool dumps results into a file. Claude Code can process all of these:

merge_and_analyze.pypython
# Example: Claude Code generates this when you ask it to
# "Clean up my ad spend CSV and merge it with my revenue data"
import pandas as pd

# Load the exports
ad_spend = pd.read_csv('data/meta-ad-spend-q1.csv')
revenue = pd.read_csv('data/stripe-revenue-q1.csv')

# Clean column names (they're always messy)
ad_spend.columns = ad_spend.columns.str.strip().str.lower().str.replace(' ', '_')
revenue.columns = revenue.columns.str.strip().str.lower().str.replace(' ', '_')

# Merge on campaign name
merged = ad_spend.merge(revenue, on='campaign_name', how='left')

# Calculate ROAS
merged['roas'] = merged['revenue'] / merged['spend']

# Sort by ROAS descending
result = merged.sort_values('roas', ascending=False)
result.to_csv('output/campaign-roas-analysis.csv', index=False)
print(f'Analysis complete. {len(result)} campaigns processed.')

Environment Variables: The Key to Safe Connections

Environment variables are how developers store sensitive information (API keys, database passwords) without putting them directly in code. Think of them as a locked drawer next to your desk — your code knows to check the drawer for the password, but the password itself isn't written on the whiteboard for everyone to see.

environment-setup.pypython
# Create a .env file in your project root
GA4_PROPERTY_ID=properties/123456789
DATABASE_URL=postgresql://user:password@host:5432/dbname
STRIPE_API_KEY=sk_live_your_key_here

# In your Python code, access them safely
import os
from dotenv import load_dotenv

load_dotenv()
db_url = os.getenv('DATABASE_URL')
Pro Tip
When you ask Claude Code to connect to any data source, always mention 'use environment variables for credentials.' Claude Code will typically do this by default, but being explicit ensures your secrets stay safe. It's the one instruction worth repeating every time.

At this point, you can build dashboards and feed them real data. That alone puts you ahead of 90% of marketing analysts. In the next lesson, we'll make it even better: automating reports so they run themselves.

Get weekly job alerts

Curated marketing analytics roles — delivered every Monday.