如何在BigQuery中使用Google Analytics(分析)来操纵“平均会话时长”到“ DAU平均会话时长”的查询?

时间:2019-09-06 15:48:43

标签: google-bigquery

我目前有一个查询,返回“跨平台的平均会话时长”。我现在需要它返回“ DAU跨平台的平均会话持续时间”。以下三个表格之间的区别是iOS,Android和网络数据。 user_id是我可以在GA中使用的自定义维度,但是仅添加此维度并不能获得我想要的结果。我想我需要添加user_id自定义维度并对日期做些事情,以便按日期获取唯一身份用户。但是,我是使用BigQuery的新手,无法自行完成此操作。我意识到这个问题含糊不清-很高兴相应地更新我的问题。

WITH
  base AS (
  SELECT
    *
  FROM (
    SELECT
      _TABLE_SUFFIX AS table_date,
      "web" AS app_source,
      *
    FROM
      `XXXX.ga_sessions_*`
    UNION ALL
    SELECT
      _TABLE_SUFFIX AS table_date,
      "iOS" AS app_source,
      *
    FROM
      `XXXX.ga_sessions_*`
    UNION ALL
    SELECT
      _TABLE_SUFFIX AS table_date,
      "Android" AS app_source,
      *
    FROM
      `XXXX.ga_sessions_*` )
  WHERE
    NOT REGEXP_CONTAINS(table_date, "intraday|backup") )
SELECT
  FORMAT_DATE("%E4Y-%m-01", PARSE_DATE("%E4Y%m%d", MIN(table_date))) AS
ymd,
  AVG(COALESCE(totals.timeOnSite, totals.timeOnScreen, 0)) as timeOnSite
 FROM
  base
WHERE
  table_date > "20180101"
  GROUP BY 1
ORDER BY 1

1 个答案:

答案 0 :(得分:1)

我认为您那时需要在两个级别上进行汇总。这有帮助吗?

WITH base AS (
  SELECT
    *
  FROM (
    SELECT
      _TABLE_SUFFIX AS table_date,
      'web' AS app_source,
      *
    FROM
      `XXXX.ga_sessions_*`
    UNION ALL
    SELECT
      _TABLE_SUFFIX AS table_date,
      'iOS' AS app_source,
      *
    FROM
      `XXXX.ga_sessions_*`
    UNION ALL
    SELECT
      _TABLE_SUFFIX AS table_date,
      'Android' AS app_source,
      *
    FROM
      `XXXX.ga_sessions_*` 
  )
  WHERE NOT REGEXP_CONTAINS(table_date, 'intraday|backup') 
)

select 
   app_source,
   parse_date('%Y%m%d', table_date) as date,
   count(distinct user_id) as total_users,
   AVG(coalesce(totals.timeOnSite, totals.timeOnScreen, 0)) as average_time_on_site
from base
group by 1,2
order by 1,2