Google Analytics stream data into bigquery in a nested json format, it make sometimes difficult for the users to flatten custom metrics data for each event, this can be overcome by using below custom dimension temp function (Standard SQL only). We can pass customMetrics.index and customMetrics.value as parameter for temp function.
CREATE TEMP FUNCTION
customMetricByIndex(indx INT64,
arr ARRAY<STRUCT<index INT64,
value INT64>>) 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,
customMetricByIndex(3, hit.customMetrics) AS custom_metrics_1
FROM
`project.dataset.ga_sessions_20180909`,
UNNEST(hits) AS hit
CREATE TEMP FUNCTION
customMetricByIndex(indx INT64,
arr ARRAY<STRUCT<index INT64,
value INT64>>) 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,
customMetricByIndex(3, hit.customMetrics) AS custom_metrics_1
FROM
`project.dataset.ga_sessions_20180909`,
UNNEST(hits) AS hit