简体   繁体   中英

How can I find continuously events groups using BigQuery?

I'm using Firebase Analytics with BigQuery. Assume I need to give a voucher to users who shares a service everyday in at least 7 continuously days. If someone share in 2 weeks continuously, those will get 2 vouchers and so on.

How can I find out the segments of continuously events logged in Firebase Analytics?

Here is the query that I can find out the individual days that users give a sharing. But I can't recognize the continuous segments.

SELECT event.user_id, event.event_date,
MAX((SELECT p.value FROM UNNEST(user_properties) p WHERE p.key='name').string_value)  as name,
MAX((SELECT p.value FROM UNNEST(user_properties) p WHERE p.key='email').string_value ) as email,
SUM((SELECT event_params.value.int_value from event.event_params where event_params.key = 'share_session_length')) as total_share_session_length

FROM `myProject.analytics_183565123.*` as event
where event_name like 'share_end'  
group by user_id,event_date
having total_share_session_length >= 1
order by user_id desc

And this is the output:

在此处输入图片说明

How can I find out the segments of continuously events logged

Below example for BigQuery Standard SQL - hope you can adopt approach to your specific use case

#standardSQL
SELECT id, ARRAY_AGG(STRUCT(first_day, days) ORDER BY grp) continuous_groups
FROM (
  SELECT id, grp, MIN(day) first_day, MAX(day) last_day, COUNT(1) days
  FROM (
    SELECT id, day,
      COUNTIF(gap != 1) OVER(PARTITION BY id ORDER BY day) grp
    FROM (
      SELECT id, day,
        DATE_DIFF(day,LAG(day) OVER(PARTITION BY id ORDER BY day), DAY) gap
      FROM (
        SELECT DISTINCT fullVisitorId id, PARSE_DATE('%Y%m%d', t.date) day
        FROM `bigquery-public-data.google_analytics_sample.ga_sessions_*` t
      )
    )
  )
  GROUP BY id, grp
  HAVING days >= 7
)
GROUP BY id
ORDER BY ARRAY_LENGTH(continuous_groups) DESC

with result

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM