Skip to content

Update GA4 Docs Session ID and Session Number #2925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ The Google Analytics 4 reports only display active users who engage with your si

If you choose to integrate with Google Analytics 4 client-side (using Gtag outside of Segment) _and_ also use Segment's Google Analytics 4 destination to send events through the API, you can track sessions server-side. When using Gtag, [Google generates a `session_id` and `session_number` when a session begins](https://support.google.com/analytics/answer/9191807?hl=en){:target='_blank'}. The `session_id` and `session_number` generated on the client can be passed as Event Parameters to stitch events sent through the API with the same session that was collected client-side.

You can double-check your session_id and session_number with the [Google Site Tag function](https://developers.google.com/tag-platform/gtagjs/reference){:target='_blank'} or by running this script in your JavaScript console and replacing `G-xxxxxxxxxx` with your GA4 measurement ID:

```java
const sessionIdPromise = new Promise(resolve => {
gtag('get', 'G-xxxxxxxxxx', 'session_id', resolve)
});
const sessionNumPromise = new Promise(resolve => {
gtag('get', 'G-xxxxxxxxxx', 'session_number', resolve)
});

Promise.all([sessionIdPromise, sessionNumPromise]).then(function(session_data) {
console.log("session ID: "+session_data[0]);
console.log("session Number: "+session_data[1]);
});
```

> info "Session tracking limitations"
> Session tracking server-side only works if you're also sending data to Google Analytics 4 client-side. This is because the `session_id` must match a value that was previously collected on the client. For events to stitch properly, they must arrive within a 48 hour window of when the client-side events arrived.
>
Expand Down