Skip to content

Instantly share code, notes, and snippets.

View shawn-jung's full-sized avatar

Shawn Jonghyuck Jung shawn-jung

View GitHub Profile
@markrittman
markrittman / multi_channel_attribution.sql
Created February 8, 2020 22:56
Multi-Channel Attribution Standard SQL query for Google BigQuery sourcing session data from Segment and attributed value from operational system
WITH session_attributiON AS
(SELECT *,
CASE WHEN session_id = LAST_VALUE(session_id)
OVER (partitiON by user_id order by session_start_tstamp ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) THEN 1
ELSE 0
END AS LAST_click_attrib_pct,
CASE WHEN session_id = FIRST_VALUE(session_id)
OVER (partitiON by user_id order by session_start_tstamp ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) THEN 1
ELSE 0
END AS first_click_attrib_pct,