> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kameleoon.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Google Analytics 4 — Event Streaming

> Automatically stream Kameleoon experiment exposure events to Google Analytics 4 from the server side for enriched behavior analysis by experiment variation.

The Kameleoon Google Analytics 4 Event Streaming integration lets you automatically send events to your Google Analytics 4 account whenever visitors are exposed to a Kameleoon experiment. This feature allows for enriched analytics in Google Analytics 4, providing insights on experiment exposure to help you better understand user behavior and engagement.

<Frame>
  ![](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/integrations/as-a-destination/analytics/google-analytics-4/google-analytics-4-event-streaming/Capture-décran-2025-04-29-à-16.22.59-1536x256.png)
</Frame>

This integration uses Kameleoon's event dispatcher to send server side events to your Google Analytics 4 account in batches using the **`/mp/collect`** endpoint of the [Google Analytics 4 HTTP API](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag#transport).

## Key benefits

* **Precise behavior insights:** The integration sends specific events to third-party tools for visitors exposed to web experiments and feature flags on SDK-based project. This detailed tracking provides a clear view of how different variations impact user actions. For example, you can observe which variation drives more clicks, conversions, or other desired outcomes, helping you determine the most effective approach.
* **Real-time decision-making:** The integration provides data in real time. With these up-to-date insights, you can swiftly adjust strategies if a variation isn't performing as expected or allocate resources to capitalize on successful variations.
* **Effortless setup:** Enable third-party integrations in the Kameloon app with no need for custom code or SDK changes.

## Considerations

**Event delivery timing:** Kameleoon's server dispatches events to Google Analytics 4 with a minimum 30-minute delay.

## Prerequisites

To configure this integration, you need the following information:

* **Kameleoon SDK:** An SDK must be installed on your server-side project.
* **API secret:** The API secret generated in Google Analytics, documented [here](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag#payload).
* **Measurement ID:** For web SDKs, the identifier for a Data Stream, as documented [here](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag#payload).
* **Firebase app ID:** For mobile SDKs, the identifier for a Firebase app, as documented [here](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=firebase#payload).

### Choosing the right GA4 identifier

To ensure proper user attribution in GA4 reports, you must create the correct custom data depending on the app type:

* **For web-based projects (websites using `gtag.js`):** Create a custom data called `ga4_client_id` and retrieve the `client_id` from GA4 (typically stored in the `_ga` cookie).
* **For Firebase-based mobile apps:** Create a custom data called `ga4_app_instance_id` and retrieve the `app_instance_id` from the Firebase SDK.

This distinction applies to all types of GA4 integrations in Kameleoon, whether for web experiments or feature flag experiments.

## Setup

This integration can be used for web and feature experimentation.

### For Web experiments

Enable the integration on the **Integration page**.

#### Identify GA4 client ID

You must send an instance identifier to the Kameleoon backend on each visit. The exact identifier depends on your GA4 setup. If you're using GA4 with the `gtag.js` method, send the `client_id`. Below is an example of a `gtat.js` implementation using our Activation API.

#### Set up the Custom data

* **Name:** `ga4_client_id`
* **Select** your project.
* **Retrieval method:** Kameleoon Activation API.
* **Type:** Single
* **Format:** String
* **Scope:** Visit

<Frame>
  ![](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/integrations/as-a-destination/analytics/google-analytics-4/google-analytics-4-event-streaming/Capture-décran-2025-05-06-à-15.00.46.png)
</Frame>

#### Add code in the Global script

```javascript theme={null}
if(!Kameleoon.API.CurrentVisit.customData["ga4_client_id"]){

// First, create a ga4ClientId variable and obtain your GA4 client ID.
let gaClientId = null;

const getClientId = () =>{
    gtag('get', 'G-XXXXXXXXXX', 'client_id', function(clientId) {
        gaClientId = clientId;
    });
    return gaClientId !== null
}

Kameleoon.API.Core.runWhenConditionTrue(
    () => getClientId(),
    () =>{
        // Then set your Kameleoon "ga_client_id" custom data to this value using Kameleoon's Activation API
        Kameleoon.API.Data.setCustomData("ga4_client_id", gaClientId);
    },
    200
)

}
```

#### Activate the integration for your campaigns

Add the integration when finalizing your web experiment.

<Frame>
  ![](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/integrations/as-a-destination/analytics/google-analytics-4/google-analytics-4-event-streaming/Capture-décran-2025-04-29-à-16.34.40-1536x407.png)
</Frame>

### For Feature experiments

Set up the integration in the feature flag environment under the **Integrations** section. Enable it for each environment where you want it to be active.

<Frame>
  ![](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/integrations/as-a-destination/analytics/google-analytics-4/google-analytics-4-event-streaming/Capture-décran-2025-06-02-à-10.37.49-1.png)
</Frame>

### For Mobile

If you're using GA4 analytics with a Firebase app, you must send the **Firebase app instance ID**.

Here's an example with the Kameleoon Android SDK:

1. Identify GA4 Firebase app instance ID
2. Set up a Custom data
   * **Name:** `ga_app_instance_id`
   * **Select** your project.
   * **Retrieval method**: Kameleoon SDK method.
   * **Type:** Single
   * **Format:** String
   * **Scope:** Visit

<Frame>
  ![](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/integrations/as-a-destination/analytics/google-analytics-4/google-analytics-4-event-streaming/Capture-décran-2025-05-06-à-15.09.15.png)
</Frame>

#### Java example using the Kameleoon Android SDK

First, get your Firebase app instance ID and store it in a variable.

Next, use the Kameleoon SDK to set your `ga4_app_instance_id` custom data to this value, ensuring you use the correct index for the `ga4_app_instance_id` custom data, which you can find in the Kameleoon interface.

In this example, the index is `12`:

`kameleoonClient.addData(new CustomData(12, appInstanceId));`

Once the integration is active and your experiment is live, Kameleoon will begin sending experiment events to Google Analytics 4 whenever a visitor is exposed. These events are built according to Google's recommended [best practices](https://developers.google.com/analytics/devguides/collection/ga4/integration). These events include key details such as:

* A unique identifier for the Firebase app instance, for SDK setup only, called `app_instance_id`.
* A unique identifier for the client, for Web setup only, called `client_id`.
* Exposure time
* A unique identifier for the experiment and variation that follow the format `KAM-XXXX-YYYY`, where `XXXX` is the Kameleoon experiment ID and `YYYY` is the variation ID.

#### Event payload example

Kameleoon sends events to Google Analytics 4 in the following format:

```json theme={null}
{
  "events": [
    {
      "name": "experience_impression",
      "exp_variant_string": "KAM-1234-5567",
      "timestamp_micros": 1718201234000
    },
    {
      "name": "experience_impression",
      "exp_variant_string": "KAM-5678-6978",
      "timestamp_micros": 1718201267000
    }
  ],
  "app_instance_id": "example-instance-id"
}
```

##### Identify users

Kameleoon does **not** include user identifiers (such as `user_id` or `client_id`) in the event payload by default. Identification is handled through the following fields, depending on the environment:

* **Mobile:** `app_instance_id` (standard for GA4 mobile apps)
* **Web:** `client_id` (the equivalent of `app_instance_id` for web clients)

If you are using Kameleoon in a web backend or server-side setup, you can transmit the GA4 `client_id` by defining it as a custom data field (for example, `ga4_client_id`).

You can set this field using any available retrieval method—not only the Activation API. It just needs to be defined at least once per Kameleoon session.

##### Enrich events

Kameleoon does not include your business-specific custom data in the server-to-server event payload.

If you need to enrich the payload with additional data (such as `client_id` or user attributes), and you have a Kameleoon account, you can add it via custom data fields in your SDK configuration or submit a [feature request](https://feedback.kameleoon.com/)
