Skip to content

Instantly share code, notes, and snippets.

@ryandhubbard
Created October 10, 2023 20:39
Show Gist options
  • Save ryandhubbard/32fdad2f8967eb7091ad83b042be6c8f to your computer and use it in GitHub Desktop.
Save ryandhubbard/32fdad2f8967eb7091ad83b042be6c8f to your computer and use it in GitHub Desktop.
get yesterdays user counts for conversion funnel steps from big query
SELECT
PARSE_DATE("%Y%m%d", event_date) as date,
geo.country as country,
case
WHEN DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)) = DATE(TIMESTAMP_MICROS(event_timestamp)) THEN 'New Visitor'
ELSE 'Returning Visitor'
END AS user_type,
COALESCE(collected_traffic_source.manual_medium) as medium,
COALESCE(collected_traffic_source.manual_source) as source,
COALESCE(collected_traffic_source.manual_campaign_name) as campaign,
device.category as device,
COUNT(DISTINCT(
SELECT value.int_value
FROM UNNEST(event_params)
WHERE key = 'ga_session_id'
)) AS sessions,
COUNT(DISTINCT(
SELECT value.int_value
FROM UNNEST (event_params)
WHERE event_name = 'view_item_list' AND key = 'ga_session_id'
)) as view_item_list,
COUNT(DISTINCT(
SELECT value.int_value
FROM UNNEST (event_params)
WHERE event_name = 'view_item' AND key = 'ga_session_id'
)) as view_item,
COUNT(DISTINCT(
SELECT value.int_value
FROM UNNEST (event_params)
WHERE event_name = 'add_to_cart' AND key = 'ga_session_id'
)) as add_to_cart,
COUNT(DISTINCT(
SELECT value.int_value
FROM UNNEST (event_params)
WHERE event_name = 'begin_checkout' AND key = 'ga_session_id'
)) as begin_checkout,
COUNT(DISTINCT(
SELECT value.int_value
FROM UNNEST (event_params)
WHERE event_name = 'purchase' AND key = 'ga_session_id'
)) as purchase,
FROM 'project'
join UNNEST (event_params)
GROUP BY
date,
country,
user_type,
medium,
source,
campaign,
Device
HAVING date > DATE_SUB(DATE(CURRENT_DATETIME), INTERVAL 2 DAY) and date < DATE(CURRENT_DATETIME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment