Use Google Analytics 4 Audiences (GA4) in Kameleoon

portrait de l'auteur Julie Trenque

Written by Julie Trenque

Updated on 06/05/2023

3 min


Manage your integrations







Was this content useful?

This article describes the different steps necessary to create a bridge between Kameleoon and Google Analytics as to identify visitors within GA4 audiences then target them accordingly.

This is a premium option. Please contact your Customer Success Manager to enable it on your Kameleoon Account.

In a nutshell, this is accomplished by pushing our Kameleoon unique identifier (Kameleoon visitorcode) through a custom dimension at the beginning of a visit, then by calling GA4’s APIs to retrieve all possible values of this custom dimension within a specific audience then pushing the audience name (or ID) to Kameleoon by using our Data API.

We also provide native bridges with Google Analytics 4 to send campaigns metadata (campaign name and variation name) so that you can analyze your campaigns performance in GA4.

Set up the authentication through a service level account

To grant a technical user account access to a Google Analytics account and privileges to retrieve data through the different available APIs, the basic requirements are the following :

  • An email account with admin access to Google Analytics account that will be used to generate a project and service account;
  • Access to all required Audiences (in GA4) for this account.

Create a project on Google Cloud Console

The first step is to create a new project in Google Cloud Console which is necessary to activate APIs and create tokens.

Go to the following URL :

Login to the email account with admin access and create a new project.

Name the project according to your naming convention.

Choose the organization and location you have access to.

Note: This is why the email account has to have admin access to the organization.

Activate appropriate APIs

In order to use any API with a particular email address, it must be activated in Google Cloud Console. This is done by clicking on Library (picking up from the previous section).

Search for the 2 next APIs in the search bar and activate them as indicated below :

  • Google Analytics Reporting API;
  • Google Analytics API.

Now both necessary APIs are active for the given user and will be usable directly in a Web service for instance.

Create service level account

In order to allow for full automation of GA4 reporting API requests, we need to create a service level account that will bypass the need for an authentication callback.

To create such an account, click on IAM & Admin > Service accounts.

Then click on Create service account :

Now fill in a name for the account that respects your usual conventions, the service email will be generated automatically.

We recommend that you add a description so that the purpose of this account is clear to all parties.

Next choose the role for this service account. We recommend that you choose a viewer role as our bridge only requires the use of Google APIs and not the project resources directly.

Once the account is created, click on Keys.

Then click on Add key > Create new key to generate a downloadable key.

Choose the JSON format then click on Create to generate the key.

This downloads the key on your computer.

From then on, send this file to your Customer Success Manager as we will require it to set up the bridge in your Kameleoon account. Please use a compression tool to secure this file with a password then send the package separately from its password.

Push Kameleoon Visitorcodes in a GA4 custom dimension

The next step to configure the bridge will be to send each visitor Kameleoon visitorcode to GA4 via a custom dimension.

The basic requirement for this is to create a custom dimension that will be used to receive our Kameleoon Visitor Code for each visitor. Please set the dimension scope to User, name it “Kameleoon Visitor Code” and enter “kameleoon_visitor_code” as the User property.

The following code has to be added in the Kameleoon global custom script section.

 if (Kameleoon.API.CurrentVisit.pageViews === 1) {
     function () {
       return typeof window.gtag !== "undefined";
     function () {
       gtag("event", "Kameleoon Visitor Code", {
         kameleoon_visitor_code: Kameleoon.API.Visitor.code,

Create a custom data to use your GA4 Audiences in Kameleoon

In order to be able to use your GA4 Audiences in Kameleoon campaigns, you will need to set up custom data within Kameleoon.

Name your custom data “GA4 Audience” and choose the acquisition method Custom JavaScript code.

The custom data should be set to the list of and strings types, the scope may be set to visit or page (when being set to page, it is re-evaluated on every page load).

Please use the code below in the custom data acquisition method:

 Kameleoon.API.Data.retrieveDataFromRemoteSource(Kameleoon.API.Visitor.code, function(data) {
     for (const [key, value] of Object.entries(data)) {
         Kameleoon.API.Data.setCustomData('GA4 audiences',key)
 return { "value": null} 

Then, click on Advanced settings, add the JavaScript code below and replace [SITECODE] by the Kameleoon Sitecode. To find it, please follow this documentation.

Also, make sure you replace [WEB_PROPERTY] by the right GA4 property ID.

 var xhr = new XMLHttpRequest();
 var GA4Audiences = [];
 xhr.onreadystatechange = function () {
   if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
     var audiences = JSON.parse(xhr.response); (audience) {
       if (audience.value && audience.label !== "undefined") {
         GA4Audiences.push({ value: audience.value, label: audience.label });
 return GA4Audiences; 

Click on Validate to create the custom data.

That’s it! You will now be able to use your GA4 Audiences in our Segment Builder to target your visitors in Kameleoon campaigns, by choosing the custom data in our Segment Builder and selecting in the dropdown list is among the values, which will then show a list of every audience name from GA4.

External Segment Sync for Server-side

If you would like to use GA4 Audiences with one of our SDKs, you will need to follow the guidelines provided in each SDK documentation and use the method getRemoteData.

  • In this article :