Sub topic 8
Sessions are derived from events
This chapter assumes you know what a GA4 event and event parameter are, and that you can read basic gtag JavaScript and SQL. By the end you will know how GA4 creates sessions from events and why a session is never something you send yourself. You will also know how to find sessions in the BigQuery export and how to avoid the common mistakes.
A session is a label, not a record
In Universal Analytics, a session was a container. Hits arrived inside it, and it had its own start and end. GA4 has no such container. The only thing stored is events. A session exists because events share the same session identifier.
Two pieces of information tie an event to a session:
ga_session_id: an event parameter holding the Unix timestamp, in seconds, of the moment the session started.ga_session_number: an event parameter counting how many sessions this browser or app instance has had, starting at 1.
Because the ID is a timestamp, it is only unique for one user. Two different users can have the same ga_session_id. To identify a session globally, combine it with the user identifier. This is why the BigQuery query later in this chapter concatenates user_pseudo_id with ga_session_id.
How a session starts
GA4 starts a session when it sees the first event from a user that is not part of a current session. On the web, the Google tag does this automatically and records two events:
session_start: marks the start of the session.first_visit: also sent on a user's very first visit, when the first-party cookie is created.
The tag writes the session ID and session number into a cookie named like _ga_<container-id>. Every later event reads that cookie and attaches the same ga_session_id. The session_start event is a marker. Reports count sessions from the session ID values, not by counting session_start events one by one, although the two numbers are normally close.
How a session ends
GA4 does not send an "end session" event. A session ends by inactivity:
- Each event the user sends refreshes a timer.
- If no event arrives for the session timeout, the session is over.
- The next event starts a new session with a new
ga_session_id.
The default timeout is 30 minutes. You can change it per web data stream in the admin settings, from 5 minutes up to 7 hours 55 minutes. You can also set the session engagement timer for how long a session may stay open. Longer timeouts merge visits that would otherwise be separate. Shorter timeouts split them.
What starts a new session besides inactivity
Inactivity is the main rule, but some other conditions can also produce a new session:
- Midnight does not split a session. Unlike Universal Analytics, GA4 does not restart sessions at midnight.
- Campaign changes do not split a session. In Universal Analytics, a new campaign source started a new session. In GA4, a visit that arrives with new UTM parameters during an active session stays in the current session. The session keeps the traffic source it started with.
- Cookie loss starts a new session. If the user clears cookies, the tag no longer finds the session ID. It creates a new client ID and a new session.
- A different device or browser is a different session. Sessions belong to a client, not to a person, unless User-ID joins them at the user level.
Session parameters and scope
A session has no parameters of its own. Instead, a few attributes are copied onto events, and reports compute session-level values from them. The most important are:
| Item | Where it lives | Notes |
|---|---|---|
| `ga_session_id` | Event parameter on every event | Timestamp of session start, in seconds |
| `ga_session_number` | Event parameter on every event | 1 for a user's first session |
| `session_engaged` | Event parameter | Set to 1 once the session becomes engaged (see the next chapter) |
| `engagement_time_msec` | Event parameter | Milliseconds of foreground time since the last event |
| Session source and medium | Derived from the first event that has traffic source data | Reported as session-scoped dimensions |
The scope matters for reporting. A dimension such as "Session source / medium" is evaluated once per session. A dimension such as "Page title" is evaluated per event. Mixing scopes in one report can give counts that look wrong, because GA4 only lets you combine dimensions and metrics that are compatible.
Seeing sessions in your own events
Any event you send through the Google tag is automatically assigned to the current session. You do not set the session ID yourself:
// The tag adds ga_session_id and ga_session_number for you.
gtag('event', 'tutorial_step_completed', {
step_number: 3,
tutorial_name: 'ga4_data_model'
});This event keeps the session alive, because any event counts as activity. Be careful with this. An event sent by a background timer, such as a heartbeat every 60 seconds, would keep a session open forever, even if the user left the tab in the background. If an event is not a user interaction, you can set the parameter non_interaction in some tools. Note that GA4 itself does not use it to decide session length, so the safer fix is to avoid sending such events.
Server-side and Measurement Protocol events
The Measurement Protocol sends events directly to GA4 from a server. It does not run the Google tag, so nothing attaches the session ID for you. To keep server events in the same session as the browser events, send the same client_id and pass the session_id parameter with the value from the browser's ga_session_id. If you leave it out, GA4 may place the event in a session of its own, which inflates the session count and drops the source information.
// Node.js 18 or later: send a server event into an existing session.
const measurementId = 'G-XXXXXXXXXX';
const apiSecret = 'YOUR_API_SECRET';
fetch(
`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`,
{
method: 'POST',
body: JSON.stringify({
client_id: '1234567890.1700000000',
events: [{
name: 'tutorial_step_completed',
params: {
session_id: '1700000000',
engagement_time_msec: 100,
step_number: 3
}
}]
})
}
);The engagement_time_msec value matters here too. Without a value greater than zero, Measurement Protocol events may not show up in some real-time and engagement reports.
Finding sessions in BigQuery
In the BigQuery export, every row is an event. There is no session table. You rebuild sessions by reading ga_session_id out of the event_params array:
-- Count events, users, and sessions per day from the GA4 BigQuery export
SELECT
event_date,
COUNT(*) AS event_count,
COUNT(DISTINCT user_pseudo_id) AS users,
COUNT(DISTINCT CONCAT(
user_pseudo_id,
'.',
CAST((SELECT ep.value.int_value
FROM UNNEST(event_params) AS ep
WHERE ep.key = 'ga_session_id') AS STRING)
)) AS sessions
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20240101' AND '20240131'
GROUP BY event_date
ORDER BY event_date;Read the query in three parts:
- The subquery with
UNNEST(event_params)pulls theint_valueof the parameter whose key isga_session_id. CONCATjoins it touser_pseudo_id, because the session ID alone is not unique across users.COUNT(DISTINCT ...)counts each combination once.
Two edge cases affect the numbers:
- A session that crosses midnight appears under two
event_datevalues, so the per-day sessions add up to more than the true total for a longer period. Count distinct sessions over the whole range if you need a period total. - Events with no
ga_session_id(some Measurement Protocol events, for example) produce aNULL. After concatenation,CONCATreturnsNULLfor those rows, andCOUNT(DISTINCT ...)ignores them. Those events are counted inevent_countbut not insessions.
Your count may also differ slightly from the GA4 interface. The interface applies data thresholds, consent-mode modeling, and sampling or approximation (HyperLogLog++) in some reports. The export holds the raw events.
Common mistakes
- Treating
session_startas a session count. Count distinct session IDs combined with the user instead. - Using
ga_session_idalone as a key. It collides across users. - Expecting campaign changes to split sessions. They do not.
- Sending background events. They extend sessions and distort duration.
- Forgetting
session_idon server events. The events land in orphan sessions. - Comparing GA4 session counts with Universal Analytics. The rules for starting a session differ, so the numbers will not match.
Exercise
A user opens your site at 10:00, browses until 10:20, and leaves the tab open. They return at 11:00 and click a link. The session timeout is the default. How many sessions does GA4 record, and what do the two sessions share and differ in?
Show answer
Two sessions. The gap between 10:20 and 11:00 is 40 minutes, which exceeds the 30-minute timeout, so the click at 11:00 starts a new session with a new ga_session_id and a session_start event, and ga_session_number goes from 1 to 2. Both sessions share the same client ID (user_pseudo_id in BigQuery) as long as the cookie was not cleared, so they count as one user.
Exercise
In the BigQuery query, why is user_pseudo_id concatenated with ga_session_id before counting distinct values?
Show answer
ga_session_id is the Unix timestamp of the session start, so two different users who start a session in the same second share the same value. Combining it with user_pseudo_id makes the key unique per session.
Exercise
Your server sends purchase events through the Measurement Protocol, and your session count rises while engagement drops. What is the likely cause and the fix?
Show answer
The server events carry no session_id, so GA4 places them in separate sessions. Fix it by sending the browser's ga_session_id as the session_id parameter, along with the same client_id, and by including a positive engagement_time_msec.