基于a post by Robert Sahlin,我想使用BigQuery UDF通过索引访问BigQuery中的任何Google Analytics自定义维度。在提议的解决方案中,Robert使用JavaScript UDF,我想知道是否可以对SQL UDF执行相同操作 - 因为SQL UDF应该比JS UDF执行得更好。
建议的JS UDF:
CREATE TEMPORARY FUNCTION customDimensionByIndex(index INT64, arr ARRAY<STRUCT<index INT64, value STRING>>)
RETURNS STRING
LANGUAGE js AS """
for (var j = 0; j < arr.length; j++){
if(arr[j].index == index){
return arr[j].value;
}
}
""";
SELECT
fullvisitorId,
visitId,
hit.hitnumber,
customDimensionByIndex(6, hit.customDimensions) as author,
customDimensionByIndex(7, hit.customDimensions) as category
FROM `123456.ga_sessions_YYYYMMDD`
JOIN
UNNEST(hits) as hit
答案 0 :(得分:6)
使用SQL UDF:
#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
fullvisitorId,
visitId,
hit.hitnumber,
customDimensionByIndex(1, hit.customDimensions),
customDimensionByIndex(2, hit.customDimensions),
customDimensionByIndex(3, hit.customDimensions)
FROM `google.com:analytics-bigquery.LondonCycleHelmet.ga_sessions_20130910`, UNNEST(hits) hit
LIMIT 1000
我不确定为什么原始解决方案会在&#34;点击&#34;而不是列&#34;命中&#34;在示例数据集上 - 所以为了获得单独的命中,我也必须使用UNNEST()它们。