Create custom MCP server for Google Analytics 4 - without code
Hoshang Mehta
Create custom MCP server for Google Analytics 4 - without code
Google Analytics 4 (GA4) is where your website and app traffic data lives. It tracks user sessions, page views, conversions, and traffic sources. But when you want to build an AI agent that can answer "What's our top traffic source this month?" or "How many users converted from the blog?", you hit complexity.
The GA4 API requires understanding dimensions, metrics, and complex query structures. Building a custom backend just to expose analytics data to your agent is time-consuming.
Pylar simplifies this.
Pylar syncs your GA4 data to a SQL-queryable warehouse and lets you build MCP (Model Context Protocol) tools with natural language prompts—no code, no API complexity.
Why Pylar for Google Analytics 4?
- SQL Over GA4: Query your analytics data with standard SQL instead of learning GA4's Data API syntax.
- Unified Marketing View: Join GA4 traffic data with HubSpot leads or Stripe revenue to understand ROI.
- Historical Analysis: Run complex queries on historical data without API limits.
Step-by-Step Walkthrough
Step 1: Connect Google Analytics 4 to Pylar
- In Pylar, go to Connections -> Google Analytics 4.
- Authenticate via OAuth (Google login) and select your GA4 property.
- Pylar will begin syncing your GA4 data (sessions, events, conversions, traffic sources, etc.).

You'll receive an email when the sync completes.
Step 2: Create a Sandboxed View
Let's say you want an agent to help your marketing team understand traffic sources.
In Pylar's SQL IDE, create a view:
-- view: traffic_source_summary
SELECT
traffic_source,
medium,
campaign,
COUNT(DISTINCT user_pseudo_id) as unique_users,
COUNT(*) as sessions,
SUM(CASE WHEN event_name = 'purchase' THEN 1 ELSE 0 END) as conversions,
DATE_TRUNC('day', event_timestamp) as date
FROM ga4.events
WHERE event_timestamp > NOW() - INTERVAL '30 days'
GROUP BY traffic_source, medium, campaign, DATE_TRUNC('day', event_timestamp)

This view gives the agent a clean summary of traffic performance by source.
Step 3: Auto-Build the MCP Tool
- Select your view (
traffic_source_summary) in the right sidebar. - Click "Create MCP Tool" and choose "Create with AI".
- Type a prompt: "Create a tool that shows traffic and conversions by source and campaign."
- Pylar generates the tool configuration:
- Name:
get_traffic_by_source - Description: "Returns traffic metrics and conversions by source, medium, and campaign."
- Parameters:
traffic_source(string),campaign(string)
- Name:

Step 4: Publish & Connect
- Click "Publish" in the right sidebar.
- Click "Generate Token".
- Copy your MCP HTTP Stream URL (
https://mcp.publish.pylar.ai/mcp) and Authorization Bearer Token.

Connect to Marketing Dashboard: You can now build a custom marketing dashboard that lets your team query GA4 data via natural language.
from langchain_mcp import MCPTool
ga4_tool = MCPTool(
url="https://api.pylar.ai/mcp/v1/server/YOUR_SERVER_ID",
api_key="YOUR_API_KEY"
)
# Agent can now answer: "What's our top traffic source this week?"
Advanced Use Cases for GA4 Agents
1. Content Performance Analysis
Goal: Identify which blog posts or landing pages drive the most conversions.
- View: Aggregate events by
page_pathand calculate conversion rates. - Tool Prompt: "Create a tool that ranks pages by conversion rate."
- User Query: "Which landing page has the highest conversion rate?"
2. Campaign ROI Calculator
Goal: Help marketing teams understand which campaigns are worth the investment.
- View: Join GA4 conversions with Google Ads spend data.
- Tool Prompt: "Create a tool that calculates ROI for a specific campaign."
- User Query: "What's the ROI for the 'Summer Sale' campaign?"
Conclusion
Google Analytics 4 is the foundation of modern marketing analytics, but giving AI agents access to it has always required complex API integrations and careful handling of data processing delays. With Pylar, you can transform your GA4 data into an AI-ready resource in under 2 minutes—no coding required.
By connecting Pylar to your GA4 properties (including multiple properties for agencies), creating sandboxed views that aggregate event data, and auto-generating MCP tools with natural language, you unlock the full potential of your marketing analytics for AI agents while maintaining complete security and governance.
Whether you're building a conversion rate analyzer, a campaign ROI calculator, or any other GA4-powered AI application, Pylar gives you the governance layer you need. Your views act as security boundaries, and with support for all custom events, you can query your analytics data just like any SQL database.
Turn your Google Analytics 4 data into an AI-ready resource. Start building marketing intelligence agents today.
