Set up Google Analytics 4

Set up Google Analytics 4 as a destination in RudderStack.

Follow the below steps to set up Google Analytics 4 as a destination in RudderStack:

  1. From the list of destinations in RudderStack dashboard, select Google Analytics 4.
  2. Select an existing source from the displayed ones or set up a new source to connect to Google Analytics 4 and click Continue.

Connection settings

To successfully configure Google Analytics 4 as a destination, configure the following settings:

  • Name: Assign a name to your destination.

  • API Secret: Enter the API Secret generated through the Google Analytics dashboard. It can be created in the Google Analytics dashboard under Admin > Data Streams > Choose your stream > Measurement Protocol > Create.

  • Client Type: Select the client type as gtag or Firebase from the dropdown:

    • gtag - Supported for both cloud and device mode. If you select gtag, enter the following:
      • Measurement ID: Enter the Measurement ID which is the identifier for a data stream. It can be found in the Google Analytics dashboard under Admin > Data Streams > Choose your stream > Measurement ID.
    • Firebase - Supported only for cloud mode. If you select Firebase, enter the following:
      • Firebase App Id: Enter the Firebase App ID which is the identifier for Firebase app. It can be found in the Firebase dashboard under Project Settings > General.

Refer to the FAQ section for more information on how to obtain the API secret, Measurement ID, and Firebase App ID.

Connection mode

Specify how you want to route events from your sources to Google Analytics 4.

RudderStack supports sending event data to Google Analytics 4 via the following connection modes:

Connection ModeWebMobileServer
Device ModeSupported--
Cloud ModeSupportedSupportedSupported
Hybrid ModeSupported--
info
In web device mode integration, that is, using JavaScript SDK as a source, RudderStack loads the Google Analytics 4 native SDK from https://www.googletagmanager.com/ domain. Based on your website’s content security policy, you might need to allowlist this domain to load the Google Analytics 4 SDK successfully.

Connection mode comparison: hybrid vs. cloud vs. device

The following table lays out a detailed comparison to help you choose a connection mode:

info

Note that:

  • RudderStack makes the primary decision on how to track and stitch events together based on the client_id and session_id parameters.
  • Server-side session tracking supports only a subset of user dimensions. Google’s Measurement Protocol API does not support the reserved fields like location, demographics, predefined user dimensions, and device-specific information.
Device modeCloud modeHybrid mode with client ID override (RECOMMENDED)Hybrid mode without client ID override (Requires SDK configuration)
DescriptionRudderStack’s SDK loads the native client libraries.Sends data server-side to Google’s Measurement Protocol API.Automatically configures a single connection that captures attribution data from page calls (sent via device mode) and all other calls via cloud mode.Automatically configures a single connection that captures attribution data from page calls (sent via device mode) and all other calls via cloud mode.
Client IDFetches the client_id automatically generated by gtag.You can send a custom client_id via externalID. Otherwise, RudderStack sends an anonymousId or rudderId. See Mapping client_id.Overrides the client_id with RudderStack’s anonymousID. (RudderStack utilizes a cookie that adds an rs_ prefix to the existing Measurement ID)Fetches the client_id automatically generated by gtag.
Session IDFetches the session_id automatically generated by gtag.Passes session_id automatically but you can customize it using these mappings.Overrides session_id with the RudderStack’s session_id.Fetches the session_id automatically generated by gtag.
ConsiderationsOffers full information capture but it’s worth noting potential impacts the performance, page speed, data resilience, and first-party data capture.Google’s Measurement Protocol doesn’t support passing certain reserved fields, including attribution data.Introducing a custom client_id may cause fragmentation in multi-domain implementations. You may observe a potential momentary spike in session count while switching from device mode initially.Buffer the SDK with a slight delay to avoid a potential race condition whereby the SDK events reach GA4 prior to the events sent via server-side.
Required actionsNATo capture full attribution data, you must pass it as custom dimensions or instrument a parallel client-side integration.Ensure that the override setting is enabled in the RudderStack dashboard. To ensure accurate cross-platform analytics, check that other cookies (such as Google Ads) also include the appropriate rs_ prefix.Instrument your SDK to buffer.

For example:
rudderanalytics.load(
"write_key",
"dataplane_url",
{
bufferDataPlaneEventsUntilReady: true
}
);

Configuration settings

In the Configuration tab of your destination, you can configure the below-mentioned cloud and device mode settings:

Cloud mode

  • Debug via Validation Server: Enable this setting to check the validation responses in the Live Events tab. Debug mode is supported via GA4’s validation server endpoint.
warning
As per the GA4 documentation, any responses sent to the validation server will not show up in the reports.
  • Filter user traits: Enter the list of user traits which you do not want RudderStack to send to GA4. You can use this field to filter sensitive PII fields like email, phone number, credit card number, etc. from your events and prevent them from being sent to GA4.
info

Note that:

Device mode

  • Filter user traits: Enter the list of user traits which you do not want RudderStack to send to GA4. You can use this field to filter sensitive PII fields like email, phone number, credit card number, etc. from your events and prevent them from being sent to GA4.
info
To specify multiple traits, press the enter key after each trait.
  • Choose how to capture pageviews: Select RudderStack JS SDK or gtag Automated Capture from the dropdown depending on whether you want to send the page view events through the RudderStack JS SDK, or through automatic collection on each page load using GA4 Enhanced Measurement (gtag) respectively. See GA4 documentation for more information.
  • Debug via DebugView: This setting lets you monitor the device mode events in DebugView. It displays the events and user properties that GA4 collects in realtime. With this feature, you can troubleshoot issues as you install your tags or go through a user’s live activity.
  • Include URL and Search as Page View properties: Enable this setting to send the following properties along with any other custom property to the page call of the RudderStack SDK:
    • url
    • search
info
Google Analytics 4 has a limit on the number of unique properties per event name. The default page_view event supports the properties mentioned in the Automatically collected events in Google Analytics 4 guide.

Hybrid mode

  • Debug via Validation Server: If you enable this setting, then RudderStack sends the cloud mode events (track and group calls) to the GA4 validation server. These events will not show up in your reports.
  • Filter user traits: Enter the list of user traits which you do not want RudderStack to send to GA4. You can use this field to filter sensitive PII fields like email, phone number, credit card number, etc. from your events and prevent them from being sent to GA4.
info
To specify multiple traits, press the enter key after each trait.
  • Choose how to capture pageviews: Select RudderStack JS SDK or gtag Automated Capture from the dropdown depending on whether you want to send the page view events through the RudderStack JS SDK, or through automatic collection on each page load using GA4 Enhanced Measurement (gtag) respectively. See GA4 documentation for more information.
  • Debug via DebugView: If this setting is enabled, then the device mode events (page calls) will be visible under the GA4 DebugView section.
  • Include URL and Search as Page View properties: Enable this setting to send the following properties along with any other custom property to the page call of the RudderStack SDK:
    • url
    • search
  • Override gtag client ID & session ID: Enable this setting to override the gtag client_id and session_id with RudderStack’s anonymousId and session_id to ensure that the attribution is properly unified across the page and track events.
warning

If you are transitioning to the GA4 hybrid mode from device mode, enabling this setting could lead to a momentary spike in session counts. To avoid this scenario, disable this setting and buffer the SDK instead.

For all other scenarios, you can enable this setting.

Other settings

  • Choose if you want to turn on events filtering: This option is applicable only if you’re sending events to Google Analytics 4 via web device mode. See Client-side Event Filtering for more information.
  • Cookie category name: This setting lets you associate the OneTrust cookie consent groups to Google Analytics 4.

FAQ

How do I obtain the Measurement Id?

  1. Log in to the Google Analytics dashboard.
  2. Go to the Admin section in the left sidebar.
  3. Select the relevant account and property.
  4. Click Data Streams and select the stream to get the Measurement Id:

How do I obtain the API Secret?

  1. Login to Google Analytics dashboard.
  2. Go to the Admin section in the left sidebar.
  3. Select the relevant account and property.
  4. Click Data Streams and select the stream.
  5. Click Measurement Protocol API secrets to get the API Secret:

How do I obtain the Firebase App ID?

  1. Login to Google Analytics dashboard.
  2. Go to the Admin section in the left sidebar.
  3. Select the relevant account and property.
  4. Click Data Streams and select the stream to get the Firebase App Id:

How can I prevent RudderStack from sending PII fields to Google Analytics 4?

To prevent RudderStack from sending sensitive PII data in your events to GA4, specify the user traits in Filter user traits while configuring your destination settings.

info
To specify multiple traits, press the enter key after each trait.


Questions? Contact us by email or on Slack