WITH data AS(
select ARRAY<STRUCT<index INT64, value STRING>> [STRUCT(NULL as index, '' as value), STRUCT(0 as index, 'test' as value), STRUCT(2 as index, 'user_1' as value)] customDimensions, ARRAY<STRUCT<page STRUCT<pagePath STRING>, eventinfo STRUCT<eventcategory STRING, eventaction STRING> >> [STRUCT(STRUCT('/home' as pagePath) as page, STRUCT("cat1" as eventcategory, "act1" as eventaction) as eventinfo), STRUCT(STRUCT('/abcshortsabc' as pagePath) as page, STRUCT("SIZE Filter Click" as eventcategory, "S" as eventaction) as eventinfo), STRUCT(STRUCT('/abcshortsabc' as pagePath) as page, STRUCT("SIZE Filter Click" as eventcategory, "S" as eventaction) as eventinfo), STRUCT(STRUCT('/abcshortsabc' as pagePath) as page, STRUCT("SIZE Filter Click" as eventcategory, "M" as eventaction) as eventinfo), STRUCT(STRUCT('/abc/t-shirtsabc' as pagePath) as page, STRUCT("SIZE Filter Click" as eventcategory, "L" as eventaction) as eventinfo)] hits union all
select ARRAY<STRUCT<index INT64, value STRING>> [STRUCT(2 as index, 'user_2' as value)] customDimensions, ARRAY<STRUCT<page STRUCT<pagePath STRING>, eventinfo STRUCT<eventcategory STRING, eventaction STRING> >> [STRUCT(STRUCT('shorts' as pagePath) as page, STRUCT("cat1" as eventcategory, "act1" as eventaction) as eventinfo), STRUCT(STRUCT('/abcshortsabc' as pagePath) as page, STRUCT("SIZE Filter Click" as eventcategory, "M" as eventaction) as eventinfo), STRUCT(STRUCT('/abcshortsabc' as pagePath) as page, STRUCT("SIZE Filter Click" as eventcategory, "M" as eventaction) as eventinfo), STRUCT(STRUCT('/abcshortsabc' as pagePath) as page, STRUCT("SIZE Filter Click" as eventcategory, "M" as eventaction) as eventinfo), STRUCT(STRUCT('/abc/t-shirtsabc' as pagePath) as page, STRUCT("SIZE Filter Click" as eventcategory, "L" as eventaction) as eventinfo)] hits
)
SELECT
(SELECT value FROM UNNEST(customDimensions) WHERE index = 2 GROUP BY value) UserID,
(SELECT eventinfo.eventaction size FROM UNNEST(hits) WHERE (REGEXP_CONTAINS(page.pagepath, r'shorts') OR REGEXP_CONTAINS(page.pagepath, r'/t-shirts')) AND eventinfo.eventcategory = 'SIZE Filter Click' GROUP BY eventinfo.eventaction ORDER BY COUNT(eventinfo.eventaction) DESC LIMIT 1) most_clicked_size
FROM data
如果您还想检索最频繁标签的总点击量(在您的案例中的操作),您还可以执行以下操作:
SELECT
(SELECT value FROM UNNEST(customDimensions) WHERE index = 2 GROUP BY value) UserID,
(SELECT AS STRUCT eventinfo.eventaction size, count(1) freq FROM UNNEST(hits) WHERE (REGEXP_CONTAINS(page.pagepath, r'shorts') OR REGEXP_CONTAINS(page.pagepath, r'/t-shirts')) AND eventinfo.eventcategory = 'SIZE Filter Click' GROUP BY eventinfo.eventaction ORDER BY COUNT(eventinfo.eventaction) DESC LIMIT 1) most_clicked_size
FROM data
data
是对实际ga会话数据的模拟。