-
Aggregating GA4 Event Data by Session ID in BigQuery
Hey there, folks! So, here’s my dilemma: I’m building this SQL query where I need to grid all my GA4 event data based on the session ID. Super simple, right? I thought so too, until now.
When I fire off this query, the resulting table gives me session ID as a completely separate column – not nestled within an array for event parameters as you’d expect for a certain event. But what’s really grinding my gears is the session_id column, which seems to have its own agenda with non-unique values. I mean, the session id is replicated multiple times for each row, representing a new event within that session. And what I really want to do is to seamlessly blend together those non-unique session ids, so I get all events that corresponds to a particular session_id.
I tested this query, and it does hand me session_id on a separate column – with the session_id cloned for each event, however.
SELECT *, ( SELECT COALESCE(value.int_value, value.float_value, value.double_value) FROM UNNEST(event_params) WHERE key = 'ga_session_id' ) AS session_id, ( SELECT COALESCE(value.string_value) FROM UNNEST(event_params) WHERE key = 'page_location' ) AS page_location FROM
digital-marketing-xxxxxx.analytics_xxxxxxx.events_intraday*
For example, my output looks like this, amidst several more columns:
session_id
event_name1234567
session_start1234567
click_urlSo, I’m trying to merge the session ids into a solitary cell. Tried this line:
SELECT *, ( SELECT COALESCE(value.int_value, value.float_value, value.double_value) FROM UNNEST(event_params) WHERE key = 'ga_session_id' ) AS session_id, ( SELECT COALESCE(value.string_value) FROM UNNEST(event_params) WHERE key = 'page_location' ) AS page_location FROM
digital-marketing-xxxxxxx.analytics_xxxxxxx.events_intraday*
GROUP BY session_idBut Kaboom! Error shows up, claiming I can’t aggregate certain values (like date) – which is tasked when trying to group by session id.
Got any idea, folks, how to outsmart this? I admit I’m a noob at SQL, and I’m currently stuck in the rabbit hole with no clear guidance on how to roll with this.
Log in to reply.