Calculating Sessions and Engaged Sessions in GA4 BigQuery

Here is how you can calculate ‘sessions’ and ‘engaged sessions’ in GA4 BigQuery.

First thing first. 

Both ‘sessions‘ and ‘engaged sessions’ metrics need to be manually calculated. 

They are not available as default fields in the GA4 BigQuery export schema.

You must know the formula (underlying logic) to calculate a particular GA4 dimension/metric in BigQuery. 

If you do not understand the underlying logic, you will have a hard time using a particular GA4 dimension/metric in BigQuery, even if I give you the exact SQL code to copy and paste. 

There can be ‘N’ use cases, and it is impossible to provide pre-built SQL code for every possible use case.

Note: To find formulas for a particular dimension or metric, refer to the ‘GA4 to BigQuery Mapping Tutorial‘ article.

In addition to knowing the formula, you must be familiar with the GA4 BigQuery export schema so that you can easily refer to a default dimension/metric and identify the data type they use.

If you are new to the schema, refer to the ‘GA4 BigQuery Schema Tutorial‘.

Once you understand the formula and export schema, it will be easier and possible for you to modify the SQL code to meet your unique requirements.

Calculating Sessions in GA4 BigQuery

To calculate the total number of sessions, use ‘Common Table Expressions (CTEs)’ and count each unique combination of ‘user_pseudo_id’ and ‘ga_session_id’.

A Common Table Expression (CTE) is a temporary result set that you define within an SQL statement. It acts like a temporary table that exists only for the duration of the query. 

CTEs allow you to break down complex queries into manageable parts, making the SQL easier to read and understand. 

This is especially helpful when working with intricate logic or multiple steps in a calculation.

With CTEs, you can define a calculation once and reference it multiple times within the same query. 

This avoids duplication of code and reduces the risk of errors.

The ‘user_pseudo_id’ is an event parameter which is extracted as an integer value. It is a unique identifier for a user.

The ‘user pseudo id is an event parameter

The ‘ga_session_id’ is an array of structures within the ‘event_params’ array of structures. 

It is extracted as a string value and is a unique identifier for a session. 

The ‘ga session id is an array of structures

Once you have created the required SQL code using the logic mentioned above, you should see an output like the one below:

sessions ga4 bigquery

If you struggle with creating the correct SQL code, message me on LinkedIn, and I will share it with you.

Calculating Engaged Sessions in GA4 BigQuery

To calculate ‘engaged sessions’, use Common Table Expression and count distinct sessions for which the value of the ‘session_engaged’ event parameter is 1. 

Needless to say, you need to calculate the ‘sessions’ metric first before you can calculate the ‘Engaged Sessions’ metric.

The ‘session_engaged’ is an array of structures within the ‘event_params’ array of structures. 

The ‘session_engaged’ parameter is extracted as an integer value.

The ‘session engaged is an array of structures

Once you have created the required SQL code using the logic mentioned above, you should see an output like the one below:

engaged sessions ga4 bigquery

If you struggle with creating the correct SQL code, message me on LinkedIn, and I will share it with you.

My best selling books on Digital Analytics and Conversion Optimization

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade