【问题标题】:Query custom dimensions on the sessions AND the hit level查询会话和命中级别的自定义维度
【发布时间】:2017-10-06 10:04:06
【问题描述】:

索引为6customDimensions 对应于会话和命中级别的UUID。

在会话级别,我可以使用以下标准 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_uuidarray_of_hit_uuids。如何实现?

【问题讨论】:

    标签: google-analytics google-bigquery


    【解决方案1】:

    以下是 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
    

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

    #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
    

    【讨论】:

    • 内部连接不是更快吗?
    • 很可能不会——但你可以试试 :) 同时,你在上面试过了吗?对你有用吗?
    • 这似乎可以解决问题,但最终,我使用了不同的方法。您的方法生成的数组不是我想要的。您的方法产生了一个以会话为中心的视图,其中命中映射到数组。对吗?
    猜你喜欢
    • 2018-06-15
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多