Skip to main content

Samarth Analytics

From GA4 to dbt: Modeling the Sessions and Events Schema

By Samarth Analytics · Published · Updated · Analytics Engineering · 4 min read

Summary: A production-grade analytics engineering guide for transforming GA4 BigQuery export data into a clean dbt model. This article explains how to flatten nested event structures, reconstruct sessions, resolve user identity, and build scalable dim/fact tables for reporting and reverse ETL workflows.

From GA4 to dbt: Modeling the Sessions and Events Schema

WHY FLATTEN GA4

GA4’s BigQuery export is powerful, but not immediately usable for reporting or business analysis. The raw schema is deeply nested, event-based, and optimized for storage rather than usability.

Every interaction is stored as an event with nested parameters, which creates three core problems for analysis:

Query complexity increases significantly Business logic is duplicated across dashboards Session and user-level analysis is not directly available

For example, a single purchase event may contain revenue, product details, coupon codes, and user metadata inside arrays that must be unnested before use.

Flattening GA4 is not optional for scalable analytics-it is a prerequisite for building reliable reporting systems.

The goal of modeling GA4 in dbt is simple:

Turn raw event data into structured, queryable dimensions and facts that reflect how the business actually operates.

DBT PROJECT STRUCTURE WE USE

A well-structured dbt project is the foundation of scalable analytics engineering. For GA4 modeling, the architecture typically follows a layered approach.

A standard structure includes:

  1. Staging layer (stg_)

Raw GA4 event flattening Parameter extraction Type casting and cleaning Basic filtering (bot traffic, invalid events)

  1. Intermediate layer (int_)

Session reconstruction User stitching logic Event aggregation logic Derived metrics (session duration, engagement)

  1. Marts layer (dim_ / fact_)

Fact tables (events, sessions, purchases) Dimension tables (users, campaigns, products) Business-ready models for BI tools

This separation ensures:

Reusable logic Clear transformation lineage Easier debugging and testing Modular scaling across teams

In production environments, dbt acts as the transformation backbone between raw GA4 export and business intelligence systems.

STG_EVENTS: UNNESTING PARAMETERS

The first and most critical transformation step is flattening GA4’s nested event structure.

GA4 stores key data inside arrays such as event_params and items, which must be extracted before analysis.

Key operations in stg_events include:

Unnesting event_params into structured columns Extracting key metrics like page_location, transaction_id, and value Standardizing event names for consistency Casting timestamps into usable datetime formats

Example transformations:

event_params → structured columns nested revenue → numeric field item arrays → product-level rows

This layer ensures that downstream models do not need to repeatedly parse nested structures.

The rule here is simple: If it is nested in GA4, it should be flattened in staging.

SESSIONS RECONSTRUCTION

GA4 does not explicitly provide clean session tables in BigQuery exports. Sessions must be reconstructed using event-level data.

Session reconstruction typically relies on:

user_pseudo_id session identifiers (when available) event timestamps session timeout logic (commonly 30 minutes inactivity rule)

Core logic:

Sort events by user and timestamp Identify gaps greater than session timeout threshold Assign session IDs based on continuity rules Aggregate events into session-level metrics

Session-level outputs include:

session start time session end time total events per session session duration conversion events within session

This reconstruction is essential because most business reporting depends on session-based metrics even though GA4 is event-driven.

USER IDENTITY RESOLUTION

One of the most complex problems in GA4 modeling is identity resolution. GA4 uses pseudonymous identifiers that must be stitched together for meaningful user analysis.

Primary identifiers include:

user_pseudo_id (device-based identifier) user_id (if implemented by the business) CRM identifiers (when integrated via reverse ETL)

Challenges:

Users switching devices break continuity Anonymous sessions cannot always be linked Logged-in vs logged-out behavior differs significantly

Resolution strategies in dbt include:

Prioritizing user_id when available Joining CRM data for deterministic identity mapping Using heuristic stitching for partial matches Maintaining separate anonymous and known user tables

The goal is not perfect identity resolution, but consistent and explainable logic.

EXPOSURES FOR LOOKER STUDIO

Once GA4 data is modeled in dbt, the final step is making it accessible for business intelligence tools like Looker Studio.

Exposures in dbt define how models are consumed downstream.

In practice, this includes:

Marking fact and dimension tables as BI-ready Documenting data dependencies for dashboards Ensuring stable schema contracts for reporting layers Connecting LTV, funnel, and acquisition models to dashboards

Typical Looker Studio datasets include:

sessions fact table events fact table users dimension table campaign performance views

Benefits of exposure definition:

Clear ownership of data models Reduced dashboard breakage risk Transparent lineage from GA4 to reporting layer Easier collaboration between analysts and engineers

This step transforms dbt from a transformation tool into a full analytics ecosystem layer.

FINAL THOUGHTS

Moving from GA4 to dbt modeling is a critical step in scaling analytics beyond surface-level reporting. GA4 provides raw event data, but dbt transforms it into structured, business-ready models that can support decision-making across teams.

By flattening nested schemas, reconstructing sessions, resolving user identity, and exposing clean datasets for BI tools, organizations move from fragmented reporting to a governed analytics architecture.

The real value of this approach is not just cleaner data-it is the ability to trust and scale analytics across the entire business without rebuilding logic in every dashboard.

Topics: GA4 dbt model, BigQuery modeling, analytics engineering, sessions schema, event tracking, dbt project structure, reverse ETL, Looker Studio

More articles on the Samarth Analytics blog · Markdown version of this article