Hello there, a simple question. What is the best w...
# support
g
Hello there, a simple question. What is the best way to calculate sessions? I see context_session_id in bigquery but not sure if it represents a number of sessions - since values are the same?? Should I maybe count only distinct values and take that as sessions? Thanks!!!
m
Hey There! đź‘‹ Your message has been received by the RudderStack team. Our standard customer support hours are 9-6 PM EST, but we will forward this request to your Technical Account Manager, and they will get back to you shortly. Please use the thread for any additional comments.
f
I case you want to be fully in control you can do post processing on your tables with DBT for example. Assigning session_id with SQL is not that hard. Note in this example I am only using a single event table.
Copy code
WITH ordered_events AS (
    SELECT 
        user_id,
        event_time,
        event_name,
        LAG(event_time) OVER (PARTITION BY user_id ORDER BY event_time) as previous_event_time
    FROM 
        analytics.events
),
sessionized_events AS (
    SELECT
        user_id,
        event_time,
        event_name,
        CASE 
            WHEN previous_event_time IS NULL OR 
                 DATETIME_DIFF(event_time, previous_event_time, MINUTE) > 30 -- 30 minutes grace period
            THEN 1
            ELSE 0 
        END as is_new_session
    FROM 
        ordered_events
)

SELECT
    user_id,
    event_time,
    event_name,
    SUM(is_new_session) OVER (PARTITION BY user_id ORDER BY event_time) as session_id
FROM 
    sessionized_events
In this example above every user session will be assigned a number that is increasing everytime when the user doesn’t produce an event for more than 30 minutes.
g
hmm this seems a but complicated than i imagined. I was thinking of just counting the unique values of context_session_id.... What do you think about this?
f
Can work as well. This is so you can to have full control over the session ids. Rudderstack provided session ids are blackbox i assume.
m
Hi Alik, Rudderstack sessions work in a similar fashion. So when the user is inactive for 30 minutes or longer a new session is created. So you can count just the number of unique sessions per user. Also not the sessionId is just the timestamp. Also sessionId works well for the JS and mobile sources, but some users mentioned that it was not being correctly reported for the Shopify source. You can use the above as a test to compare the accuracy of the number of sessions in your data.
g
And is there a trait / property called session? And if so, how should I search for this? I only see context_session_id in the bigquery... So counting unique values would suffice I suppose?
m
Yes the context_session_id is the correct property and you should be able to count the number of unique sessions using this parameter.