GA4 BigQuery SQL: How to UNNEST Event Parameters Cleanly
Copy-ready SQL patterns to flatten GA4 event_params without row explosion, build unique session keys, and verify clean GTM schemas before querying.
Syed Yasir
Conversion Tracking & Server-Side Attribution Expert
Table of Contents
Direct Answer
To query raw GA4 exports without multiplying row counts, extract nested event_params using scalar subqueries (SELECT value.string_value FROM UNNEST(event_params) WHERE key = '...') instead of a cross-join UNNEST. Always combine user_pseudo_id with ga_session_id to build a unique session key, and fix upstream GTM event naming first so downstream SQL stays accurate.
How GA4 Stores Nested Event Parameters
Each row in analytics_<property_id>.events_* represents one event, while parameters (page_location, ga_session_id, transaction_id, value) live inside a repeated event_params record with typed value columns:
| Parameter Type | Column to Query | Typical GA4 / Shopify / Lead-Gen Keys |
|---|---|---|
| String | value.string_value |
page_location, source, medium, currency, form_id |
| Integer | value.int_value |
ga_session_id, ga_session_number, engagement_time_msec |
| Decimal / Mixed Numeric | COALESCE(double_value, float_value, int_value) |
value, shipping, tax |
1. Extract Event Parameters Without Row Explosion
Using scalar subqueries keeps one row per event while pulling exact Shopify purchase or lead-gen parameters cleanly:
SELECT
event_date,
TIMESTAMP_MICROS(event_timestamp) AS event_ts,
user_pseudo_id,
event_name,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS ga_session_id,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS page_location,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'source') AS event_source,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'medium') AS event_medium,
ecommerce.transaction_id,
ecommerce.purchase_revenue
FROM `your_project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20261101' AND '20261202'
AND event_name IN ('page_view', 'generate_lead', 'purchase');
2. Extract Numeric Values Safely With COALESCE
Because GTM tags sometimes send value as an integer (100) on one order and a float (100.50) on another, wrap numeric extractions in COALESCE:
(
SELECT COALESCE(value.double_value, value.float_value, CAST(value.int_value AS FLOAT64))
FROM UNNEST(event_params)
WHERE key = 'value'
) AS event_value
3. Build a Globally Unique Session Key
ga_session_id alone is only the Unix timestamp of session start—two shoppers arriving at the same second share the same ID. Always concatenate user_pseudo_id and ga_session_id:
CONCAT(
user_pseudo_id,
'-',
CAST((SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS STRING)
) AS session_key
4. Fix Upstream GTM Hygiene Before Writing SQL
No SQL query can repair broken upstream collection. Before reconciling raw GA4 exports against store revenue or ad spend:
- Eliminate duplicate GA4 configuration tags and inconsistent event naming (review GTM vs. GA4 Architecture).
- Resolve Unassigned sessions and missing
page_viewhits (see Fix GA4 Unassigned & Google Ads Mismatches). - Always filter
_TABLE_SUFFIXby explicit date ranges to prevent unnecessary query scan costs.
Next Step
Clean reporting starts with a clean GTM container. Inspect your live Web GTM export locally for duplicate GA4 tags, PII leaks, and bloated scripts with the free in-browser GTM Container JSON Auditor, or book a Free Tracking Audit to fix upstream GA4 and attribution defects.
Free Tracking Audit
Want Me to Audit Your Conversion Tracking Stack?
Book a free tracking audit. I'll review your GTM, GA4, Server-Side Tracking, Meta CAPI, Google Ads Enhanced Conversions, and Consent Mode V2 setup and highlight exactly what to fix.