Skip to content

Commit e7f13e6

Browse files
authored
Merge pull request #3943 from segmentio/050phy-ga4-sessions-serverside-only-vs-with-clientside
Clarified on GA4 sessions tracking implementation
2 parents 525e2f6 + 8c6ec81 commit e7f13e6

File tree

1 file changed

+19
-12
lines changed
  • src/connections/destinations/catalog/actions-google-analytics-4

1 file changed

+19
-12
lines changed

src/connections/destinations/catalog/actions-google-analytics-4/index.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,38 @@ To send parameters to Google Analytics 4, use the Event Parameters or User Prope
7777
To achieve parity with Universal Analytics, you should create the same custom dimensions that you defined in Universal Analytics. Additionally, in Google Analytics 4, you should recreate many of the values that you tracked as event dimensions in Universal Analytics, particularly event category and event label. For more information, see [Google Analytics 4 Custom dimensions and metrics](https://support.google.com/analytics/answer/10075209){:target='_blank'}.
7878

7979
#### Tracking Active Users and Sessions
80+
81+
##### Server-side Implementation using Google Analytics 4 Destination
82+
8083
The Google Analytics 4 reports only display active users who engage with your site for a non-zero amount of time. To ensure users are rendered in reports, Segment sets the `engagement_time_msec` parameter to 1 by default. If you track engagement time on your Segment events, you can use the **Engagement Time in Milliseconds** field mapping to set `engagement_time_msec` to a different value.
8184

82-
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.
85+
Besides Engagement Time in Milliseconds, you can also generate your own `session_id` and `session_number` and pass them as event properties to Segment. These properties can then be mapped to their corresponding parameters in Segment's Google Analytics 4 destination.
86+
87+
> info "Session tracking limitations"
88+
> Session tracking server-side only supports a subset of user dimensions. Certain reserved fields such as location, demographics, other [predefined user dimensions](https://support.google.com/analytics/answer/9268042?hl=en&ref_topic=11151952){:target='_blank'} and device-specific information are not supported by Google's Measurement Protocol API.
89+
90+
##### Using Gtag.js and Google Analytics 4 Destination
91+
92+
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 will have all the reserved parameters and sessions tracking information available in Google Analytics 4 reports.
93+
94+
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. For events to stitch properly, server-side events must arrive within a 48 hour window of when the client-side events arrive.
8395

8496
You can 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 Google Analytics 4 Measurement ID:
8597

8698
```java
87-
const sessionIdPromise = new Promise(resolve => {
88-
gtag('get', 'G-xxxxxxxxxx', 'session_id', resolve)
99+
const sessionIdPromise = new Promise(resolve => {
100+
gtag('get', 'G-xxxxxxxxxx', 'session_id', resolve)
89101
});
90-
const sessionNumPromise = new Promise(resolve => {
91-
gtag('get', 'G-xxxxxxxxxx', 'session_number', resolve)
102+
const sessionNumPromise = new Promise(resolve => {
103+
gtag('get', 'G-xxxxxxxxxx', 'session_number', resolve)
92104
});
93105

94106
Promise.all([sessionIdPromise, sessionNumPromise]).then(function(session_data) {
95-
console.log("session ID: "+session_data[0]);
96-
console.log("session Number: "+session_data[1]);
107+
console.log("session ID: "+session_data[0]);
108+
console.log("session Number: "+session_data[1]);
97109
});
98110
```
99111

100-
> info "Session tracking limitations"
101-
> 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.
102-
>
103-
> Google doesn't currently support passing other reserved fields, such as [predefined user dimensions](https://support.google.com/analytics/answer/9268042?hl=en&ref_topic=11151952){:target='_blank'} or device-specific information, to the Measurement Protocol API.
104-
105112
#### User Identification
106113
Segment requires a Client ID to send data to Google Analytics 4. The Client ID is the web equivalent of a device identifier and uniquely identifies a given user instance of a web client. By default, Segment sets Client ID to the Segment `userId`, falling back on `anonymousId`. This mapping can be changed within each action if you prefer to map a different field to Client ID.
107114

0 commit comments

Comments
 (0)