SELECT
'desktop' AS device,
p.productSKU as listing_id,
CAST(REGEXP_EXTRACT(pcd.value, r'"page":([0-9]+),') AS INT64) page,
CAST(REGEXP_EXTRACT(pcd.value, r'"limit":([0-9]+),') AS INT64) page_limit,
CAST(REGEXP_EXTRACT(pcd.value, r'"rank":([0-9]+)') AS INT64) rank,
SUM(IF(h.eventInfo.eventAction = 'productImpression',1,0)) impressions,
SUM(IF(h.eventInfo.eventAction = 'productClick',1,0)) clicks
FROM `propertyguru-datalake-v0.103434014.ga_sessions_*`,
UNNEST(hits) as h,
UNNEST(h.product) as p,
UNNEST(p.customDimensions) as pcd
WHERE _TABLE_SUFFIX BETWEEN '20190101' AND '20190103'
AND h.eventInfo.eventCategory = 'EnhancedEcommerce'
AND h.eventInfo.eventAction IN ('productImpression','productClick')
AND h.eventInfo.eventLabel = 'Listing Search'
AND pcd.index = 22
GROUP BY 1,2,3,4,5