BigQuery GA4 Users Sessions Tutorial: How to Identify Your Top Traffic Sources

BigQuery GA4 Users Sessions Tutorial: How to Identify Your Top Traffic Sources

If you want to dive deeper into understanding which traffic sources bring the most users to your website, this tutorial is perfect for you. Using Google Analytics 4 (GA4) data in BigQuery, you can unlock detailed insights about your user sessions by querying raw event data directly. This guide walks you through the process step-by-step, showing you how to extract and analyze traffic source data to track campaign performance, SEO traffic, and referral spikes effectively.

Before we jump in, feel free to watch the full video tutorial here for a visual walkthrough: BigQuery GA4 Users Sessions Tutorial.

Step 1: Understanding GA4 Traffic Source Data Structure in BigQuery

GA4 organizes traffic source information in the traffic_source field, which is part of each user session event. This field contains three key components:

  • Source: The origin of your traffic (e.g., google, facebook.com)
  • Medium: The marketing medium (e.g., organic, referral, cpc)
  • Name: Often used as the default channel grouping (e.g., Organic Search, Paid Search, Referral)

Each user session is linked with these components, allowing us to analyze traffic by source, medium, and channel grouping.

GA4 traffic source fields: source, medium, and name

Step 2: Preparing Your Query to Extract Traffic Source and User Data

To analyze which traffic sources bring the most users, you need to query the BigQuery dataset containing your GA4 raw event data. Here’s the approach:

  1. Parse the date: GA4 stores the date as a string, so you must parse it to a date format for proper grouping and filtering.
  2. Select traffic source fields: Extract source, medium, and name from the traffic_source field.
  3. Count distinct users: Use the user_pseudo_id field to count unique users.
  4. Group by date and traffic source dimensions: Since each event is a separate row, grouping is necessary to aggregate data by date, source, medium, and default channel grouping.

Here is a simplified version of the SQL query structure that accomplishes this:

SELECT
  PARSE_DATE('%Y%m%d', event_date) AS date,
  traffic_source.source,
  traffic_source.medium,
  traffic_source.name AS default_channel_grouping,
  COUNT(DISTINCT user_pseudo_id) AS users
FROM
  `your_project.your_dataset.events_*`
GROUP BY
  date, source, medium, default_channel_grouping
ORDER BY
  date, users DESC
SQL query example for counting distinct users by traffic source

Step 3: Counting Sessions Alongside Users

While user count is crucial, understanding sessions gives you additional context on user engagement. Sessions represent distinct visits to your site, which may include multiple events.

If you want to count sessions, you can modify your query to count distinct session_id or use the session_start event as a reference. I have another detailed tutorial on session counting, but here’s a quick tip:

  • Count distinct session_id per traffic source and date.
  • Group by the same dimensions as in the user count query.
Counting sessions in BigQuery GA4 data example

Step 4: Running and Interpreting Your Query Results

After running your query, you will get a table showing the number of users coming from each traffic source, medium, and default channel grouping for each date. This data is powerful for:

  • Tracking campaign performance across different channels.
  • Monitoring SEO traffic trends.
  • Spotting referral traffic spikes that might indicate new partnerships or viral content.

Because this data comes from raw event data, it is highly flexible and customizable to your specific marketing questions.

Query results showing users by traffic source and date

Step 5: Using Looker Studio to Visualize Your Traffic Source Data

Once you have your query results, you can connect your BigQuery output to Looker Studio (formerly Google Data Studio) for visualization. This step helps you create interactive dashboards that update automatically with fresh GA4 data.

On my website, you can find free Looker Studio templates designed specifically for marketing and sales analytics. These templates help you visualize key metrics like users by channel, sessions, conversions, and more.

Looker Studio templates for marketing and sales analytics

Step 6: Automate and Enhance Your Marketing Analytics Workflow

By combining GA4 raw data in BigQuery with Looker Studio dashboards, you create a robust analytics workflow that goes beyond standard GA4 reports. Here are some best practices to keep in mind:

  • Schedule queries to update your data regularly.
  • Filter data to focus on specific campaigns, geographies, or devices.
  • Use calculated fields to create custom KPIs like conversion rates or session duration.

Final Thoughts

Querying GA4 data in BigQuery to analyze users and sessions by traffic source unlocks a new level of insight for marketers. You gain full control over your data, enabling you to track performance with precision and react quickly to changes in traffic patterns.

This method not only helps you identify which channels bring the most users but also supports deeper analyses like session trends and campaign ROI calculations. Plus, pairing this data with Looker Studio dashboards makes it easy to share insights with your team or stakeholders.

If you found this tutorial helpful, I encourage you to explore more advanced analyses in my other articles:

Feel free to leave a comment or reach out with questions about your specific use case. Happy querying!