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 many built-in integrations. However, if you happen to use a platform for which we don’t yet provide an official analytics bridge, it’s 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 for your website, click on “Configuration”: you’ll now access the configuration of your website on Kameleoon.

Unfold the “General” section.

The “Global custom script” insert will enable you to link Kameleoon to your analytics solution. Any JavaScript code you add in this insert will be executed every 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 works 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, variation name, etc.). You can get the full list of properties for an experiment and the full list of properties for a personalization.
  • You then need to send these 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 stay informed of any new campaigns that are activated later. So, we also set up 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 into this code your own logic, which depends on the APIs of the analytics solution you use. Once this is done, you can copy and paste it into 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 send them live on your website.

  • In this article :