How to calculate Session and Session duration in Firebase Analytics raw data?

前端 未结 3 1344
抹茶落季
抹茶落季 2020-11-29 08:48

How to calculate Session Duration in Firebase analytics raw data which is linked to BigQuery?

I have used the following blog to calculate the users

3条回答
  •  伪装坚强ぢ
    2020-11-29 09:21

    First you need to define a session - in the following query I'm going to break a session whenever a user is inactive for more than 20 minutes.

    Now, to find all sessions with SQL you can use a trick described at https://blog.modeanalytics.com/finding-user-sessions-sql/.

    The following query finds all sessions and their lengths:

    #standardSQL
    
    SELECT app_instance_id, sess_id, MIN(min_time) sess_start, MAX(max_time) sess_end, COUNT(*) records, MAX(sess_id) OVER(PARTITION BY app_instance_id) total_sessions,
       (ROUND((MAX(max_time)-MIN(min_time))/(1000*1000),1)) sess_length_seconds
    FROM (
      SELECT *, SUM(session_start) OVER(PARTITION BY app_instance_id ORDER BY min_time) sess_id
      FROM (
        SELECT *, IF(
                    previous IS null 
                    OR (min_time-previous)>(20*60*1000*1000),  # sessions broken by this inactivity 
                    1, 0) session_start 
                    #https://blog.modeanalytics.com/finding-user-sessions-sql/
        FROM (
          SELECT *, LAG(max_time, 1) OVER(PARTITION BY app_instance_id ORDER BY max_time) previous
          FROM (
            SELECT user_dim.app_info.app_instance_id
              , (SELECT MIN(timestamp_micros) FROM UNNEST(event_dim)) min_time
              , (SELECT MAX(timestamp_micros) FROM UNNEST(event_dim)) max_time
            FROM `firebase-analytics-sample-data.ios_dataset.app_events_20160601`
          )
        )
      )
    )
    GROUP BY 1, 2
    ORDER BY 1, 2
    

提交回复
热议问题