Use Google Analytics Universal Segments (GA) in Kameleoon

portrait de l'auteur Julie Trenque

Written by Julie Trenque

Updated on 04/27/2022

5 min

Advanced

Configure your assets

Manage your integrations

Analytics

CDP/DMP

CMP

CMS

Developers

Others

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 GA segments 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 GA’s APIs to retrieve all possible values of this custom dimension within a specific segment then pushing the segment name (or ID) to Kameleoon by using our Data API.

We also provide native bridges with Google Universal Analytics and Google Analytics 4 to send campaigns meta data (campaign name and variation name) so that you can analyse your campaigns performance in GA.

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 segments (in GA) for this account (You can read this GA documentation: Share segments).

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 : https://console.cloud.google.com/home

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 GA 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 GA custom dimension

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

The basic requirement for this is to have a “reserved” dimension ID, that is to say one where no other data will be sent (we will use custom dimension 19 as an example below). Please set the dimension scope to “User” level (See GA documentation).

The following code has to be implemented (changing dimension19 to whichever dimension you will use) in the Kameleoon global custom script section.

if(Kameleoon.API.CurrentVisit.pageViews === 1){
    Kameleoon.API.Core.runWhenConditionTrue(function(){
        return typeof window.ga !== "undefined" && window.ga.getAll && window.ga.getAll().length > 0;
    },function(){
        var universalAnalyticsObject = window.ga;
        var universalAnalyticsTrackerPrefix = window.ga.getAll()[0].get("name");
     
        universalAnalyticsObject(
            universalAnalyticsTrackerPrefix + ".set",
            "dimension4",
            Kameleoon.API.Visitor.code
        );
     
        universalAnalyticsObject(
            universalAnalyticsTrackerPrefix + ".send",
            "event",
            "Kameleoon",
            "Bridge",
            "Segment",
            undefined,
            { nonInteraction: 1 }
        );
    },500)
}

It will set the dimension with the visitor code and trigger a tracking call to GA.

Create a custom data to use your GA Segments in Kameleoon

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

Name your custom data “GA segment” 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('GA segments',key)
 }
 });

 return { "value": null} 

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

var xhr = new XMLHttpRequest();

xhr.open("GET", 'https://customers.kameleoon.com/kameleoon/ga-segment/?sitecode=XXXXXX', false);

var gaSegments = [];

xhr.onreadystatechange = function() { 

   if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {

     var segments = JSON.parse(xhr.response);

     segments.map(function (segment) {

       if (segment.value && segment.label !== 'undefined') {

        gaSegments.push({value: segment.value, label: segment.label});

       }

     });

   }

}

xhr.send();

return gaSegments;

Click on “Validate” to create the custom data.

That’s it! You will now be able to use your GA Segments 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 segment name from GA.

External Segment Sync for Server-side

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

  • In this article :