-
-
Save BasLinders/c55388fd1680afed0639be570c633d84 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| Query for connecting GSC results with experiment results through BigQuery. | |
| */ | |
| -- Binomial counts for conversions, visitors with AOV, and SEO query segmentation | |
| DECLARE start_date STRING DEFAULT 'YYYY-MM-DD'; | |
| DECLARE end_date STRING DEFAULT 'YYYY-MM-DD'; | |
| DECLARE variant_a STRING DEFAULT 'A-string'; | |
| DECLARE variant_b STRING DEFAULT 'B-string'; | |
| -- Pull data from GSC | |
| WITH gsc_data AS ( | |
| SELECT | |
| query, | |
| page AS landing_page, | |
| SUM(clicks) as total_clicks, | |
| SUM(impressions) as total_impressions | |
| FROM | |
| `bq-account.search-console-table.searchdata_all_pages_*` -- Substitute for the correct table | |
| WHERE | |
| _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', start_date)) | |
| AND FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', end_date)) | |
| GROUP BY query, page | |
| ), | |
| -- Get experiment variant assignments (user-level) | |
| variant_data AS ( | |
| SELECT | |
| user_pseudo_id, | |
| MAX(CASE | |
| WHEN params.value.string_value = variant_a THEN 'A' | |
| WHEN params.value.string_value = variant_b THEN 'B' | |
| ELSE 'Other' | |
| END) AS experience_variant_label | |
| FROM | |
| `bq-account.ga4-table.events_*`, -- Substitute for the correct table | |
| UNNEST(event_params) AS params | |
| WHERE | |
| _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', start_date)) | |
| AND FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', end_date)) | |
| AND params.key = 'exp_variant_string' | |
| AND params.value.string_value IS NOT NULL | |
| AND params.value.string_value IN (variant_a, variant_b) -- Only include actual test variants | |
| GROUP BY user_pseudo_id | |
| ), | |
| -- Get organic search users and their first search query (user-level attribution) | |
| organic_users AS ( | |
| SELECT | |
| user_pseudo_id, | |
| MIN(event_timestamp) as first_organic_visit, | |
| ARRAY_AGG( | |
| (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') | |
| ORDER BY event_timestamp ASC LIMIT 1 | |
| )[OFFSET(0)] AS first_landing_page | |
| FROM | |
| `bq-account.ga4-table.events_*` -- Substitute for the correct table | |
| WHERE | |
| _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', start_date)) | |
| AND FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', end_date)) | |
| AND event_name = 'page_view' | |
| AND traffic_source.medium = 'organic' | |
| AND traffic_source.source = 'google' | |
| GROUP BY user_pseudo_id | |
| ), | |
| -- Link users to their initial search query | |
| user_search_queries AS ( | |
| SELECT DISTINCT | |
| ou.user_pseudo_id, | |
| gsc.query AS search_query | |
| FROM | |
| organic_users ou | |
| JOIN | |
| gsc_data gsc ON REGEXP_EXTRACT(ou.first_landing_page, r'^[^?]+') = gsc.landing_page -- remove query parameters | |
| ), | |
| -- Get user-level purchase data (aggregate all purchases per user) | |
| user_purchases AS ( | |
| SELECT | |
| user_pseudo_id, | |
| COUNT(DISTINCT ecommerce.transaction_id) AS transaction_count, | |
| SUM(ecommerce.purchase_revenue) AS total_revenue, | |
| SUM(ecommerce.total_item_quantity) AS total_quantity | |
| FROM | |
| `bq-account.ga4-table.events_*` -- Substitute for the correct table | |
| WHERE | |
| _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', start_date)) | |
| AND FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', end_date)) | |
| AND event_name = 'purchase' | |
| AND ecommerce.transaction_id IS NOT NULL | |
| GROUP BY user_pseudo_id | |
| ), | |
| -- Get users who added to cart | |
| user_add_to_cart AS ( | |
| SELECT DISTINCT | |
| user_pseudo_id, | |
| 1 as has_added_to_cart | |
| FROM | |
| `bq-account.ga4-table.events_*` -- Substitute for the correct table | |
| WHERE | |
| _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', start_date)) | |
| AND FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', end_date)) | |
| AND event_name = 'add_to_cart' | |
| ), | |
| -- Get user device preference (most used device) | |
| user_devices AS ( | |
| SELECT | |
| user_pseudo_id, | |
| CASE | |
| WHEN COUNTIF(device.category = 'mobile') > COUNTIF(device.category = 'desktop') THEN 'mobile' | |
| WHEN COUNTIF(device.category = 'desktop') > COUNTIF(device.category = 'mobile') THEN 'desktop' | |
| ELSE 'mixed' | |
| END AS primary_device | |
| FROM | |
| `bq-account.ga4-table.events_*` -- Substitute for the correct table | |
| WHERE | |
| _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', start_date)) | |
| AND FORMAT_DATE('%Y%m%d', PARSE_DATE('%Y-%m-%d', end_date)) | |
| GROUP BY user_pseudo_id | |
| ), | |
| -- Create user-level dataset for A/B test analysis | |
| user_level_data AS ( | |
| SELECT | |
| vd.user_pseudo_id, | |
| vd.experience_variant_label, | |
| usq.search_query, | |
| ud.primary_device, | |
| COALESCE(up.transaction_count, 0) AS transaction_count, | |
| COALESCE(up.total_revenue, 0) AS total_revenue, | |
| COALESCE(up.total_quantity, 0) AS total_quantity, | |
| COALESCE(uatc.has_added_to_cart, 0) AS has_added_to_cart, | |
| -- Binary conversion flag (purchased or not) | |
| CASE WHEN up.user_pseudo_id IS NOT NULL THEN 1 ELSE 0 END AS has_converted | |
| FROM | |
| variant_data vd | |
| LEFT JOIN | |
| user_search_queries usq ON vd.user_pseudo_id = usq.user_pseudo_id | |
| LEFT JOIN | |
| user_purchases up ON vd.user_pseudo_id = up.user_pseudo_id | |
| LEFT JOIN | |
| user_add_to_cart uatc ON vd.user_pseudo_id = uatc.user_pseudo_id | |
| LEFT JOIN | |
| user_devices ud ON vd.user_pseudo_id = ud.user_pseudo_id | |
| WHERE | |
| usq.search_query IS NOT NULL -- Only include users who came from organic search | |
| ), | |
| -- Experiment results by search query | |
| query_level_results AS ( | |
| SELECT | |
| experience_variant_label, | |
| search_query, | |
| COUNT(user_pseudo_id) AS total_users, | |
| SUM(has_converted) AS conversions, | |
| SUM(total_revenue) AS total_revenue, | |
| SUM(has_added_to_cart) AS add_to_cart_users, | |
| SUM(transaction_count) AS total_transactions, | |
| COUNT(CASE WHEN primary_device = 'mobile' THEN user_pseudo_id END) AS mobile_users, | |
| SUM(CASE WHEN primary_device = 'mobile' AND has_converted = 1 THEN 1 ELSE 0 END) AS mobile_conversions, | |
| -- Key test metrics | |
| ROUND(SAFE_DIVIDE(SUM(has_converted), COUNT(user_pseudo_id)), 2) AS conversion_rate, | |
| ROUND(SAFE_DIVIDE(SUM(total_revenue), SUM(has_converted)), 2) AS average_order_value, | |
| ROUND(SAFE_DIVIDE(SUM(total_revenue), COUNT(user_pseudo_id)), 2) AS revenue_per_user, | |
| ROUND(SAFE_DIVIDE(SUM(has_added_to_cart), COUNT(user_pseudo_id)), 2) AS add_to_cart_rate, | |
| ROUND(SAFE_DIVIDE(SUM(CASE WHEN primary_device = 'mobile' AND has_converted = 1 THEN 1 ELSE 0 END), | |
| COUNT(CASE WHEN primary_device = 'mobile' THEN user_pseudo_id END)), 2) AS mobile_conversion_rate | |
| FROM | |
| user_level_data | |
| GROUP BY | |
| experience_variant_label, | |
| search_query | |
| HAVING | |
| COUNT(user_pseudo_id) >= 10 -- Minimum users for statistical relevance | |
| ), | |
| -- Overall A/B test results (across all queries) | |
| overall_results AS ( | |
| SELECT | |
| experience_variant_label, | |
| 'ALL_QUERIES' AS search_query, | |
| COUNT(user_pseudo_id) AS total_users, | |
| SUM(has_converted) AS conversions, | |
| SUM(total_revenue) AS total_revenue, | |
| SUM(has_added_to_cart) AS add_to_cart_users, | |
| SUM(transaction_count) AS total_transactions, | |
| COUNT(CASE WHEN primary_device = 'mobile' THEN user_pseudo_id END) AS mobile_users, | |
| SUM(CASE WHEN primary_device = 'mobile' AND has_converted = 1 THEN 1 ELSE 0 END) AS mobile_conversions, | |
| -- Key A/B test metrics | |
| ROUND(SAFE_DIVIDE(SUM(has_converted), COUNT(user_pseudo_id)), 2) AS conversion_rate, | |
| ROUND(SAFE_DIVIDE(SUM(total_revenue), SUM(has_converted)), 2) AS average_order_value, | |
| ROUND(SAFE_DIVIDE(SUM(total_revenue), COUNT(user_pseudo_id)), 2) AS revenue_per_user, | |
| ROUND(SAFE_DIVIDE(SUM(has_added_to_cart), COUNT(user_pseudo_id)), 2) AS add_to_cart_rate, | |
| ROUND(SAFE_DIVIDE(SUM(CASE WHEN primary_device = 'mobile' AND has_converted = 1 THEN 1 ELSE 0 END), | |
| COUNT(CASE WHEN primary_device = 'mobile' THEN user_pseudo_id END)), 2) AS mobile_conversion_rate | |
| FROM | |
| user_level_data | |
| GROUP BY | |
| experience_variant_label | |
| ) | |
| -- Return both query-specific and overall results | |
| SELECT * FROM query_level_results | |
| UNION ALL | |
| SELECT * FROM overall_results | |
| ORDER BY | |
| CASE WHEN search_query = 'ALL_QUERIES' THEN 1 ELSE 0 END, | |
| search_query, | |
| experience_variant_label; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment