查询会话和命中级别的自定义维度
Posted
技术标签:
【中文标题】查询会话和命中级别的自定义维度【英文标题】:Query custom dimensions on the sessions AND the hit level 【发布时间】:2017-10-06 10:04:06 【问题描述】:索引为6
的customDimensions
对应于会话和命中级别的UUID。
在会话级别,我可以使用以下标准 SQL 查询来检索 UUID:
CREATE TEMP FUNCTION customDimensionByIndex(indx INT64, arr ARRAY<STRUCT<index INT64, value STRING>>) AS (
(SELECT x.value FROM UNNEST(arr) x WHERE indx=x.index)
);
SELECT
customDimensionByIndex(6, customDimensions) AS session_uuid -- Customer UUID
FROM `94860076.ga_sessions_20170822`
limit 10
同样,在我可以使用的点击级别上:
CREATE TEMP FUNCTION customDimensionByIndex(indx INT64, arr ARRAY<STRUCT<index INT64, value STRING>>) AS (
(SELECT x.value FROM UNNEST(arr) x WHERE indx=x.index)
);
SELECT
customDimensionByIndex(6, hits.customDimensions) AS hit_uuid -- Customer UUID
FROM `94860076.ga_sessions_20170822`, unnest(hits) as hits
limit 10
但是,我未能在同一个查询中同时使用两者。例如,我想要一个结果集,其中每一行对应一个会话,列是session_uuid
和array_of_hit_uuids
。如何实现?
【问题讨论】:
【参考方案1】:以下是 BigQuery 标准 SQL
#standardSQL
CREATE TEMP FUNCTION customDimensionByIndex(indx INT64, arr ARRAY<STRUCT<index INT64, value STRING>>) AS (
(SELECT x.value FROM UNNEST(arr) x WHERE indx=x.index)
);
SELECT *
FROM (
SELECT
customDimensionByIndex(6, customDimensions) AS session_uuid,
ARRAY(
SELECT val FROM (
SELECT customDimensionByIndex(6, hits.customDimensions) AS val
FROM UNNEST(hits) AS hits
)
WHERE NOT val IS NULL
) AS hit_uuid
FROM `94860076.ga_sessions_20170822`
)
WHERE session_uuid IS NOT NULL
LIMIT 10
您可以使用公共数据集对其进行测试
#standardSQL
CREATE TEMP FUNCTION customDimensionByIndex(indx INT64, arr ARRAY<STRUCT<index INT64, value STRING>>) AS (
(SELECT x.value FROM UNNEST(arr) x WHERE indx=x.index)
);
SELECT *
FROM (
SELECT
customDimensionByIndex(2, customDimensions) AS session_uuid,
ARRAY(
SELECT val FROM (
SELECT customDimensionByIndex(1, hits.customDimensions) AS val
FROM UNNEST(hits) AS hits
)
WHERE NOT val IS NULL
) AS hit_uuid
FROM `google.com:analytics-bigquery.LondonCycleHelmet.ga_sessions_20130910`
)
WHERE session_uuid IS NOT NULL
LIMIT 10
【讨论】:
内部连接不是更快吗? 很可能不会——但你可以试试 :) 同时,你在上面试过了吗?对你有用吗? 这似乎可以解决问题,但最终,我使用了不同的方法。您的方法生成的数组不是我想要的。您的方法产生了一个以会话为中心的视图,其中命中映射到数组。对吗?以上是关于查询会话和命中级别的自定义维度的主要内容,如果未能解决你的问题,请参考以下文章
取消嵌套命中和取消嵌套会话范围的自定义维度 BigQuery 代码过滤器