/* Standard SQL */ SELECT DISTINCT user_id, session_date, /* Case when logic to 'clean up' the data into 0 or 1 values */ CASE WHEN global_consent_status IS NULL THEN '0' WHEN global_consent_status='false' THEN '0' ELSE global_consent_status END AS global_consent_status /* Selecting from the table we've just made */ FROM ( SELECT /* Unnesting the custom dimension part of the GA export and returning only values in slot 5 (which we know from the GA interface is User ID) */ (SELECT MAX(IF(index = 5,value,NULL)) FROM UNNEST(customDimensions)) AS user_id, date AS session_date, /* Unnesting the custom dimension field and returning only values in slot 22 (which we know from the GA interface is the Global Consent Status) */ (SELECT MAX(IF(index = 22,value,NULL)) FROM UNNEST(customDimensions)) AS global_consent_status FROM /* Selecting from the GA export tables where the data is coming from */ `hbo-nordic-bigquery-1322.134556033.ga_sessions_*` /* Date range of the query */ WHERE _TABLE_SUFFIX BETWEEN '20181201' AND '20181212') WHERE /* Only return rows with distinct user_id values */ user_id IS NOT NULL AND user_id != '' ORDER BY user_id