How to Query Google Analytics Data
Your Google Analytics account holds the key to understanding your website's performance, but the standard reports don't always answer your most specific questions. To get deeper insights, you need to query your data directly. This article will show you three different ways to query your Google Analytics data, from easy point-and-click methods to more powerful, advanced techniques.
Why Bother Querying Google Analytics Data?
You might wonder why you need to go beyond the default dashboards in Google Analytics. While the built-in reports like "Traffic acquisition" and "Engagement" are great for a high-level overview, they have limitations when you need to dig deeper.
- Answering Custom Questions: The standard reports are designed to answer common questions, but what if your questions are unique to your business? Queries let you ask anything, like "How many users from our recent email campaign in Germany visited a product page and then made a purchase of over $100?"
- Overcoming Data Sampling: For high-traffic websites, Google Analytics often uses data sampling in its reports to provide a faster response. This means the report is based on a subset of your data, not all of it, which can lead to inaccuracies. Querying your data directly, especially through a BigQuery connection, gives you access to the raw, unsampled data for maximum accuracy.
- Deeper Segmentation: While you can create segments in the standard reports, queries allow for much more complex and layered segmentation. You can combine user attributes, events, and transactional data in ways the standard UI simply can't handle.
- Combining with Other Data: Your GA data becomes incredibly powerful when you combine it with information from other systems. You can join your GA data with sales data from your CRM, ad spend from Facebook Ads, or customer info from Shopify to create a complete picture of your customer journey.
In short, querying moves you from being a passive consumer of pre-made reports to an active analyst who can find specific, actionable insights tailored to your business goals.
Method 1: Using the "Explore" Tab in GA4 (The Built-in Query Builder)
The easiest way to start querying your data without leaving the Google Analytics interface is by using the "Explore" section. Think of it as a guided query builder that lets you drag and drop dimensions and metrics to build custom reports, which Google calls "explorations."
Step-by-Step Guide to Creating a Free Form Exploration
Let’s say you want to see which specific blog posts drove the most user engagement last month. A standard report might show you engagement by page, but an exploration can give you more control.
1. Navigate to the Explore Section
In your GA4 property, click on the Explore icon in the left-hand navigation pane. This will take you to your exploration hub, where you can start a new one from scratch or use a template.
2. Start a "Free form" Exploration
Click on the blank template labeled Free form. This gives you a blank canvas to build your own custom table or visualization.
3. Add Dimensions and Metrics
This is where you tell Google Analytics what data you want to see. The columns on the left are your toolboxes for building the query.
- Dimensions: These are the attributes of your data - the "what" or "who." Things like city, traffic source, or page title.
- Metrics: These are the quantitative measurements - the "how many." Things like sessions, event count, or conversions.
For our example, we need to add the following:
- Click the "+" sign next to Dimensions. Search for and import "Page path and screen class" and "Session source / medium."
- Click the "+" sign next to Metrics. Search for and import "Sessions," "Engaged sessions," and "Event count."
4. Build Your Report by Dragging and Dropping
Now, drag your selected dimensions and metrics into the "Tab Settings" column.
- Drag Page path and screen class to the Rows section.
- Drag Sessions, Engaged sessions, and Event count to the Values section.
Instantly, a table will appear on the right, showing you these metrics for every single page on your site.
5. Filter Your Data for Specific Insights
This list includes every page, but we only want to see blog posts. We can use a filter to narrow the results down, which is a core part of querying.
- In the "Tab Settings" column, scroll down to the Filters section and click "Drop or select dimension or metric."
- Select Page path and screen class.
- In the condition dropdown, select "contains."
- In the value field, enter part of the URL that all your blog posts share, like /blog/.
- Click Apply.
Your table now shows only blog posts, giving you a clear view of which content is most engaging. You just built a custom query without a single line of code.
Method 2: Querying with Looker Studio (The Visual Approach)
If you find the GA4 interface limiting or want to build more permanent, shareable dashboards, Looker Studio (formerly Google Data Studio) is the next logical step. It's a free tool that lets you connect to various data sources, including Google Analytics, and build interactive reports with rich visualizations. Building a chart in Looker Studio is essentially creating a visual query.
You’re still choosing dimensions and metrics and applying filters, but with more flexibility in design and the ability to pull in data from other platforms (like a Google Sheet or Facebook Ads) into the same dashboard.
How it Works
- Connect Your Data: In Looker Studio, choose to create a new blank report. You'll be prompted to select a data source. Choose the "Google Analytics" connector, then authorize your account and select the GA4 property you want to use.
- Build a Chart: Add a chart to the canvas, like a time series chart. In the chart settings panel on the right, you'll see options to select your "Dimension" (e.g., 'Date') and "Metric" (e.g., 'Sessions').
- Apply Filters: Just like in GA4's Explore tab, you can add filters to your chart or the entire report page. You could create a filter to show data only for 'device category = mobile' or 'country = United States.'
Each chart you create sends a query to the Google Analytics API to fetch the precise data you asked for, giving you a shareable, auto-updating dashboard based on your custom queries.
Method 3: Querying GA4 with BigQuery (The Power User Method)
For the ultimate level of power and accuracy, you need to connect Google Analytics to BigQuery. BigQuery is Google's enterprise data warehouse, and you can link it to GA4 for free (within the free tier usage limits) to get a daily export of all your raw, unsampled event-level data.
This is the definitive way to query your data. Instead of being limited by the dimensions and metrics available in the UI or API, you get the raw event logs. This allows you to write SQL (Structured Query Language) queries to get any answer you can think of.
Getting Started with GA4 and BigQuery
- Link GA4 to BigQuery: Go to the Admin section of your GA4 property. Under "Product Links," select "BigQuery Links." Follow the on-screen instructions to link your GA4 property to a Google Cloud project. Once configured, GA4 will start exporting your raw data to BigQuery each day.
- Accessing Your Data: In the Google Cloud Console, navigate to BigQuery. You'll find your GA4 data organized into tables named 'events_YYYYMMDD.'
Writing a Simple SQL Query
Let's say you want to count how many users came from Google organic search yesterday and viewed your pricing page. Writing SQL might seem intimidating, but the logic is straightforward once you understand the structure. You 'SELECT' what you want to see, 'FROM' the table where the data lives, and use 'WHERE' to filter it down.
Here’s what that query might look like:
SELECT
COUNT(DISTINCT user_pseudo_id) AS organic_users_on_pricing_page
FROM
`your-project-id.analytics_XXXXXXX_events_*`
WHERE
event_name = 'page_view'
AND traffic_source.source = 'google'
AND traffic_source.medium = 'organic'
AND (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') LIKE '%/pricing%'
AND _TABLE_SUFFIX = FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))Let's break that down:
- 'SELECT COUNT(DISTINCT user_pseudo_id)': This tells BigQuery to count the number of unique users.
- 'FROM "your-project-id..."': This specifies your GA4 data table. You'd replace this with your actual project and dataset ID.
- 'WHERE...': This section contains all of our filters. We're telling it to only count users where the event was a 'page_view,' the source was 'google,' the medium was 'organic,' the page location contained '/pricing,' and the event happened yesterday.
This is the gold standard for data analysis because it's completely custom, lightning-fast, and bypasses all sampling. It requires learning some SQL, but the payoff in terms of insight quality is enormous.
Final Thoughts
Querying your Google Analytics data opens up a new level of understanding about your audience and website performance. Whether you use the intuitive Explore tab in GA4, build visual dashboards in Looker Studio, or write powerful SQL in BigQuery, the goal is the same: to move past generic reports and get specific answers to your most important business questions.
We know that learning new reporting interfaces or a language like SQL isn't always practical when you just need answers quickly. That's why we built Graphed. After easily connecting your Google Analytics account, you can simply ask questions in plain English, like, "Show me a chart of our top 10 landing pages from Google organic search last month" or "Compare sessions vs. conversions by city for the last quarter." Our AI analyst builds the report or dashboard for you in seconds, saving you from the hours typically spent wrangling data so you can focus on making decisions.
Related Articles
What SEO Tools Work with Google Analytics?
Discover which SEO tools integrate seamlessly with Google Analytics to provide a comprehensive view of your site's performance. Optimize your SEO strategy now!
Looker Studio vs Metabase: Which BI Tool Actually Fits Your Team?
Looker Studio and Metabase both help you turn raw data into dashboards, but they take completely different approaches. This guide breaks down where each tool fits, what they are good at, and which one matches your actual workflow.
How to Create a Photo Album in Meta Business Suite
How to create a photo album in Meta Business Suite — step-by-step guide to organizing Facebook and Instagram photos into albums for your business page.