Select-- traffic_source.name (dimension | name of the marketing campaign that first acquired the user) traffic_source.name,-- traffic_source.medium (dimension | name of the medium (paid search, organic search, email, etc.) that first acquired the user) traffic_source.medium,-- traffic_source.source (dimension | name of the network that first acquired the user) traffic_source.sourcefrom-- change this to your google analytics 4 export location in bigquery`privat-327611.analytics_266663932.events_*`where-- define static and/or dynamic start and end date _table_suffix between'20211222'and format_date('%Y%m%d',date_sub(current_date(), interval 1day))
Berechnete Traffic-Quellen-Dimensionen
default channel grouping (user or session)
source / medium (user or session)
campaign (user or session)
full referrer
-- subquery to set the start and end date once for the whole querywith date_range as (select'20211222'asstart_date, format_date('%Y%m%d',date_sub(current_date(), interval 1day)) as end_date),-- subquery to prepare and calculate traffic source data based on user and session idtraffic as (select user_pseudo_id, session_id, session_number,concat(source,' / ',medium) as source_medium_session,casewhen campaign_session isnullthen'(direct)'else campaign_session endas campaign_session, full_referrer,-- definitions of the channel grouping based on the source / medium of every sessioncasewhen source ='(direct)'and (medium ='(not set)'or medium ='(none)') then'Direct'when medium ='organic'then'Organic Search'when regexp_contains(medium, r'^(social|social-network|social-media|sm|social network|social media)$') then'Social'when medium ='email'then'Email'when medium ='affiliate'then'Affiliates'when medium ='referral'then'Referral'when regexp_contains(medium, r'^(cpc|ppc|paidsearch)$') then'Paid Search'when regexp_contains(medium, r' ^(cpv|cpa|cpp|content-text)$') then'Other Advertising'when regexp_contains(medium, r'^(display|cpm|banner)$') then'Display'else'(Other)'endas default_channel_grouping_session, source_medium_user, campaign_user, default_channel_grouping_userfrom (select user_pseudo_id, (select value.int_value from unnest(event_params) where event_name ='page_view'andkey='ga_session_id') as session_id, (select value.int_value from unnest(event_params) where event_name ='page_view'andkey='ga_session_number') as session_number, event_timestamp,rank() over (partitionby user_pseudo_id, (select value.int_value from unnest(event_params) where event_name ='page_view'andkey='ga_session_id') order by event_timestamp) as rank,casewhen (select value.string_value from unnest(event_params) where event_name ='page_view'andkey='source') isnullthen'(direct)'else (select value.string_value from unnest(event_params) where event_name ='page_view'andkey='source') endas source,casewhen (select value.string_value from unnest(event_params) where event_name ='page_view'andkey='medium') isnullthen'(none)'else (select value.string_value from unnest(event_params) where event_name ='page_view'andkey='medium') endas medium, (select value.string_value from unnest(event_params) where event_name ='page_view'andkey='campaign') as campaign_session, (select value.string_value from unnest(event_params) where event_name ='page_view'andkey='page_referrer') as full_referrer,concat(traffic_source.source,' / ',traffic_source.medium) as source_medium_user, traffic_source.name as campaign_user,-- definitions of the channel grouping based on the source / medium of a user's first sessioncasewhen traffic_source.source ='(direct)'and (traffic_source.medium ='(not set)'or traffic_source.medium ='(none)') then'Direct'when traffic_source.medium ='organic'then'Organic search'when regexp_contains(traffic_source.medium, r'^(social|social-network|social-media|sm|social network|social media)$') then'Social'when traffic_source.medium ='email'then'Email'when traffic_source.medium ='affiliate'then'Affiliates'when traffic_source.medium ='referral'then'Referral'when regexp_contains(traffic_source.medium, r'^(cpc|ppc|paidsearch)$') then'Paid Search'when regexp_contains(traffic_source.medium, r' ^(cpv|cpa|cpp|content-text)$') then'Other Advertising'when regexp_contains(traffic_source.medium, r'^(display|cpm|banner)$') then'Display'else'(Other)'endas default_channel_grouping_userfrom-- change this to your google analytics 4 bigquery export location`privat-327611.analytics_266663932.events_*`, date_rangewhere _table_suffix between date_range.start_date and date_range.end_dateand (select value.int_value from unnest(event_params) where event_name ='page_view'andkey='ga_session_id') is not null)where rank =1)-- main queryselect-- user default channel grouping (dimension | the channel group associated with an user's first session) default_channel_grouping_user,-- user source / medium (dimension | the referral source and type associated with an user's first session) source_medium_user,-- user campaign (dimension | the value of a campaign associated with an user's first session) campaign_user,-- session default channel grouping (dimension | the channel group associated with a session) default_channel_grouping_session,-- session source / medium (dimension | the referral source and type associated with a session) source_medium_session,-- session campaign (dimension | the value of a campaign associated with a session) campaign_session,-- session full referrer (dimension | the full referring url of a session, including the hostname and path) full_referrerfrom traffic