channel_attribution - BigQuery

Function

channel_attribution(source, medium, campaign_name)

Description

Channel Attribution - Performs channel attribution using the <source>, <medium>, and <campaign_name>.

Example Query


SELECT `justfunctions.eu.channel_attribution`("shopify", "paid", "test")
                                            
/*--Output--
paid shopping
*/

Statement

CREATE OR REPLACE FUNCTION `your_project_id.your_dataset_id.channel_attribution`(`source` string, `medium` string, `campaign_name` string) 
  RETURNS string AS (CASE
  -------direct
  WHEN  source IS NULL AND medium IS NULL THEN 'direct'
  WHEN  source='' AND medium='' THEN 'direct'
  WHEN  source IN ('(direct)','direct') AND medium IN ('(none)','none','(not set)','not set','') THEN 'direct'
  
  -------cross-network
  WHEN REGEXP_CONTAINS(campaign_name, 'cross-network') THEN 'cross-network'
  
  -------paid-shopping
  WHEN (
    REGEXP_CONTAINS(
      source,
      'alibaba|amazon|google shopping|shopify|etsy|ebay|stripe|walmart'
    )
    OR REGEXP_CONTAINS(
      campaign_name,
      '^(.*(([^a-df-z]|^)shop|shopping).*)$'
    )
  )
  AND REGEXP_CONTAINS(medium, '^(.*cp.*|ppc|retargeting|paid.*)$') THEN 'paid shopping'
 
  -------paid search
  WHEN REGEXP_CONTAINS(
    source,
    'baidu|bing|duckduckgo|ecosia|google|yahoo|yandex'
  )
  AND REGEXP_CONTAINS(medium, '^(.*cp.*|ppc|retargeting|paid.*)$') THEN 'paid search'
  
  -------paid social
  WHEN REGEXP_CONTAINS(
    source,
    'badoo|facebook|fb|instagram|linkedin|pinterest|tiktok|twitter|whatsapp'
  )
  AND REGEXP_CONTAINS(medium, '^(.*cp.*|ppc|retargeting|paid.*)$') THEN 'paid social'

  -------paid video
  WHEN REGEXP_CONTAINS(
    source,
    'dailymotion|disneyplus|netflix|youtube|vimeo|twitch|vimeo|youtube'
  )
  AND REGEXP_CONTAINS(medium, '^(.*cp.*|ppc|retargeting|paid.*)$') THEN 'paid video'

  -------display
  WHEN medium IN (
    'display',
    'banner',
    'expandable',
    'interstitial',
    'cpm'
  ) THEN 'display'

  -------paid other
  WHEN REGEXP_CONTAINS(medium, r"^(.*cp.*|ppc|retargeting|paid.*)$")
   THEN 'paid other'

  -------organic shopping
  WHEN REGEXP_CONTAINS(
    source,
    'alibaba|amazon|google shopping|shopify|etsy|ebay|stripe|walmart'
  )
  OR REGEXP_CONTAINS(
    campaign_name,
    '^(.*(([^a-df-z]|^)shop|shopping).*)$'
  ) THEN 'organic shopping'
  -------organic social
  WHEN REGEXP_CONTAINS(
    source,
    'badoo|facebook|fb|instagram|linkedin|pinterest|tiktok|twitter|whatsapp'
  )
  OR medium IN (
    'social',
    'social-network',
    'social-media',
    'sm',
    'social network',
    'social media'
  ) THEN 'organic social'
  -------organic video
  WHEN REGEXP_CONTAINS(
    source,
    'dailymotion|disneyplus|netflix|youtube|vimeo|twitch|vimeo|youtube'
  )
  OR REGEXP_CONTAINS(medium, '^(.*video.*)$') THEN 'organic video'
  -------organic search
  WHEN REGEXP_CONTAINS(
    source,
    'baidu|bing|duckduckgo|ecosia|google|yahoo|yandex'
  )
  OR medium = 'organic' THEN 'organic search'
  -- Other
  WHEN medium IN ("referral", "app", "link") THEN 'referral'
  WHEN REGEXP_CONTAINS(source, 'email|e-mail|e_mail|e mail')
  OR REGEXP_CONTAINS(medium, 'email|e-mail|e_mail|e mail') THEN 'email'
  WHEN medium = 'affiliate' THEN 'affiliates'
  WHEN medium = 'audio' THEN 'audio'
  WHEN medium = 'sms' THEN 'sms'
  WHEN REGEXP_CONTAINS(medium, r'push$|mobile|notification') THEN 'mobile push notifications'
  ELSE 'unassigned'
END
)
  OPTIONS ( description = '''Performs channel attribution using the <source>, <medium>, and <campaign_name>.''')

Regions

justfunctions.eu.channel_attribution(source, medium, campaign_name),
justfunctions.us.channel_attribution(source, medium, campaign_name)

Type

SQL User Defined Function (SQL UDF)

Related Functions

How to Use

Frequently Asked Questions

User-Defined Functions (UDFs) in Google BigQuery are custom functions that you can create to perform operations that aren't available through the standard SQL functions. These UDFs allow you to extend BigQuery's SQL capabilities to suit your specific data processing needs. JustFunctions is a collection of open-source user-defined functions (UDFs).

JustFunctions is a collection of open-source User-Defined Functions (UDFs) designed to extend the capabilities of Google BigQuery. These functions cover a wide range of applications, including text manipulation, URL processing, date processing, email handling, similarity measures, and more. Moreover, JustFunctions is frequently updated to include more use cases.

We welcome any feedback or questions you may have. You can Contact us or report an issue on Github.

Functions and procedures from JustFunctions can be used directly in any of your projects.
To start, simply click 👆 on any function,  Copy  the 'Example Query' and run it in your BigQuery console.
You can also  Copy  the 'Statement' to create your own private user-defined function.

Yes, JustFunctions is completely free to use.

Yes, currently JustFunctions is only available for Google BigQuery. In the future, we will also support PostgreSQL.

See something wrong? Contact us or report an issue on Github.