How to Connect Google Analytics to CrewAI

Cody Schneider

Giving your AI agents access to real-time data is the key to unlocking their true potential. By connecting Google Analytics to a CrewAI-powered team, you can build autonomous agents that monitor website performance, analyze marketing campaign effectiveness, and surface insights without any manual work. This guide will walk you through the entire process, from setting up your Google Cloud credentials to creating a custom tool your agents can use to fetch live data.

Why Connect Google Analytics to CrewAI?

Before jumping into the technical steps, it helps to understand what this connection makes possible. When your CrewAI agents can access Google Analytics data directly, they can stop relying on static information and start operating on live, real-world metrics. This turns them from general-purpose task-doers into specialized analysts.

Here are a few practical examples of what you can build:

  • An SEO Content Auditor Agent: Create an agent that pulls a list of your top-performing blog posts from the last 30 days, analyzes their titles and traffic sources, and then provides recommendations for new content based on what's already working.

  • A Marketing Campaign Monitor Agent: Task an agent to check traffic from specific UTM campaigns daily. If a campaign's sessions drop below a certain threshold or its bounce rate spikes, the agent can alert you in Slack or email.

  • A Weekly Performance Reporter Agent: Delegate your weekly reporting. An agent can pull key metrics like users, sessions, and conversion rates, compare them to the previous week, and write a summary report for your team.

The goal is to automate the data-gathering and initial analysis stages, freeing you and your team up to focus on strategy and implementation based on the insights your AI agents uncover.

Prerequisites: What You'll Need Before You Start

Setting up this integration involves a few moving pieces. Getting everything in order upfront will make the process much smoother. Here’s what you need:

  • A Python Environment: You should have Python installed on your machine and be comfortable working with a virtual environment and installing packages using pip.

  • CrewAI: You'll need to have the CrewAI library installed. If you don't, you can install it, along with its preferred tools package, by running:pip install 'crewai[tools]'

  • A Google Cloud Platform (GCP) Project: The Google Analytics APIs are managed through GCP. If you don't have a project already, you'll need to create one. It's free to get started.

  • A Google Analytics 4 Property: This guide is for GA4. You need to have administrative access to the GA4 property you want to pull data from.

A Step-by-Step Guide to Connecting the Google Analytics Data API

Ready to build? Let's break this down into four manageable steps: activating the API in Google Cloud, creating secure credentials, granting those credentials access in Google Analytics, and finally, writing the Python code to make it all work.

Step 1: Enable the Google Analytics Data API in Your GCP Project

First, you need to tell Google that you intend to use the GA4 Data API within your cloud project.

  1. Navigate to the Google Cloud Console and select the project you want to use from the dropdown at the top.

  2. In the search bar, type "Google Analytics Data API" and select it from the results.

  3. You'll land on the API's overview page. Click the blue "Enable" button. If it's already enabled, you're good to go.

Step 2: Create a Service Account and JSON Key

You don't want your AI agent logging in with your personal Google account. Instead, you'll create a "service account," which is essentially a robotic user that has specific permissions to act on your behalf.

  1. In the Google Cloud Console, use the search bar to find and navigate to "Service Accounts."

  2. Click "+ Create Service Account" at the top.

  3. Give your service account a descriptive name, like "crewai-analytics-reader," and a description. The "Service account ID" will auto-populate. Click "Create and Continue."

  4. For the "Grant this service account access to project" step, you can generally skip assigning a role here for now. Just click "Continue."

  5. Skip the final step as well and click "Done."

  6. Now, find the service account you just created in the list. Click on the three dots under the "Actions" column and select "Manage keys."

  7. Click "Add Key" > "Create new key."

  8. Choose JSON as the key type and click "Create."

A JSON file containing your service account's private key will automatically download. Treat this file like a password! Do not share it publicly or commit it to a public GitHub repository. Store it in a secure location on your computer. You'll need this file in the Python script later.

Step 3: Grant the Service Account Access in Google Analytics

Your service account now exists, but it has no permission to read your website's analytics data. You need to invite it into your GA4 property just like you would a human team member.

  1. Open the JSON credentials file you just downloaded. Find the line with "client_email" and copy the email address. It will look something like crewai-analytics-reader@your-project-id.iam.gserviceaccount.com.

  2. Open Google Analytics and navigate to the Admin section (the gear icon in the bottom-left).

  3. Ensure you have the correct Account and Property selected. In the "Property" column, click on "Property Access Management."

  4. Click the blue "+" button in the top-right and select "Add users."

  5. Paste the service account's email address into the "Email addresses" field.

  6. Under "Permissions," assign the role of "Viewer." Your agent only needs to read data, so this is the safest and most appropriate permission level. Do not check "Notify new users by email."

  7. Click the "Add" button to save. It may take a few minutes for the permissions to take effect.

Creating a Custom CrewAI Tool for Google Analytics

With the authentication complete, it's time for the fun part: writing the code that allows your CrewAI agents to use this connection. We'll create a custom tool that can query the GA4 Data API.

First, Install the Necessary Python Libraries:

You'll need Google's official client library for the analytics data API and pandas for easier data handling.

Writing the Custom Tool Code:

Now, let's build the GoogleAnalyticsTool. This tool will take a GA4 Property ID and query strings for metrics and dimensions, run a report, and return the formatted data.

Integrating the Tool With Your CrewAI Agents

With your custom tool defined, you can now equip your agents with it. The agent will be able to invoke the GoogleAnalyticsTool to answer questions related to your website's performance.

Here’s a simple example of a task and agent that uses this new capability:

When you run this code, the web_analyst_agent will recognize that the task requires analytics data. It will use the GoogleAnalyticsTool, call the API with the proper dimensions (pagePath) and metrics (sessions), receive the structured data, and format its final answer based on your request.

Final Thoughts

Connecting your autonomous agents to a live data stream like Google Analytics is a powerful step towards building truly useful and intelligent automation. By creating a custom tool, you give CrewAI a window into your business performance, opening up a whole new category of tasks you can delegate, from daily reporting to proactive performance monitoring.

While coding a custom connection offers ultimate flexibility for complex agent-based workflows, we know this level of setup isn't always necessary for your day-to-day reporting needs. At Graphed, we've automated this entire process. We connect directly to tools like Google Analytics in one click, handling all the APIs and authentication for you, so you can simply ask questions in plain English - like "What were my top 5 pages last week?" - and get back live, interactive dashboards instantly.