Skip to main content

Application file self-hosting with Cloudflare CDN

This article aims to provide a quick guide to self-hosting Kameleoon application file for Web Experimentation using Cloudflare CDN. Hosting the Kameleoon application file on your Cloudflare can give noticeable performance boost, by removing the additional DNS query and SSL handshake needed if you use our CDN.

Setting up a worker in Cloudflare

Log in to your Cloudflare account and create a worker to point the original request back to your Kameleoon Web Experimentation snippet. You will need to configure the necessary Cloudflare route and script.

Route

Add the following path: *<your_website_url_here>/kameleoonjs/script.js.

Script

You will need a script that listens for requests from the /kameleoonjs/ route, and replaces that route with the one that leads to your Kameleoon snippet.

You can use the script below, where YOUR_SITECODE has to be replaced with your Kameleoon Project ID (please follow the steps outlined in this article to retrieve it).

async function handleRequest(request { 
const url = "https://YOUR_SITECODE.direct.kameleoon.eu/kameleoon.js";
return fetch(url)
}

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

Add the Kameleoon snippet in your source code

note

Refer to this article to understand how to implement Kameleoon on your website.

The final step is to replace the default Kameleoon Application JavaScript URL in the installation tag by your Cloudflare URL (eg. <your_website_url_here>/kameleoonjs/script.js if you have chosen this route). For instance, let's say you are using the JavaScript File (Asynchronous Loading with Anti-Flicker) implementation method. The Kameleoon application file is by default hosted on //SITE_CODE.kameleoon.eu/kameleoon.js. You just need to change this URL in the installation tag, replacing it with your own URL.