NAV
  • Synchronization code methods
  • Synchronization code methods

    Several options are possible to synchronize the visitorCode identifier (via the kameleoonVisitorCode cookie) between front-end and back-end, and to avoid ITP issues on Safari:

    1. Add a DNS entry on your main domain.
    2. Modify the configuration of your front facing web server (or CDN) to generate and add the required cookie. This depends on your particular web server or CDN. We provide instructions for Apache as well as nginx.
    3. Implement a specific code snippet on your backend server, corresponding to the technology you use (Java, NodeJS, etc).
    4. Use one of our SDKs. Our SDKs include a synchronization method which you must call.

    DNS Entry

    You must create a DNS entry on your main domain (for instance kameleoon.customer.com). This should be a CNAME entry that points to synchronization.kameleoon.com. You will also need to provide an SSL certificate for the chosen domain. Contact your Kameleoon CSM and give him the certificate. He will then activate this synchronization for you.

    Web Server Configuration (or CDN)

    Nginx

    The Nginx module is currently in development and is expected to be released in November, 2021.

    Apache

    The Apache module is currently in development and is expected to be released in November, 2021.

    Akamai

    Documentation is currently being written to setup Akamai properly. In the meantime, please contact your Customer Success Manager to create the necessary configuration.

    Backend Code Snippets

    You can either host this code on a single specific URL (this URL should belong to your main top-level domain, but it can be on any subdomain), or on every page hosting / serving the Kameleoon application file (normally all pages of your website).

    If you created a single endpoint for synchronization, please contact your Customer Success Manager with the chosen URL to activate synchronization. In this case, the Kameleoon engine will make a single call (per session) to the designated endpoint to ensure the cookie is set by the server. The call is done asynchronously after the page has loaded.

    If you embed the code everywhere, no additional actions are needed. The cookie will be automatically created when normal page requests are made to your servers.

    Java

    javax.servlet.http.HttpServletRequest request; // Provide a reference to the HttpServletRequest
    javax.servlet.http.HttpServletResponse response; // Provide a reference to the HttpServletResponse
    String topLevelDomain = "MY_DOMAIN";// Here you must provide your own base domain, eg mydomain.com
    String visitorCode = null; // Here you can provide your own identifier if needed, else it will be generated randomly
    
    String cookiesHeader = request.getHeader("Cookie");
    if (cookiesHeader != null)
    {
        String[] cookies = cookiesHeader.split(";");
        for(String cookieNameAndValue :cookies)
        {
            String[] cookieNameAndValuePair = cookieNameAndValue.split("=");
            if (cookieNameAndValuePair[0].trim().equals("kameleoonVisitorCode"))
            {
                if (cookieNameAndValuePair[1].matches("^_js_(.*)"))
                {
                    visitorCode = cookieNameAndValuePair[1].substring(4);
                }
                else
                {
                    visitorCode = cookieNameAndValuePair[1];
                }
            }
        }
    }
    if (visitorCode == null)
    {
        final String alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
        final int alphabetLength = alphabet.length();
        final int visitorCodeLength = 16;
        final StringBuilder visitorCodeBuilder = new StringBuilder(visitorCodeLength);
        for (int i = 0; i < visitorCodeLength; ++i)
        {
            visitorCodeBuilder.append(alphabet.charAt(java.util.concurrent.ThreadLocalRandom.current().nextInt(alphabetLength)));
        }
        visitorCode = visitorCodeBuilder.toString();
    }
    
    javax.servlet.http.Cookie cookie = new Cookie("kameleoonVisitorCode", visitorCode.substring(100));
    cookie.setMaxAge(32_832_000);
    cookie.setHttpOnly(false);
    cookie.setPath("/");
    cookie.setDomain(topLevelDomain);
    response.addCookie(cookie);
    

    NodeJS

    let topLevelDomain = "MY_DOMAIN";// Here you must provide your own base domain, eg mydomain.com
    let visitorCode = null; // Here you can provide your own identifier if needed, else it will be generated randomly
    
    request.headers && request.headers.cookie.split(';').forEach(function (cookie)
    {
        let cookieNameAndValuePair = cookie.match(/(.*?)=(.*)$/);
        if (cookieNameAndValuePair[1].trim() == "kameleoonVisitorCode")
        {
            let value = cookieNameAndValuePair[2].trim();
            if (value.indexOf("_js_") != -1)
            {
                visitorCode = value.substring(4);
            }
            else
            {
                visitorCode = value;
            }
        }
    });
    if (! visitorCode)
    {
        let alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
        let alphabetLength = alphabet.length;
        visitorCode = "";
        for (let i = 0; i < 16; ++i)
        {
            let randomNumber = Math.floor(Math.random() * alphabetLength);
            visitorCode += alphabet[randomNumber];
        }
    }
    
    let expires = new Date(new Date().getTime() + 32832000000);
    response.writeHead(200, {'Set-Cookie': [`kameleoonVisitorCode=${visitorCode}; expires=${expires}; path=/; domain=${topLevelDomain};`]});
    

    PHP

    <?php
    $topLevelDomain = "MY_DOMAIN";// Here you must provide your own base domain, eg mydomain.com
    $visitorCode = NULL; // Here you can provide your own identifier if needed, else it will be generated randomly
    
    if (isset($_COOKIE["kameleoonVisitorCode"]))
    {
        $value = $_COOKIE["kameleoonVisitorCode"];
        if (strpos($value, "_js_") !== false)
        {
            $visitorCode = substr($value, 4);
        }
        else
        {
            $visitorCode = $value;
        }
    }
    if (is_null($visitorCode))
    {
        $alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
        $alphabetLength = strlen($alphabet);
        $visitorCode = "";
    
        for ($i = 0; $i < 16; $i++)
        {
            $randomNumber = floor((mt_rand() / mt_getrandmax()) * $alphabetLength);
            $visitorCode .= substr($alphabet, $randomNumber, 1);
        }
    }
    
    setcookie("kameleoonVisitorCode", $visitorCode, time() + 32832000, "/", $topLevelDomain); 
    ?>
    

    Copy and paste this code, and replace MY_DOMAIN by your base top-level domain (ie, mydomain.com).

    C#

    
    Microsoft.AspNetCore.Http.HttpRequest request; // Provide a reference to the HttpRequest
    Microsoft.AspNetCore.Http.HttpResponse response; // Provide a reference to the HttpResponse
    String topLevelDomain = "MY_DOMAIN";// Here you must provide your own base domain, eg mydomain.com
    String visitorCode = null; // Here you can provide your own identifier if needed, else it will be generated randomly
    
    if (request.Cookies.ContainsKey("kameleoonVisitorCode"))
    {
        String cookieValue = request.Cookies["kameleoonVisitorCode"];
        if (cookieValue.Contains("_js_"))
        {
            visitorCode = cookieValue.Substring(4);
        }
        else
        {
            visitorCode = cookieValue;
        }
    }
    if (visitorCode == null)
    {
        String alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
        int alphabetLength = alphabet.Length;
        String randomVisitorCode = "";
        Random rnd = new Random();
        for (int i = 0; i < 16; i++)
        {
            int randomNumber = rnd.Next(alphabetLength);
            visitorCode += alphabet[randomNumber];
        }
    }
    
    Microsoft.AspNetCore.Http.CookieOptions cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions.CookieOptions();
    cookieOptions.Expires = DateTime.Now.AddSeconds(32_832_000);
    cookieOptions.HttpOnly = false;
    cookieOptions.Path = "/";
    cookieOptions.Domain = topLevelDomain;
    response.Cookies.Append("kameleoonVisitorCode", visitorCode, cookieOptions);
    

    Python

    import random
    
    topLevelDomain = "MY_DOMAIN" # Here you must provide your own base domain, eg mydomain.com
    
    def fill_visitor_code(request, response, customer_visitor_code): # Here you can provide your own identifier via customer_visitor_code if needed, else it will be generated randomly
        visitor_code_cookie = request.cookies.get("kameleoonVisitorCode")
        if "kameleoonVisitorCode" in request.cookies:
            visitor_code_cookie = request.cookies.get("kameleoonVisitorCode")
            visitor_code = visitor_code_cookie[4:] if "_js_" in visitor_code_cookie else visitor_code_cookie
        else:
            visitor_code = customer_visitor_code if customer_visitor_code is not None else "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(16))
        response.set_cookie("kameleoonVisitorCode", visitor_code[0:100], max_age=32832000, domain=topLevelDomain)
    
    fill_visitor_code(request, response) # The method must be called with correct arguments (depending on your Python web framework)
    

    Backend SDK

    Refer to the documentation of the particular SDK you're using (the important part is the obtainVisitorCode() method).