Google Analytics 4 Explorations: Create Field Case When
Cleaning up messy data directly inside Google Analytics 4 can feel like a puzzle, but the CASE WHEN function is the secret piece you need to solve it. This powerful feature allows you to create custom dimensions on the fly within Exploration reports, turning jumbled information into clear, actionable categories. This article will show you exactly how to use the CASE statement to build custom channel groupings, categorize your website's content, and get cleaner data for your analysis.
What Exactly is "Create Field" in GA4?
In older versions of Google Analytics (Universal Analytics), if you wanted to build custom groupings, you had to go into the admin settings and define them permanently. This was inflexible and often required you to wait hours, or even days, for the data to be processed.
GA4 introduces a much more fluid approach with calculated metrics and dimensions inside of its Exploration reports. The "Create field" feature is part of this improvement. It allows you to build temporary, custom dimensions and metrics that exist only within a specific Exploration you’re working on. You’re not making permanent changes to your entire property, you’re just creating a new lens through which to view your data for a particular analysis.
Think of it as creating a custom column in a spreadsheet. You're not changing the original data, just adding a new column that uses a formula to categorize the existing rows. This is where the CASE WHEN statement comes in.
The Power of the CASE WHEN Statement
The CASE WHEN statement is a simple but powerful logical tool. It works like a series of "if/then" rules that you define. It inspects a dimension (like Session source / medium or Page path), checks it against a list of conditions you provide, and then assigns a new value based on the first condition that it meets.
The basic structure looks like this:
case
when [condition 1] then "[output 1]"
when [condition 2] then "[output 2]"
when [condition 3] then "[output 3]"
else "[other]"
endThis is extremely useful for things like:
- Grouping Campaign Data: Combine all campaigns related to a specific product launch under a single name.
- Refining Channel Groupings: Separate branded organic search from non-branded organic search.
- Categorizing Content: Group all of your blog posts together for analysis, or all your product pages.
Essentially, it gives you the power to tell GA4, "When you see X, call it Y." Let's walk through how to put this into practice.
How to Access the "Create Field" Option
Before building our custom fields, you need to know where to find the 'Create field' feature. It's located within the Explorations section of your GA4 property.
- Navigate to Explore in the left-hand menu of Google Analytics 4.
- Start a new exploration (a "Blank" one is usually best) or open an existing one.
- In the Variables column on the left, look at the list of Dimensions. At the very bottom, next to the "Dimensions" heading, you’ll see a small plus sign (+).
- Click this plus sign to open the dimension library.
- In the top right corner of this pop-up window, you'll see a button labeled Create custom field. That's your destination. Click it, then select Field with conditions.
Now you're in the right place to start building your own custom dimensions using CASE WHEN logic.
Example 1: Creating a Custom Channel Grouping
One of the most common frustrations with GA4 is that the Default Channel Group can be inflexible or miscategorize traffic. For example, it might not properly distinguish between different social media platforms or between branded and non-branded search traffic. Let's create our own channel grouping to fix this.
The Goal: We want to create a dimension that groups traffic into more specific channels: Branded Search, Non-Branded Search, Paid Social, Organic Social, and Referral.
Step 1: Set Up the Custom Field
Inside the field builder, give your new dimension a clear name, like "Custom Channel Grouping V1".
Step 2: Write Your CASE WHEN Formula
Now, we'll tell Google Analytics exactly how to categorize the sessions. For this, we often need to look at multiple dimensions at once, such as Session source / medium and Session campaign name.
Here’s what a formula would look like. We’ll break it down piece by piece below.
case
when sessionDefaultChannelGroup = "Paid Search" and sessionCampaignName CONTAINS "(Brand)" then "Branded Paid Search"
when sessionDefaultChannelGroup = "Paid Search" then "Generic Paid Search"
when sessionMedium = "organic" and landingPage CONTAINS "(not set)|brand-name" then "Branded Organic"
when sessionDefaultChannelGroup = "Organic Search" then "Generic Organic Search"
when sessionSource CONTAINS "facebook|instagram|linkedin|twitter" and sessionMedium CONTAINS "cpc|ppc|paid" then "Paid Social"
when sessionSource CONTAINS "facebook|instagram|linkedin|twitter" then "Organic Social"
when sessionDefaultChannelGroup = "Referral" then "Referral"
else "Other"
endStep 3: Understanding the Logic
Let's break down that formula line by line:
when sessionDefaultChannelGroup = "Paid Search" and sessionCampaignName CONTAINS "(Brand)" then "Branded Paid Search": This is our most specific rule, so it goes first. It checks if traffic came from Paid Search AND the campaign name includes our brand term. If so, it labels this traffic "Branded Paid Search." The order is critical - if this rule were at the bottom, the next rule would catch the traffic first.when sessionDefaultChannelGroup = "Paid Search" then "Generic Paid Search": If the first rule wasn't met, this one checks if the traffic is simply from Paid Search. If it is, it gets labeled "Generic Paid Search."when sessionMedium = "organic" and landingPage CONTAINS "(not set)|brand-name" then "Branded Organic": This rule looks for organic traffic that landed on a page with your brand name in the URL or an undefined landing page (which can sometimes happen with branded searches). Replace "brand-name" with your actual brand.when sessionDefaultChannelGroup = "Organic Search" then "Generic Organic Search": This acts as the catch-all for any other organic search traffic.when sessionSource CONTAINS "facebook|instagram|linkedin|twitter" and sessionMedium CONTAINS "cpc|ppc|paid" then "Paid Social": This line uses theCONTAINSoperator and regular expressions (|means "OR"). It looks for traffic from common social platforms where the medium indicates it was paid.when sessionSource CONTAINS "facebook|instagram|linkedin|twitter" then "Organic Social": If the traffic came from a social site but wasn't paid, it falls into this bucket.when sessionDefaultChannelGroup = "Referral" then "Referral": A simple rule to categorize referral traffic.else "Other": This is vital. It’s the "catch-all" bucket. Any traffic that didn't meet any of the conditions above will be labeled "Other." Without this, that traffic would show up as (not set).
Step 4: Use Your New Dimension in an Exploration
After you save the new field, it will appear in your list of dimensions on the left. Now you can drag "Custom Channel Grouping V1" into the Rows and add metrics like Sessions, Engaged sessions, and Conversions to the Values section. Your report will now clearly break down performance by the precise channel groups you created, giving you much cleaner data for your analysis.
Example 2: Grouping Content by Topic
Imagine you run a blog with articles categorized into different topics like "Marketing," "Sales," and "Data Analysis." Your URL structure likely reflects this (e.g., yoursite.com/blog/marketing/article-name). By default, GA4 will only show you performance for each individual page. A CASE WHEN statement lets you group these pages together to see which topics drive the most traffic and engagement.
The Goal: We want to create a custom dimension that groups website pages into categories: Blog, Product Features, Pricing, and Homepage.
Step 1: Set Up and Name the Field
Follow the same process to create a new custom field. Call it something like "Content Group".
Step 2: Write the Content Grouping Formula
This time, we will primarily use the Page path dimension.
case
when pagePath CONTAINS "/blog/" then "Blog Content"
when pagePath CONTAINS "/features/" then "Feature Pages"
when pagePath = "/pricing" then "Pricing Page"
when pagePath REGEXP_MATCH "contacts|contact|about-us" then "Company Info"
when pagePath = "/" then "Homepage"
else "Other"
endStep 3: Breaking Down the Content Formula
when pagePath CONTAINS "/blog/" then "Blog Content": This rule is simple. If the page URL contains "/blog/", categorize it as "Blog Content." This will group all of your blog posts together, regardless of their specific title.when pagePath CONTAINS "/features/" then "Feature Pages": This does the same for any URL containing "/features/".when pagePath = "/pricing" then "Pricing Page": Here, we use the equals (=) operator because we are looking for an exact match for the pricing page.when pagePath REGEXP_MATCH "contacts|contact|about-us" then "Company Info": This uses a regular expression match (REGEXP_MATCH) to catch multiple variations of company information pages.when pagePath = "/" then "Homepage": Another exact match for the homepage.else "Other": again, this indispensable clause will bundle all the various low-volume pages into a helpful 'OtherPages' record for decluttering our data tables.
Step 4: Analyze Your Content Groups
Save this field and add it as a row in your Exploration report. Now, you can add metrics like Users, Average engagement time, and Conversions to see which category of content is most effective. Are your blog posts driving new users? Are your feature pages leading to conversions? This layered view provides insights that are impossible to get when looking at hundreds of individual page URLs.
Best Practices and Pro Tips
As you get comfortable with the CASE WHEN function, keep these tips in mind to get the most out of it:
- Order Is Everything: The statement checks conditions from top to bottom and stops at the first one it meets. Always place your most specific and restrictive rules at the top and your more general rules towards the bottom.
- Master the Operators: You have a variety of matching styles available, each with a specific purpose.
- Always Use an 'else' Statement: Forgetting the
elseclause at the end of your formula is a common mistake. Without it, any data that doesn't match one of yourwhenconditions will be left uncategorized as (not set), which can make your reports messy and confusing. Theelse "Other"rule ensures everything is accounted for. - Test Your Logic Incrementally: If you're building a complex statement with many conditions, build it one line at a time. Add one condition, save the field, and check your Exploration report to ensure it's working as expected. Then add the next one. This makes it far easier to troubleshoot errors.
Final Thoughts
The CASE WHEN function in GA4 Explorations is a game-changer for marketers and analysts who need cleaner, more relevant data. By learning this simple framework, you can move beyond GA4's default categories and create custom dimensions that perfectly align with your own business logic, whether you're organizing channel traffic, campaigns, or website content.
While powerful, building and debugging these statements still takes time and a specific kind of analytical knowledge. We designed Graphed to remove this friction entirely. Instead of writing formulas, you can connect your data sources - like Google Analytics, Google Ads, and Shopify - and just ask what you want to see in plain English. You can simply say, "Show me a report of sales broken down by branded versus non-branded traffic" and our AI will build the report for you instantly, handling all the underlying data grouping and logic automatically.
Related Articles
How to Connect Facebook to Google Data Studio: The Complete Guide for 2026
Connecting Facebook Ads to Google Data Studio (now called Looker Studio) has become essential for digital marketers who want to create comprehensive, visually appealing reports that go beyond the basic analytics provided by Facebook's native Ads Manager. If you're struggling with fragmented reporting across multiple platforms or spending too much time manually exporting data, this guide will show you exactly how to streamline your Facebook advertising analytics.
Appsflyer vs Mixpanel: Complete 2026 Comparison Guide
The difference between AppsFlyer and Mixpanel isn't just about features—it's about understanding two fundamentally different approaches to data that can make or break your growth strategy. One tracks how users find you, the other reveals what they do once they arrive. Most companies need insights from both worlds, but knowing where to start can save you months of implementation headaches and thousands in wasted budget.
DashThis vs AgencyAnalytics: The Ultimate Comparison Guide for Marketing Agencies
When it comes to choosing the right marketing reporting platform, agencies often find themselves torn between two industry leaders: DashThis and AgencyAnalytics. Both platforms promise to streamline reporting, save time, and impress clients with stunning visualizations. But which one truly delivers on these promises?