Integrate Kameleoon with Segment

portrait de l'auteur Julie Trenque

Written by Julie Trenque

Updated on 04/29/2022

5 min

Advanced

Manage your integrations

Analytics

CDP/DMP

CMP

CMS

Developers

Others

Was this content useful?

With this integration you can seamlessly connect Kameleoon with Segment.io to:

  • push campaign exposure event data (campain name and variation name) to Segment.io, which can then be used by other platforms available in Segment.io marketplace;
  • import custom event and revenue;
  • import personas and use them in Kameleoon Segment Builder.

Push Kameleoon data into Segment

Enable Segment.io integration on Kameleoon

On the Integrations page

Log in to your Kameleoon App, click on “Administrate” and then “Integrations” in the sidebar.

For more information on this page, see our dedicated article.

By default, the tool is not installed. This is signaled by this icon:

With a click on “Install the tool”, you will be able to select the websites on which you want to activate it.

Once you have configured the tool, click on “Validate” in the bottom-right corner: the configuration panel closes. You will then see a “ON” toggle on the right of the tool’s line, as well as the number of websites the tool is configured on.

The results of your experiments will be transmitted to Segment.io if the script of the tool is installed on your pages.

Activate Segment.io on a campaign

In an A/B experiment

Once Segment.io is activated on the Integrations page, you can select it as a reporting tool at the “Tracking and goals” step of the finalization panel (or in the Code editor).

In a personalization

Once Segment.io is activated on the Integrations page, you can select it as a reporting tool on the personalization creation page.

On the results page

It is also possible to select it among the reporting tools on the results page.

In the right sidebar, click on “Reporting Tools” and then on “Edit”.

Select Segment.io and don’t forget to validate!

Data transmission

As soon as Segment has been installed and defined as a reporting tool in a campaign, an event called “Experiment viewed” will be sent automatically every time a visitor sees a variation of an active Kameleoon A/B experiment.

More information

Import Segment event and personas into Kameleoon

With this Segment x Kameleoon integration, every event that is triggered by Segment will be automatically sent to Kameleoon, saving developers from having to re-implement an existing Segment Tracking plan in Kameleoon. Page, Screen and Track events will be automatically collected as a Kameleoon custom goal. Track events of type “Audience Entered” or “Audience exited” will be collected as a Kameleoon custom data to be used in our Segment builder.

Step 1: Generate an API key

First, you will have to request an APIKey in order to use this integration. Please ask your Kameleoon Customer Success Manager for it or send us an email to support@kameleoon.com.

Step 2: Get your sitecode

You will also need to get your sitecode in your Kameleoon account.

To learn how to find your sitecode, please read this documentation.

Step 3: Enter this information in your Segment App

  • From the Destinations catalog page in the Segment App, click “Add Destination”.
  • Search for Kameleoon in the Destinations Catalog, and select the Kameleoon destination.
  • Click on “Configure Kameleoon”.
  • Choose which Source should send data to the Kameleoon destination.
  • Enter the APIKey and the sitecode in the Kameleoon destination settings in Segment.

Step 4: Matching Users between Kameleoon and Segment

The integration requires that you use the same system of identifiers for both tools, meaning the userId value you pass to Segment should be the same value as Kameleoon uses to identify a “visitor”.

If you use Kameleoon Experiment or Kameleoon AI Personalization on your website, Kameleoon places a cookie that contains an anonymous unique identifier, called the Kameleoon visitorcode, randomly assigned to a visitor. This ID is used to uniquely identify a browser. You have several options available:

  • You can pass our visitorcode in the userId property of the Segment identify call to ensure Kameleoon can attribute all events data received from Segment to the right visitor.
  • You can specify in all subsequent Segment calls an additional user property “k_visitorCode“, whose value is the Kameleoon visitorcode. You can retrieve the user visitorCode from the browser by using our Activation API Kameleoon.API.Visitor.code. Here is an example of a track event call with the user property “k_visitorCode“:  analytics.track(‘Add to cart’, {“k_visitorCode”: Kameleoon.API.Visitor.code}).
  • You can pass our visitor code in an additional Segment call as follow: analytics.track('Kameleoon identifier', {'k_visitorCode': Kameleoon.API.Visitor.code}). Kameleoon will automatically link the Segment user ID or anonymous ID to our own visitorCode. Make sure to call it only once per session. For instance, you can use this code example in the Global custom script section of the Kameleoon Site property.
Kameleoon.API.Core.runWhenConditionTrue(
     () => window.analytics && window.analytics.track && window.analytics.user,
     () => {
         const kSegmentioIdentifier = window.localStorage.getItem('k_segmentio_identifier');
         if(kSegmentioIdentifier && kSegmentioIdentifier === window.analytics.user().anonymousId()) return;
         window.localStorage.setItem('k_segmentio_identifier', window.analytics.user().anonymousId());
         window.analytics.track('Kameleoon identifier', {'k_visitorCode': Kameleoon.API.Visitor.code})
     }
 );

If you use Kameleoon Full Stack, please refer to the Kameleoon SDK documentation as you would be able to set your own ID instead of using a generated Kameleoon Visitor Code and so have the exact same one between Segment and Kameleoon.

Step 5: Supported Segment events and Personas

Kameleoon supports the following methods, as specified in the Segment Spec.

Behind the scenes, if the Segment event name matches exactly the name of an existing goal in your Kameleoon account, a conversion for this goal will be associated to the visitor. If the goal does not exist, Kameleoon will create a custom goal by using the Segment event name and associate the conversion to the visitor. The goal will appear in the Kameleoon goals page with the name convention ‘SegmentIO eventType – eventName’. Once the goal has been created, you can use them in any of your campaign.

Segment track calls of type “Audience Entered” and “Audience Exited” will be collected as a Kameleoon custom data.

Page

Send Page calls to record whenever a user sees a page of your website, along with any optional properties about the page. Calling page or screen in one of our sources is one of the first steps to getting started with Segment.

For example:

analytics.page('Home', {"k_visitorCode": "oa16i4syt2ve3b0z"});

OR

analytics.page('Home');

Screen

Send Screen calls to record whenever a user sees a screen, the mobile equivalent of page, in your mobile app, along with any properties about the screen. Calling page or screen in one of our sources is one of the first steps to getting started with Segment.

For example:

[[SEGAnalytics sharedAnalytics] screen:@"Home"
properties:@{ @"k_visitorCode": @"oa16i4syt2ve3b0z" }];

OR

[[SEGAnalytics sharedAnalytics] screen:@"Home"];

Segment sends Screen calls to Kameleoon as a screenview.

Track

Send Track calls to record any actions your users perform, along with any properties that describe the action.

For example:

analytics.track('Login Button Clicked', {"k_visitorCode": "oa16i4syt2ve3b0z"});

OR

analytics.track('Login Button Clicked');

Segment sends Track calls to Kameleoon as a track event.

Personas

Kameleoon allows you to target users based on their belonging to a Segment persona.

In order to be able to use personas in our Segment builder, you will need to set up a custom data in your Kameleoon account for the site of your choice.

You can choose the name of your choice (eg. SegmentIO personas) and the custom data must be of type “List of string”.

If you use our Kameleoon visitor code as your Segment’s User ID in all track event calls, please use the code below in the custom data acquisition method:

window.k_segmentIO = window.k_segmentIO || {};
 if(!window.k_segmentIO.runtime)
 {
     window.k_segmentIO.runtime = true;
     const user_id = Kameleoon.API.Visitor.code;
     Kameleoon.API.Data.retrieveDataFromRemoteSource(user_id, cohorts => {
         window.k_segmentIO.cohorts = cohorts && cohorts !== '{}' ? Object.keys(cohorts).filter(key => cohorts[key]) : [];
     });
 }
 if(!window.k_segmentIO.cohorts || !window.k_segmentIO.cohorts.length) return;
 return {"value": window.k_segmentIO.cohorts, "overwrite": true};

If you rather use SegmentIO default Identify method to set your own user ID, please use the code below:

if(!window.analytics || !window.analytics.user) return;
 window.k_segmentIO = window.k_segmentIO || {}; if(!window.k_segmentIO.runtime)
 {
     window.k_segmentIO.runtime = true;
     const user_id = window.analytics.user().id() || window.analytics.user().anonymousId();
     Kameleoon.API.Data.retrieveDataFromRemoteSource(user_id, cohorts => {
         window.k_segmentIO.cohorts = cohorts && cohorts !== '{}' ? Object.keys(cohorts).filter(key => cohorts[key]) : [];
     });
 }
 if(!window.k_segmentIO.cohorts || !window.k_segmentIO.cohorts.length) return;
 return {"value": window.k_segmentIO.cohorts, "overwrite": true};

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

var xhr = new XMLHttpRequest();
xhr.open("GET", 'https://api-data.kameleoon.com/data?key=common&siteCode=XXXXXXXXX', false);
var SegmentIOPersonas = [];
xhr.onreadystatechange = function() { 
   if(this.readyState === 4) {
     var segmentIOcohorts = JSON.parse(xhr.response);
	  if (segmentIOcohorts && segmentIOcohorts.cohorts) {
		  segmentIOcohorts.cohorts.forEach(persona => SegmentIOPersonas.push({value: persona, label: persona}) );

	   }
   }
}
xhr.send();
return SegmentIOPersonas;

Click on “Validate” to create the Custom Data.

That’s it! You will now be able to use Segment Personas in our Segment Builder.

External Segment Sync for Server-side

If you would like to use Segment Personas 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 :