Documentation
NAV Navbar
  • Sample code for integration with a third-party platform, via the Activation API
  • Sample code for integration with a third-party platform, via the Activation API

    Context

    Nowadays, customers use a lot of different digital marketing platforms. Most of these tools are specialized in a given domain (Web Analytics such as Google Analytics, UX Analytics such as Hotjar, and so on). Integration between all these different platforms is usually quite important. For instance, during an A/B experiment, you may want to send to your UX analytics software the information regarding which variation a given visitor has been allocated to, in order to check if clicking behavior changes from one variation to the other.

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

    Sample Code

    var processExperimentForMyTargetPlatform = function(experiment)
    {
        // Here you should implement your own logic, depending on the APIs of your target platform
        // It can be something similar to:
    
        myTargetPlatform.addExternalData({"experimentId": experiment.id, "experimentName": experiment.name, "variationId": experiment.associatedVariation.id, "variationName": experiment.associatedVariation.name});
    }
    
    // We loop over the experiments, but the exact same logic could be used for personalizations as well
    
    Kameleoon.API.Experiments.getActive().forEach(processExperimentForMyTargetPlatform);
    
    window.addEventListener("Kameleoon::ExperimentTriggered", function(event) {
        processExperimentForMyTargetPlatform(event.detail.experiment);
    });
    

    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 perform a given action on each experiment. This given action is dependent on the target platform for which you're building the integration, although it will often be along the lines of obtaining the ids of the experiment and of the variation, and passing it to the third-party tool using their API or integration mechanism. By "active" we mean experiments or personalizations that have been triggered on the current page for instance. It's usually enough to run your JavaScript code everytime on every page, but you could implement more complex logic and only pass experiment information to your solution once per session, for instance.

    Since your integration code can either load and execute before, or after Kameleoon experiments trigger, you must both check the current active experiments at the start of your code, AND also stay informed of any new experiments that are triggered later. Be aware that since experiments can trigger at any time (for instance only after the user has clicked on a specific button), you can never assume that your code, when running, has all the required information about the active Kameleoon experiments at any given moment. Always setup an event listener (as shown in the sample code) to be alerted of any new experiment triggered in the future.