Migrating UA to GA4: BigQuery Custom Dimensions Guide
Universal Analytics is dead, but your historical SQL queries do not have to be. Most marketing ops teams treat the migration to Google Analytics 4 as a simple interface swap. They quickly realize that querying event-stream data in BigQuery requires an entirely different structural approach than the old session-based hit schema. If you query ga_sessions_* tables by joining hit-level custom dimensions, your pipelines will break on day one of the new event-driven architecture.
Bridging this gap requires disciplined data engineering. You must map flattened session hits to nested event arrays, translate scoped custom metrics, and re-establish identity resolution. This guide details how to preserve your custom dimensions, restructure event taxonomies in BigQuery, and build resilient pipelines that survive the schema shift.
Why Does the Universal Analytics to GA4 BigQuery Schema Break Legacy Pipelines?
Universal Analytics stored data in a relatively flat, pseudo-relational structure exported daily into partitioned tables named ga_sessions_YYYYMMDD. Each row represented a session, with hits nested inside repeating records. Custom dimensions lived in indexed arrays like `customDimensions[i].index` and `customDimensions[i].value`. You could easily query specific attributes by referencing index positions.
GA4 drops this session-first paradigm entirely. The native BigQuery export (`analytics_XXXXXXXXX.events_YYYYMMDD`) is strictly event-driven. Every user interaction—from a page view to an e-commerce purchase—is an individual record. Custom dimensions and metrics do not rely on static numeric indexes. Instead, they are stored as key-value pairs within repeated record fields: `event_params.key` and `event_params.value.string_value` (or `int_value`, `float_value`, `double_value`).
This architectural shift breaks legacy SQL scripts that reference `hit.customDimensions`. To query a custom dimension in GA4, you must unnest the `event_params` array. Failing to unnest correctly leads to massive query cost inflation through accidental cross-joins or empty result sets when dimensions are missing from specific event types.
How Do You Map UA Custom Dimensions to GA4 Event Parameters?
Before writing a single line of migration SQL, audit your current Universal Analytics custom dimension implementation. Map every index (e.g., `dimension1: author_name`) to its corresponding GA4 user-scoped, item-scoped, or event-scoped parameter.
| Universal Analytics Scope | GA4 Equivalent Scope | BigQuery Storage Structure | Query Complexity |
|---|---|---|---|
| Hit / Action | Event-Scoped | `event_params` repeated record | High (Requires UNNEST per event) |
| Session | Event-Scoped (Passed on every hit) | `event_params` repeated record | Medium (Requires session aggregation) |
| User | User-Scoped | `user_properties` repeated record | Low-Medium (Persists across user events) |
| Product | Item-Scoped | `items` repeated record | High (Nested within an array inside events) |
Event-scoped parameters in GA4 only attach to the specific event where they are logged. In Universal Analytics, if a session-scoped dimension was set on page view, it persisted for the session. In GA4, if you need a dimension like `user_type` to apply to every event in a session, your tracking implementation must inject that parameter into every outgoing event, or you must handle it downstream in BigQuery via window functions.
What Is the Step-by-Step Process for Restructuring Event Taxonomies in BigQuery?
Migrating your reporting views requires building unified SQL views that normalize UA and GA4 data into a single schema. Follow this operational sequence to construct a backward-compatible data model in BigQuery.
- Extract and Unnest Event Parameters: Write a base SQL transformation layer that safely extracts custom dimensions from the `event_params` array without causing fan-out amplification. Use scalar subqueries or conditional aggregation instead of raw `CROSS JOIN UNNEST` when extracting multiple parameters from the same event.
- Normalize Event Names: Map old UA action categories and labels to the new GA4 event taxonomy. For example, convert `Category: 'Lead', Action: 'Form Submit'` into the standardized GA4 event name `generate_lead` with event parameters for form type and location.
- Reconstruct Sessions: Because GA4 does not have a native session table, generate session IDs in BigQuery using the `ga_session_id` event parameter (which is a timestamp combination). Group events by `user_pseudo_id` and `ga_session_id` to calculate session durations and bounce rates that match your legacy metrics. Read more about handling identity matching and attribution discrepancies in our GA4 User-ID cross-device tracking guide.
- Union Historical and New Data: Create a BigQuery view or materialized table using a `UNION ALL` statement that combines your legacy `ga_sessions_*` tables with the new `events_*` tables. Ensure column data types match precisely before executing the union.
When implementing these transformations, watch out for common pitfalls like unassigned traffic spikes or missing campaign parameters. For broader attribution continuity, ensure your channel grouping logic accounts for missing UTM tags by following recommendations in the GA4 unassigned traffic guide.
How Do You Write Efficient SQL to Query GA4 Custom Dimensions?
Inefficient unnesting is the primary driver of runaway BigQuery costs during GA4 analysis. When querying multiple event-scoped custom dimensions, a naive `CROSS JOIN UNNEST(event_params)` multiplies your row count exponentially if an event contains fifty parameters and you unnest three of them in separate statements.
Instead, use conditional aggregation with SAFE_OFFSET or targeted subqueries. Here is a production-tested SQL snippet that extracts custom event parameters and item-scoped parameters efficiently:
SELECT
event_date,
TIMESTAMP_MICROS(event_timestamp) AS event_timestamp,
event_name,
user_pseudo_id,
-- Extracting an event-scoped custom dimension safely
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'author_name') AS author_name,
-- Extracting a numeric event parameter
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'reading_time') AS reading_time,
-- Extracting session ID
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS session_id
FROM
`your-project-id.analytics_123456789.events_*`
WHERE
_TABLE_SUFFIX = FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))
If you need to connect your offline sales data or CRM pipeline back to these GA4 events for attribution modeling, make sure your event parameters capture client IDs and transaction IDs correctly. For advanced attribution setups, review the methodology for how to sync GA4 BigQuery data to Salesforce.
What Are the Best Practices for Monitoring BigQuery Cost and Schema Drift?
GA4 drops new parameters into the event schema dynamically when app or web developers deploy updates through Google Tag Manager. This dynamic schema evolution causes downstream SQL queries to fail when columns expected by reporting tools change type or disappear.
Implement these operational safeguards to maintain data integrity:
- Enforce Partition and Cluster Filters: Always filter queries by `_TABLE_SUFFIX` or partition fields (`event_date`). Never run full table scans on multi-terabyte GA4 datasets.
- Use Partitioned Tables for Derived Models: Write heavy aggregations into scheduled query tables partitioned by day rather than querying raw event logs repeatedly.
- Set BigQuery Billing Alerts: Configure strict cost controls at the project level to prevent rogue unnested cross-joins from exhausting monthly budgets.
- Audit Schema Changes Weekly: Run information schema queries against `INFORMATION_SCHEMA.COLUMN_FIELD_PATHS` to detect when developers modify event parameter names or data types in GTM.
Transitioning your analytics engineering stack from Universal Analytics to GA4 BigQuery export demands a complete mindset shift. Ditch session-level thinking, master efficient array unnesting, and build robust validation views. For teams also managing server-side tagging pipelines alongside BigQuery storage, verify your transport layers with the server-side GTM implementation guide to ensure clean data ingestion at the edge.
Frequently Asked Questions
Why do my GA4 BigQuery row counts multiply when I query custom dimensions?
This happens when you use a standard `CROSS JOIN UNNEST(event_params)` without filtering for specific keys first. Because each event contains multiple parameters in an array, a naive cross-join duplicates the parent event row for every parameter present. Use scalar subqueries or filter the array with a `WHERE` clause inside the unnest statement to prevent row multiplication.
Can I recreate Universal Analytics session metrics in GA4 BigQuery?
Yes, but you must calculate sessions programmatically using the `ga_session_id` event parameter combined with the `user_pseudo_id`. GA4 does not store a standalone session table. You can group events by user and session ID to derive session duration, page views per session, and bounce rates that align with legacy reports.
How do I handle schema drift when developers change GA4 event parameters in GTM?
Schema drift occurs when parameter values change from strings to integers or when parameter names are altered. Implement scheduled validation queries that check `INFORMATION_SCHEMA` tables for unexpected data type changes, and establish a strict naming convention governance document with your front-end development team.
What is the most cost-effective way to store historical UA and GA4 data together?
The most efficient approach is to transform and normalize your raw GA4 event logs into a daily partitioned and clustered summary table using BigQuery scheduled queries. Querying these aggregated derivative tables costs a fraction of scanning raw `events_*` wildcard tables repeatedly for dashboard reporting.