在BigQuery中计算当前用户的返回率

时间:2019-03-14 13:44:34

标签: google-bigquery firebase-analytics

我正在尝试使用从Firebase导入到BigQuery中的数据来计算CURR(当前用户返回率,请参阅https://lloydmelnick.com/2019/02/05/lifetime-value-part-26-my-most-valuable-retention-kpis/)。

我尝试创建三列来确定用户在2周前,1周前和本周是否处于活动状态,但是似乎没有任何作用。我想查看本周处于活动状态并且还在2周和3周前处于活动状态的用户。

这是我尝试过的查询:

SELECT
  COUNT(DISTINCT user_pseudo_id)
FROM(SELECT
  user_pseudo_id,
  IF( days_from_today >13 AND days_from_today <21, 1, 0) AS prev_week,
  IF( days_from_today >6 AND days_from_today <14, 1, 0) AS last_week,
  IF( days_from_today <7, 1, 0) AS this_week
FROM(
SELECT
    DATE_DIFF(CURRENT_DATE(), DATE(TIMESTAMP_MICROS(event_timestamp)), day) AS days_from_today,
    user_pseudo_id
  FROM
    `dataset.events_2019*`
  WHERE
    event_name = 'user_engagement'
  GROUP BY
    days_from_today,
    user_pseudo_id))
    WHERE prev_week=1
    GROUP BY prev_week, last_week, this_week

1 个答案:

答案 0 :(得分:0)

在查询中进行一些修改,这对我有用:

@Query("select e from Entity e where e.someField like :param")
public Entity getEntityBySomeField(@Param("param") String param);

播放一些虚拟数据:

SELECT
  user_pseudo_id
FROM (
  SELECT
    user_pseudo_id,
    MAX(IF( days_from_today >13 AND days_from_today < 21, 1, 0)) AS prev_week,
    MAX(IF( days_from_today >6 AND days_from_today <14, 1, 0)) AS last_week,
    MAX(IF( days_from_today <7, 1, 0)) AS this_week
  FROM (
    SELECT
      DATE_DIFF(CURRENT_DATE(), DATE(TIMESTAMP_MICROS(event_timestamp)), day) AS days_from_today,
      user_pseudo_id
    FROM
      test_table
    WHERE
      event_name = 'user_engagement'
    GROUP BY
      days_from_today,
      user_pseudo_id)
  GROUP BY
    user_pseudo_id)
WHERE
  prev_week = 1

哪个提供此数据集:

WITH test_table as (
  select 1 as user_pseudo_id, 'user_engagement' as event_name, 1552208299000000 as event_timestamp union all
  select 2 as user_pseudo_id, 'user_engagement' as event_name, 1552079299000000 as event_timestamp union all
  select 3 as user_pseudo_id, 'user_engagement' as event_name, 1552186299000000 as event_timestamp union all
  select 1 as user_pseudo_id, 'user_engagement' as event_name, 1551024899000000 as event_timestamp union all
  select 2 as user_pseudo_id, 'user_engagement' as event_name, 1551024899000000 as event_timestamp union all
  select 1 as user_pseudo_id, 'user_engagement' as event_name, 1551523899000000 as event_timestamp union all
  select 1 as user_pseudo_id, 'user_engagement' as event_name, 1552024899000000 as event_timestamp
)
SELECT
  DATE_DIFF(CURRENT_DATE(), DATE(TIMESTAMP_MICROS(event_timestamp)), day) AS days_from_today,
  user_pseudo_id
FROM
  test_table
WHERE
  event_name = 'user_engagement'
GROUP BY
  days_from_today,
  user_pseudo_id
ORDER BY 2, 1

这里上周加入的用户是 days_from_today user_pseudo_id 1 4 1 2 6 1 3 12 1 4 18 1 5 6 2 6 18 2 7 4 3 1

使用虚拟数据集运行查询:

2

将用户WITH test_table as ( select 1 as user_pseudo_id, 'user_engagement' as event_name, 1552208299000000 as event_timestamp union all select 2 as user_pseudo_id, 'user_engagement' as event_name, 1552079299000000 as event_timestamp union all select 3 as user_pseudo_id, 'user_engagement' as event_name, 1552186299000000 as event_timestamp union all select 1 as user_pseudo_id, 'user_engagement' as event_name, 1551024899000000 as event_timestamp union all select 2 as user_pseudo_id, 'user_engagement' as event_name, 1551024899000000 as event_timestamp union all select 1 as user_pseudo_id, 'user_engagement' as event_name, 1551523899000000 as event_timestamp union all select 1 as user_pseudo_id, 'user_engagement' as event_name, 1552024899000000 as event_timestamp ) SELECT user_pseudo_id FROM ( SELECT user_pseudo_id, MAX(IF( days_from_today >13 AND days_from_today < 21, 1, 0)) AS prev_week, MAX(IF( days_from_today >6 AND days_from_today <14, 1, 0)) AS last_week, MAX(IF( days_from_today <7, 1, 0)) AS this_week FROM ( SELECT DATE_DIFF(CURRENT_DATE(), DATE(TIMESTAMP_MICROS(event_timestamp)), day) AS days_from_today, user_pseudo_id FROM test_table WHERE event_name = 'user_engagement' GROUP BY days_from_today, user_pseudo_id) GROUP BY user_pseudo_id) WHERE prev_week = 1 1作为结果。这应该是您想要的结果。您可以使用此查询来构建所需的不同分析结果。