Skip to main content
Google Analytics 4 (GA4) groups visitors into audiences by behavior, such as people who viewed a product page several times but didn’t buy. Kameleoon can’t see those audiences until you connect the two products. The bridge in this article sends each visitor’s Kameleoon visitor code to GA4, and it brings your GA4 audiences into Kameleoon as custom data that you can select in a segment.

How GA4 audiences fit into your workflow

  • Set up authentication: Create a Google Cloud service account, and give it Viewer access to your GA4 property.
  • Push visitor codes to GA4: Send each visitor’s Kameleoon visitor code to GA4 in a custom dimension, with gtag.js or Google Tag Manager.
  • Create custom data: Add your GA4 audiences to Kameleoon as custom data, so you can select them in a segment.

Before you begin

  • Administrator access: An email account with administrator access to your GA4 account. You use it to create a Google Cloud project and service account, and it needs access to every audience you want to use.
  • Information for your Customer Success Manager: Send the following to your Kameleoon Customer Success Manager, who sets up the bridge in your account:
    • The credentials of the service account, in JSON format.
    • Your GA4 property ID.
    • Your Kameleoon project’s sitecode.

Step 1: Set up authentication with a service account

A service account lets the bridge call the Google Analytics APIs automatically, without an authentication callback.

Create a project in Google Cloud console

Create a Google Cloud project to hold the APIs and the service account for this integration.
  1. Go to the Google Cloud console, and sign in with your administrator email account.
  2. Create a new project.
  3. Name the project, and choose your organization and location.

Activate the APIs

Activate the three Google Analytics APIs in your project, so the bridge can read your GA4 data.
  1. Click APIs & Services > Library.
  2. Search for each of these APIs, and activate it:
    • Google Analytics Reporting API
    • Google Analytics Data API
    • Google Analytics API

Create a service account

Create a service account, and download a JSON key for it.
  1. Click IAM & Admin > Service accounts.
  1. Click Create service account.
  1. Enter a name and a description for the account, so its purpose is clear. Google generates the service account’s email address automatically.
  2. Choose the Viewer role. Kameleoon recommends this role.
  1. Once you create the account, click Keys.
  1. Click Add key > Create a new key.
  1. Choose the JSON format, and click Create to generate the key.
The key downloads to your computer. Secure the file with a password by using a compression tool, and send the password separately from the file. Then, send the file to your Kameleoon Customer Success Manager.

Grant the service account Viewer access to your GA4 property

Give the service account Viewer access, so it can read data from your GA4 property.
  1. Sign in to your GA4 account, and select the property you want to manage.
  2. Open the Admin settings.
  3. Look for the Property User Management or Property Access Management option.
  4. Click + > Add user.
  1. In the Email addresses field, enter the email address of the service account.
  2. In the Standard roles section, select Viewer.
  3. Click Add.

Step 2: Push Kameleoon visitor codes to a GA4 custom dimension

Send each visitor’s Kameleoon visitorCode to GA4 through a custom dimension. Choose the method that matches how your site loads GA4.

Use gtag.js

1. Push the visitor code to GA4

Add the following code in the Kameleoon global custom script, on your project’s Scripts tab:

2. Create the custom dimension in GA4

Create a custom dimension that receives the Kameleoon visitorCode for each visitor.
  1. Set the dimension scope to User.
  2. Name the dimension Kameleoon Visitor Code.
  3. Enter kameleoon_visitor_code as the user property.

Use Google Tag Manager

1. Push the visitor code in the dataLayer

Add the following code in the Kameleoon global custom script, on your project’s Scripts tab:

2. Create a variable in GTM

Create a Data Layer Variable that reads the Kameleoon visitor code from the dataLayer.
  1. Open Google Tag Manager, and navigate to your container.
  2. Click Variables in the left-hand menu.
  3. Under User-Defined Variables, click New.
  4. Click Variable Configuration, and choose Data Layer Variable.
  5. Configure the variable:
    • Name: kameleoon_visitor_code
    • Variable type: Data Layer Variable
    • Data Layer Variable Name: kameleoon_visitor_code
  6. Click Save.

3. Create a trigger in GTM

Create a trigger that fires when the Kameleoon visitor code event reaches the dataLayer.
  1. Click Triggers in the left-hand menu, and then click New.
  2. Click Trigger Configuration, and choose Custom Event.
  3. Configure the trigger:
    • Trigger name: A descriptive name, for example, Kameleoon visitor code.
    • Event name: Kameleoon Visitor Code, which is the event the script pushes.
    • This trigger fires on: Choose Some Custom Events.
    • Conditions: Set kameleoon_visitor_code to match the regular expression ^[a-z0-9]{16}$, which matches the format of a visitor code.
  4. Click Save.

4. Attach the trigger to a tag

Create a GA4 tag that sends the visitor code to GA4, and attach the trigger to it.
  1. Click Tags in the left-hand menu.
  2. Create a tag, or edit an existing one.
  3. In the tag configuration, scroll to the Triggering section, click Triggering, and select the trigger you created.
  1. Click Save.

Step 3: Create custom data to use your GA4 audiences in Kameleoon

To use your GA4 audiences in Kameleoon, create custom data that retrieves them for each visitor.

Create the GA4 Audiences custom data

Create custom data that reads each visitor’s GA4 audiences.
  1. Click Settings > Custom Data, and then click New custom data.
  2. Enter GA4 Audiences as the name, and select your project.
  3. Set the Retrieval method to Custom JavaScript code.
  4. Set Type to List, Format to String, and Scope to Visit or Page. With the Page scope, Kameleoon resets the value on each new page.
  1. Paste the following code into the code field.

Add the audience names to the custom data

Add the audience names to the custom data, so the segment builder lists every GA4 audience. Complete these steps on the options page, which opens when you click Next.
  1. Click Next to open the options.
  2. Turn on Save the values for the targeting condition associated with the custom data point. A code field appears.
  3. Paste the following script into the code field. Replace [SITECODE] with your sitecode, and replace [WEB_PROPERTY] with your GA4 property ID.
  1. Click Create.
The GA4 Audiences custom data appears in your list of custom data. If the returned value has the wrong format, Kameleoon shows an error when you click Next.

Build a segment from GA4 audiences

Build a segment that targets visitors in one or more GA4 audiences.
  1. Click Settings > Segments, and then click New segment.
  2. Enter a name, select your project, and click Next.
  3. In the Conditions column, search for Custom data, and double-click it to add the condition.
  4. Select GA4 Audiences in the condition, choose is among the values, and then select the audiences you want to target.
  5. Choose whether to include or exclude the visitors who match the condition.
The segment appears in the Segments list. To learn more about segments, see create a segment.

Use GA4 audiences with an SDK

To use GA4 audiences with a Kameleoon SDK, follow the guidance in that SDK’s documentation. The iOS SDK, for example, uses the getRemoteData() method.