'BigQuery query review for session based attribution
I've been struggling for a while to get a query to return the number of sessions and users per source/campaign/medium/content, based on the current session of the user and not first touch based attribution.
Will someone review this? As a side question, why do I have to CONCAT session id and user id to get accurate session count?
---tempquery
with first_page_view as (
select
event_date,
user_pseudo_id,
(select value.int_value from unnest(event_params) where key = 'ga_session_id') as session_id,
((select value.string_value from unnest(event_params) where key = 'medium' )) as session_medium,
((select value.string_value from unnest(event_params) where key = 'source')) as session_source,
((select value.string_value from unnest(event_params) where key = 'campaign')) as session_campaign,
((select value.string_value from unnest(event_params) where key = 'content')) as session_content,
((select value.int_value from unnest(event_params) where key = 'entrances' )) as entrances,
from
`*******.events_*`
where event_name = "page_view"
)
,
conversion_event as (
select
user_pseudo_id,
(select value.int_value from unnest(event_params) where key = 'ga_session_id') as session_id,
from
`ga4*****************.events_*`
where event_name = 'ConversionEvent'
)
--mainquery
SELECT
a.event_date,
a.session_source,
a.session_medium,
a.session_campaign,
a.session_content,
count(distinct (CONCAT(a.session_id, a.user_pseudo_id))) as sessions,
count(distinct a.user_pseudo_id) as users,
count(distinct (CONCAT(b.session_id, b.user_pseudo_id))) as conversion_sessions,
count(distinct b.user_pseudo_id) as conversion_users
from first_page_view as a
left join conversion_event as b
on (CONCAT(a.session_id, a.user_pseudo_id)) = (CONCAT(b.session_id, b.user_pseudo_id))
where a.entrances = 1
group by 1,2,3,4,5
order by sessions desc
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|