diff --git a/macros/base_select.sql b/macros/base_select.sql index 1f374539..b7354292 100644 --- a/macros/base_select.sql +++ b/macros/base_select.sql @@ -136,7 +136,17 @@ , unnested_items.item_params )) from unnest(items) as unnested_items ) items - , {{ ga4.unnest_key('event_params', 'ga_session_id', 'int_value', 'session_id') }} + , COALESCE( + -- Use int_value if available + {{ ga4.unnest_key('event_params', 'ga_session_id', 'int_value', 'session_id') }}, + -- Extract the second section (numeric part) from the string_value format + CAST( + REGEXP_EXTRACT( + {{ ga4.unnest_key('event_params', 'ga_session_id', 'string_value', 'session_id_string') }}, + r'^GS\d\.\d\.(\d+)' + ) AS INT64 + ) +) AS session_id , {{ ga4.unnest_key('event_params', 'page_location') }} , {{ ga4.unnest_key('event_params', 'ga_session_number', 'int_value', 'session_number') }} , COALESCE(