Integrate Kameleoon with any third-party analytics solution

portrait de l'auteur Julie Trenque

Written by Julie Trenque

Updated on 22/09/2020

2 min

Intermediate

Was this content useful?

Kameleoon comes with a lot of built-in integrations. However, if you happen to use a platform for which we do not provide an official analytics bridge yet, it is very easy to build your own custom analytics integration by leveraging our Activation API Events. This article provides the methodology and a sample code to do so, which you can easily adapt to achieve your desired integration.

Global custom script

On your Kameleoon App, click on “Administrate” in the left menu, then on “Sites”.


On the card of your website, click on “Configuration”: you access the configuration of your website on Kameleoon.

Unfold the “General” section.

The “Global custom script” insert will allow you to link Kameleoon to your analytics solution. Any JavaScript code you add in this insert will be executed each time the page is loaded. This feature is usually used to add complex tracking code or integration to other solutions.

To activate the bridge between Kameleoon and your analytics solution, please use the sample code below.

Sample code

Usually, the implementation logic goes this way:

  • You need to obtain a list of all active experiments or personalizations, then loop over this list and get the properties from each campaign you would like to send to your analytics solution (campaign name, variant name, etc.). You can get the whole list of properties for an experiment and the whole list of properties for a personalization.
  • Then, you need to pass them to the analytics tool using their APIs. Please refer to the analytics vendor solution.
  • Since your integration code can either load and execute before, or after Kameleoon campaigns trigger and activate, you must both check the current active campaigns at the start of your code, AND also stay informed of any new campaigns that are activated later. So we also setup an event listener (as shown in the sample code) to be alerted of any new experiment or personalization activated in the future.

You will need to implement in this code your own logic, which depends on the APIs of the analytics solution you use. Once it is done, you can copy and paste it in the “Global custom script” section.


 window.my_analytics_triggered_campaigns = window.my_analytics_triggered_campaigns || [];
 

 var processCampaignForMyAnalyticsPlatform = function(campaign)
 {
 var campaignId = campaign.id;
 var associatedVariationId = campaign.associatedVariation.id;
 var campaignName = campaign.name;
 var associatedVariationName = campaign.associatedVariation.name;
 

     // Here you should implement your own logic, depending on the APIs of your analytics solution
     // It can be something similar to:
 

 Kameleoon.API.Core.runWhenConditionTrue(function(){
 return typeof my_analytics() == "function"; //please use the analytics function and always check if the API function is available before using it.
 }, function(){
 if(my_analytics_triggered_campaigns.indexOf(campaignId) == -1){ 
 

 my_analytics().track({"experimentId": campaignId, "experimentName": campaignName, "variationId": associatedVariationId, "variationName": associatedVariationName});
 my_analytics_triggered_campaigns.push(campaignId);
 console.log("Tracking sent for campaign: "+campaignId);
 

 }
 }, 500);
 }
 

 // We loop over the experiments
 Kameleoon.API.Experiments.getActive().forEach(processCampaignForMyAnalyticsPlatform);
 window.addEventListener("Kameleoon::ExperimentActivated", function(event) {
 processCampaignForMyAnalyticsPlatform(event.detail.experiment);
 });
 

 // We loop over the personalizations
 Kameleoon.API.Personalizations.getActive().forEach(processCampaignForMyAnalyticsPlatform);
 window.addEventListener('Kameleoon::PersonalizationActivated', function (event) {
 processCampaignForMyAnalyticsPlatform(event.detail.personalization);
 }); 

You can click on “Validate” to apply the changes to your configuration and push them live on your website.

  • In this article :