Created
July 18, 2021 16:14
-
-
Save markrittman/c392156f9945c53b1923c41c06edb54f to your computer and use it in GitHub Desktop.
SQL Query to list out all sessions in GA4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
user_pseudo_id, | |
TIMESTAMP_MICROS(event_timestamp) AS session_start_ts, | |
CAST(LEAD(TIMESTAMP_MICROS(event_timestamp),1) OVER (PARTITION BY CONCAT(user_pseudo_id) | |
ORDER BY | |
event_timestamp) AS timestamp) AS session_end_ts, | |
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS session_id, | |
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_number') AS session_number, | |
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_referrer') AS referrer, | |
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS landing_page_path, | |
(SELECT value.string_value FROM UNNEST(event_params) WHERE event_name = 'page_view' AND key = 'page_title') AS landing_page_title, | |
traffic_source.name, | |
traffic_source.medium, | |
traffic_source.source, | |
CASE | |
WHEN device.category = "desktop" THEN "desktop" | |
WHEN device.category = "tablet" AND app_info.id IS NULL THEN "tablet-web" | |
WHEN device.category = "mobile" AND app_info.id IS NULL THEN "mobile-web" | |
WHEN device.category = "tablet" AND app_info.id IS NOT NULL THEN "tablet-app" | |
WHEN device.category = "mobile" AND app_info.id IS NOT NULL THEN "mobile-app" | |
END AS device, | |
device.mobile_brand_name, | |
device.mobile_model_name, | |
device.mobile_marketing_name, | |
device.mobile_os_hardware_model, | |
device.operating_system, | |
device.operating_system_version, | |
device.vendor_id, | |
device.advertising_id, | |
device.language, | |
device.is_limited_ad_tracking, | |
device.time_zone_offset_seconds, | |
device.browser, | |
device.browser_version, | |
device.web_info.browser, | |
device.web_info.browser_version, | |
device.web_info.hostname | |
FROM | |
`ra-development.analytics_277223877.events_*` s -- modify to your project | |
WHERE | |
event_name = 'session_start' | |
order by 1,2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment