Google Analytics stream data into bigquery in a nested json format, it make sometimes difficult for the users to flatten custom dimension data for each event, this can be overcome by using below custom dimension temp function (Standard SQL only). We can pass customDimensions.index and customDimensions.value as parameter for temp function.
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 visitStarttime, visitId, visitNumber,
hit.hitNumber AS session_hit_count,
hit.type AS hit_type,
hit.page.hostname url_domain_name,
hit.page,
customDimensionByIndex(165, hit.customDimensions) AS custom_variable_1
FROM
`project.dataset.ga_sessions_20180909`,
UNNEST(hits) AS hit
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 visitStarttime, visitId, visitNumber,
hit.hitNumber AS session_hit_count,
hit.type AS hit_type,
hit.page.hostname url_domain_name,
hit.page,
customDimensionByIndex(165, hit.customDimensions) AS custom_variable_1
FROM
`project.dataset.ga_sessions_20180909`,
UNNEST(hits) AS hit
No comments:
Post a Comment