Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align default channel groupings with GA4 docs #282

Merged
merged 3 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 128 additions & 65 deletions macros/default_channel_grouping.sql
Original file line number Diff line number Diff line change
@@ -1,85 +1,148 @@
-- Inspired by https://github.com/llooker/ga_four_block_dev/blob/master/views/sessions.view.lkml
-- Google's documentation is here: https://support.google.com/analytics/answer/9756891?hl=en
-- source_category Excel file can be downloaded from the above link and may change over time

{% macro default_channel_grouping(source, medium, source_category) %}
{{ return(adapter.dispatch('default_channel_grouping', 'ga4')(source, medium, source_category)) }}
{% macro default_channel_grouping(source, medium, source_category, campaign) %}
{{ return(adapter.dispatch('default_channel_grouping', 'ga4')(source, medium, source_category, campaign)) }}
{% endmacro %}

{% macro default__default_channel_grouping(source, medium, source_category) %}
case
when
(
{{source}} is null
{% macro default__default_channel_grouping(source, medium, source_category, campaign) %}
case
-- Direct: Source exactly matches "(direct)" AND Medium is one of ("(not set)", "(none)")
when (
{{source}} is null
and {{medium}} is null
)
or (
{{source}} = '(direct)'
and ({{medium}} = '(none)' or {{medium}} = '(not set)')
)
)
then 'Direct'
when
(
REGEXP_CONTAINS({{source}}, r"^(facebook|instagram|pinterest|reddit|twitter|linkedin)") = true
or {{source_category}} = 'SOURCE_CATEGORY_SOCIAL'

-- Cross-network: Campaign Name contains "cross-network"
when REGEXP_CONTAINS({{campaign}}, r"cross-network")
then 'Cross-network'

-- Paid Shopping:
-- (Source matches a list of shopping sites
-- OR
-- Campaign Name matches regex ^(.*(([^a-df-z]|^)shop|shopping).*)$)
-- AND
-- Medium matches regex ^(.*cp.*|ppc|retargeting|paid.*)$
when (
{{source_category}} = 'SOURCE_CATEGORY_SHOPPING'
or REGEXP_CONTAINS({{campaign}}, r"^(.*(([^a-df-z]|^)shop|shopping).*)$")
)
and REGEXP_CONTAINS({{medium}}, r"^(.*cp.*|ppc|retargeting|paid.*)$") = true
then 'Paid Social'
when
REGEXP_CONTAINS({{source}}, r"^(facebook|instagram|pinterest|reddit|twitter|linkedin)") = true
or {{medium}} in ("social","social-network","social-media","sm","social network","social media")
or {{source_category}} = 'SOURCE_CATEGORY_SOCIAL'
then 'Organic Social'
when
REGEXP_CONTAINS({{medium}}, r"email|e-mail|e_mail|e mail") = true
or REGEXP_CONTAINS({{source}}, r"email|e-mail|e_mail|e mail") = true
then 'Email'
when
REGEXP_CONTAINS({{medium}}, r"affiliate|affiliates") = true
then 'Affiliates'
when
{{source_category}} = 'SOURCE_CATEGORY_SHOPPING'
and REGEXP_CONTAINS({{medium}},r"^(.*cp.*|ppc|paid.*)$")
and REGEXP_CONTAINS({{medium}},r"^(.*cp.*|ppc|retargeting|paid.*)$")
then 'Paid Shopping'
when
({{source_category}} = 'SOURCE_CATEGORY_VIDEO' AND REGEXP_CONTAINS({{medium}},r"^(.*cp.*|ppc|paid.*)$"))
or {{source}} = 'dv360_video'

-- Paid Search:
-- Source matches a list of search sites
-- AND
-- Medium matches regex ^(.*cp.*|ppc|retargeting|paid.*)$
when {{source_category}} = 'SOURCE_CATEGORY_SEARCH'
and REGEXP_CONTAINS({{medium}}, r"^(.*cp.*|ppc|retargeting|paid.*)$")
then 'Paid Search'

-- Paid Social:
-- Source matches a regex list of social sites
-- AND
-- Medium matches regex ^(.*cp.*|ppc|retargeting|paid.*)$
when {{source_category}} = 'SOURCE_CATEGORY_SOCIAL'
and REGEXP_CONTAINS({{medium}}, r"^(.*cp.*|ppc|retargeting|paid.*)$")
then 'Paid Social'

-- Paid Video:
-- Source matches a list of video sites
-- AND
-- Medium matches regex ^(.*cp.*|ppc|retargeting|paid.*)$
when {{source_category}} = 'SOURCE_CATEGORY_VIDEO'
and REGEXP_CONTAINS({{medium}},r"^(.*cp.*|ppc|retargeting|paid.*)$")
then 'Paid Video'
when
REGEXP_CONTAINS({{medium}}, r"^(display|cpm|banner)$")
or {{source}} = 'dv360_display'

-- Display:
-- Medium is one of ("display", "banner", "expandable", "interstitial", "cpm")
when {{medium}} in ('display', 'banner', 'expandable', 'interstitial', 'cpm')
then 'Display'
when
{{source_category}} = 'SOURCE_CATEGORY_SEARCH'
and REGEXP_CONTAINS({{medium}}, r"^(.*cp.*|ppc|retargeting|paid.*)$")
then 'Paid Search'
when
REGEXP_CONTAINS({{medium}}, r"^(cpv|cpa|cpp|content-text)$")
then 'Other Advertising'
when
{{medium}} = 'organic' or {{source_category}} = 'SOURCE_CATEGORY_SEARCH'
then 'Organic Search'
when
{{source_category}} = 'SOURCE_CATEGORY_VIDEO'
or REGEXP_CONTAINS({{medium}}, r"^(.*video.*)$")
then 'Organic Video'
when
{{source_category}} = 'SOURCE_CATEGORY_SHOPPING'

-- Paid Other:
-- Medium matches regex ^(.*cp.*|ppc|retargeting|paid.*)$
when REGEXP_CONTAINS({{medium}}, r"^(.*cp.*|ppc|retargeting|paid.*)$")
then 'Paid Other'

-- Organic Shopping:
-- Source matches a list of shopping sites
-- OR
-- Campaign name matches regex ^(.*(([^a-df-z]|^)shop|shopping).*)$
when {{source_category}} = 'SOURCE_CATEGORY_SHOPPING'
or REGEXP_CONTAINS({{campaign}}, r"^(.*(([^a-df-z]|^)shop|shopping).*)$")
then 'Organic Shopping'
when
{{medium}} in ("referral", "app", "link")

-- Organic Social:
-- Source matches a regex list of social sites
-- OR
-- Medium is one of ("social", "social-network", "social-media", "sm", "social network", "social media")
when {{source_category}} = 'SOURCE_CATEGORY_SOCIAL'
or {{medium}} in ("social","social-network","social-media","sm","social network","social media")
then 'Organic Social'

-- Organic Video:
-- Source matches a list of video sites
-- OR
-- Medium matches regex ^(.*video.*)$
when {{source_category}} = 'SOURCE_CATEGORY_VIDEO'
or REGEXP_CONTAINS({{medium}}, r"^(.*video.*)$")
then 'Organic Video'

-- Organic Search:
-- Source matches a list of search sites
-- OR
-- Medium exactly matches organic
when {{source_category}} = 'SOURCE_CATEGORY_SEARCH' or {{medium}} = 'organic'
then 'Organic Search'

-- Referral:
-- Medium is one of ("referral", "app", or "link")
when {{medium}} in ("referral", "app", "link")
then 'Referral'
when
{{medium}} = 'audio'

-- Email:
-- Source = email|e-mail|e_mail|e mail
-- OR
-- Medium = email|e-mail|e_mail|e mail
when REGEXP_CONTAINS({{source}}, r"email|e-mail|e_mail|e mail")
or REGEXP_CONTAINS({{medium}}, r"email|e-mail|e_mail|e mail")
then 'Email'

-- Affiliates:
-- Medium = affiliate
when {{medium}} = 'affiliate'
then 'Affiliates'

-- Audio:
-- Medium exactly matches audio
when {{medium}} = 'audio'
then 'Audio'
when
{{medium}} = 'sms'
or {{source}} = 'sms'

-- SMS:
-- Source exactly matches sms
-- OR
-- Medium exactly matches sms
when {{source}} = 'sms'
or {{medium}} = 'sms'
then 'SMS'
when
REGEXP_CONTAINS({{medium}}, r"(mobile|notification|push$)") or {{source}} = 'firebase'
then 'Push Notifications'
else '(Other)'
end

-- Mobile Push Notifications:
-- Medium ends with "push"
-- OR
-- Medium contains "mobile" or "notification"
-- OR
-- Source exactly matches "firebase"
when REGEXP_CONTAINS({{medium}}, r"push$")
or REGEXP_CONTAINS({{medium}}, r"mobile|notification")
or {{source}} = 'firebase'
then 'Mobile Push Notifications'

-- Unassigned is the value Analytics uses when there are no other channel rules that match the event data.
else 'Unassigned'
end

{% endmacro %}
6 changes: 3 additions & 3 deletions models/staging/stg_ga4__sessions_traffic_sources.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
with session_events as (
select
select
session_key
,event_timestamp
,events.event_source
Expand All @@ -17,11 +17,11 @@ with session_events as (
set_default_channel_grouping as (
select
*
,{{ga4.default_channel_grouping('event_source','event_medium','source_category')}} as default_channel_grouping
,{{ga4.default_channel_grouping('event_source','event_medium','source_category', 'event_campaign')}} as default_channel_grouping
from session_events
),
session_source as (
select
select
session_key
,COALESCE(FIRST_VALUE((CASE WHEN event_source <> '(direct)' THEN event_source END) IGNORE NULLS) OVER (session_window), '(direct)') AS session_source
,COALESCE(FIRST_VALUE((CASE WHEN event_source <> '(direct)' THEN COALESCE(event_medium, '(none)') END) IGNORE NULLS) OVER (session_window), '(none)') AS session_medium
Expand Down
10 changes: 5 additions & 5 deletions models/staging/stg_ga4__sessions_traffic_sources_daily.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


with session_events as (
select
select
client_key
,session_partition_key
,event_date_dt as session_partition_date
Expand All @@ -42,11 +42,11 @@ with session_events as (
set_default_channel_grouping as (
select
*
,{{ga4.default_channel_grouping('event_source','event_medium','source_category')}} as default_channel_grouping
,{{ga4.default_channel_grouping('event_source','event_medium','source_category', 'event_campaign')}} as default_channel_grouping
from session_events
),
first_session_source as (
select
select
client_key
,session_partition_key
,session_partition_date
Expand All @@ -63,13 +63,13 @@ first_session_source as (
),
find_non_direct_session_partition_key as (

select
select
*
,if(session_source <> '(direct)', session_partition_key, null) as non_direct_session_partition_key --provide the session_partition_key only if source is not direct. Useful for last non-direct attribution modeling
from first_session_source
)

select
select
client_key
,session_partition_key
,session_partition_date
Expand Down
8 changes: 4 additions & 4 deletions seeds/ga4_source_categories.csv
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ crackle.com,SOURCE_CATEGORY_VIDEO
cross.tv,SOURCE_CATEGORY_SOCIAL
crunchyroll,SOURCE_CATEGORY_SOCIAL
crunchyroll.com,SOURCE_CATEGORY_SOCIAL
cse.google.com,SOURCE_CATEGORY_SEARCH
curiositystream,SOURCE_CATEGORY_VIDEO
curiositystream.com,SOURCE_CATEGORY_VIDEO
cyworld,SOURCE_CATEGORY_SOCIAL
Expand Down Expand Up @@ -314,9 +313,7 @@ goodreads.com,SOURCE_CATEGORY_SOCIAL
google,SOURCE_CATEGORY_SEARCH
google+,SOURCE_CATEGORY_SOCIAL
google-play,SOURCE_CATEGORY_SEARCH
google.com,SOURCE_CATEGORY_SEARCH
googlegroups.com,SOURCE_CATEGORY_SOCIAL
googlemybusiness,SOURCE_CATEGORY_SEARCH
googleplus,SOURCE_CATEGORY_SOCIAL
govloop,SOURCE_CATEGORY_SOCIAL
govloop.com,SOURCE_CATEGORY_SOCIAL
Expand Down Expand Up @@ -398,6 +395,7 @@ l.facebook.com,SOURCE_CATEGORY_SOCIAL
l.instagram.com,SOURCE_CATEGORY_SOCIAL
l.messenger.com,SOURCE_CATEGORY_SOCIAL
last.fm,SOURCE_CATEGORY_SOCIAL
lens.google.com,SOURCE_CATEGORY_SEARCH
librarything,SOURCE_CATEGORY_SOCIAL
librarything.com,SOURCE_CATEGORY_SOCIAL
lifestream.aol.com,SOURCE_CATEGORY_SOCIAL
Expand Down Expand Up @@ -431,7 +429,6 @@ m.twitch.tv,SOURCE_CATEGORY_VIDEO
m.vk.com,SOURCE_CATEGORY_SOCIAL
m.yelp.com,SOURCE_CATEGORY_SOCIAL
m.youtube.com,SOURCE_CATEGORY_VIDEO
mail.google.com,SOURCE_CATEGORY_SEARCH
mail.rambler.ru,SOURCE_CATEGORY_SEARCH
mail.yandex.ru,SOURCE_CATEGORY_SEARCH
malaysia.search.yahoo.com,SOURCE_CATEGORY_SEARCH
Expand Down Expand Up @@ -555,6 +552,7 @@ pinterest.se,SOURCE_CATEGORY_SOCIAL
pixiv.net,SOURCE_CATEGORY_SOCIAL
pl.pinterest.com,SOURCE_CATEGORY_SOCIAL
pl.search.yahoo.com,SOURCE_CATEGORY_SEARCH
play.google.com,SOURCE_CATEGORY_SEARCH
playahead.se,SOURCE_CATEGORY_SOCIAL
player.twitch.tv,SOURCE_CATEGORY_VIDEO
player.vimeo.com,SOURCE_CATEGORY_VIDEO
Expand Down Expand Up @@ -645,6 +643,7 @@ smartnews.com,SOURCE_CATEGORY_SOCIAL
snapchat,SOURCE_CATEGORY_SOCIAL
snapchat.com,SOURCE_CATEGORY_SOCIAL
so.com,SOURCE_CATEGORY_SEARCH
social,SOURCE_CATEGORY_SOCIAL
sociallife.com.br,SOURCE_CATEGORY_SOCIAL
socialvibe,SOURCE_CATEGORY_SOCIAL
socialvibe.com,SOURCE_CATEGORY_SOCIAL
Expand Down Expand Up @@ -710,6 +709,7 @@ tripadvisor,SOURCE_CATEGORY_SOCIAL
tripadvisor.com,SOURCE_CATEGORY_SOCIAL
trombi,SOURCE_CATEGORY_SOCIAL
trombi.com,SOURCE_CATEGORY_SOCIAL
trustpilot,SOURCE_CATEGORY_SOCIAL
tudou,SOURCE_CATEGORY_SOCIAL
tudou.com,SOURCE_CATEGORY_SOCIAL
tuenti,SOURCE_CATEGORY_SOCIAL
Expand Down
Loading