google-analytics - 查询 session 上的自定义维度和点击级别

标签 google-analytics google-bigquery

索引为 6customDimensions 对应于 session 和点击级别上的 UUID。

在 session 级别,我可以使用以下标准 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 ,列为session_uuidarray_of_hit_uuids。如何实现这一目标?

最佳答案

以下适用于 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

您可以使用公共(public)数据集对其进行测试

#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

关于google-analytics - 查询 session 上的自定义维度和点击级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46603235/

相关文章:

python - 在 DataLab 笔记本中使用 Dataproc 和 Spark BigQuery 连接器时出错

google-analytics - 跟踪页面浏览量而无需重新加载页面

symfony - 未指定范围-Google_Auth_Exception

google-bigquery - 如何使用 BigQuery 查找最常见的二元组?

sql - 具有间隙约束的 BigQuery 平均时间

google-bigquery - Google BigQuery getQueryResults 为有效的作业 ID 返回 404 错误

excel - Excel 错误的 BQ 连接器

ios - iphone - 如何使用 Google Analytics EasyTracker 跟踪事件?

seo - 在 Google Analytics 中添加自定义 cookie 的正确语法是什么?

ios - 无法使用适用于 iOS 的 Google Analytics(分析)