Based on a post by Robert Sahlin, I want to use a BigQuery UDF to access any Google Analytics custom dimension in BigQuery by its index. In the proposed solution Robert uses a JavaScript UDF, and I'm wondering if it's possible to do the same with a SQL UDF - since a SQL UDF should perform better than a JS one.
The proposed 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