2

I am using HAProxy for A/B testing and here is the snippet of my HAProxy configuration

listen  http 127.0.0.1:8080
    maxconn 18000

    use_backend a-version-backend if { req.cook(SITEID) -m beg a_version }
    use_backend b-version-backend if { req.cook(SITEID) -m beg b_version } 
    default_backend ab-test

backend ab-test
    balance roundrobin
    cookie SITEID insert indirect nocache maxlife 48h

    server server1 10.0.0.2:80 weight 25 cookie a_version
    server server2 10.0.0.3:80 weight 25 cookie a_version
    server server3 10.0.0.4:80 weight 50 cookie b_version

After reading the documentation, I realised that HAProxy can only create session cookie. Is it possible to create a persistent cookie in HAProxy ?

==== UPDATE ====

Sorry for the confusion, what I meant is persistent cookie (updated my question ) https://en.wikipedia.org/wiki/HTTP_cookie#Persistent_cookie

This is the cookie that set by HAProxy. It doesn't have expiry date and browser will treat it as session cookie https://en.wikipedia.org/wiki/HTTP_cookie#Session_cookie session cookie

The problem with session cookie is when user close their browser, it will delete the cookie. This is unacceptable for A/B test because they might come back to the website few days later and analytics tracking will treat them as different person.

And this is an example of persistent cookie from Facebook. It has an expiry date. persistent cookie

3
  • What are you trying to achieve? Doesn't haproxy already do what you want? The cookie SITEID will be stored in the user's browser (for 48 hours, because of maxlife 48h).
    – wurtel
    May 19, 2016 at 12:51
  • I don't get what's not working here..
    – GregL
    May 19, 2016 at 14:13
  • hi guys, thanks for the comments. I've updated my question. cheers.
    – Rudy Lee
    May 20, 2016 at 1:08

1 Answer 1

0

I managed to convert the HAProxy session cookie to persistent cookie using Google Tag Manager.

Since GTM available in both applications, I just need to create javascript code to convert the cookie to persistent cookie.

Here is the code that I put in GTM:

<script type="text/javascript">
(function() {
  function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
  }

  function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') {
        c = c.substring(1);
      }
      if (c.indexOf(name) == 0) {
        return c.substring(name.length, c.length);
      }
    }
    return "";
  }

  if(getCookie('SITEID').length !== 0) {
    setCookie('SITEID', getCookie('SITEID'), 365);
  }
})();  
</script>

The code basically checks whether you have SITEID or not. If you have the cookie, it will set the expiry date by one year.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .