Skip to main content

Routing to multiple groups on the same page randomly or with varying intensity

This will be useful mainly for licenses that want different agents to answer chats on the same pages but be unable to view each other’s chats but also for licenses that want to spread traffic between agents/groups on the same pages randomly or with higher/lower intensity.

NOTE: The Routing Rules that you can add in your LiveChat settings always have the priority over the code. It means that if you set some certain group to be loaded in the code (by adding the dedicated command - window.__lc.group = x;) and at the same time there is some Routing Rule that applies to the page where the code is added - the Routing Rules settings always overwrite the settings from the code. If the code says that group A should be loaded, but Routing Rules say that group B should be loaded, then the chat widget loads the group B.

The following custom script should be added to default LiveChat tracking code:

var response = (function() {
var randNum = Math.floor(Math.random() * (3) +1);
return randNum;
})();
console.log(response)
if (response == 1) { x = 0} else
if (response == 2) { x = 2} else
if (response == 3) { x = 3} else
console.log(x);
window.__lc.group = x;

it should be added below the license number line:

window.__lc.license = 13017213;

here’s a working example that loads one of 3 groups with defined ID’s (0,2,3) randomly on the page:

<script>
window.__lc = window.__lc || {};
window.__lc.license = 13017213;
var response = (function() {
var randNum = Math.floor(Math.random() * (3) +1);
return randNum;
})();
console.log(response)
if (response == 1) { x = 0} else
if (response == 2) { x = 2} else
if (response == 3) { x = 3} else
console.log(x);
window.__lc.group = x;
;(function(n,t,c){function i(n){return e._h?e._h.apply(null,n):e._q.push(n)}var e={_q:[],_h:null,_v:"2.0",on:function(){i(["on",c.call(arguments)])},once:function(){i(["once",c.call(arguments)])},off:function(){i(["off",c.call(arguments)])},get:function(){if(!e._h)throw new Error("[LiveChatWidget] You can't use getters before load.");return i(["get",c.call(arguments)])},call:function(){i(["call",c.call(arguments)])},init:function(){var n=t.createElement("script");n.async=!0,n.type="text/javascript",n.src="https://cdn.livechatinc.com/tracking.js",t.head.appendChild(n)}};!n.__lc.asyncInit&&e.init(),n.LiveChatWidget=n.LiveChatWidget||e}(window,document,[].slice))
</script>
<noscript><a href="https://www.livechatinc.com/chat-with/13017213/" rel="nofollow">Chat with us</a>, powered by <a href="https://www.livechatinc.com/?welcome" rel="noopener nofollow" target="_blank">LiveChat</a></noscript>

the script uses Math.random method to return number between 1 and in this case 3 randomly:

var randNum = Math.floor(Math.random() * (3) +1);

3 can be replaced with any other number - it should be equal to number of groups you want to split the traffic between following:

if (response == 0) { x = 0} else

if (response == 1) { x = 2} else

if (response == 2) { x = 3} else

those 3 lines define that if Math.random method returns:

0, group 0 will load on the page

1, group 2 will load on the page

2, group 3 will load on the page

x = ID of the group that should be loaded

if you have more than 3 groups you’d like to split traffic into - you can of course add more as shown below:

if (response == 1) { x = 0} else

if (response == 2) { x = 2} else

if (response == 3) { x = 3} else

if (response == 4) { x = 4} else

if (response == 5) { x = 5} else

to make the script work with more groups you will need to adjust 3 in snippet below:

var randNum = Math.floor(Math.random() * (3) +1);

this time our Math.random() method will return random number between 1 and 5

If you do not want to spread the traffic between groups randomly, you can increase probability of the group being assigned like this:

if (response == 1) { x = 0} else

if (response == 2) { x = 0} else

if (response == 3) { x = 2} else

if (response == 4) { x = 2} else

if (response == 5) { x = 3} else

With this script group 0 is loaded if Math.random method returns 1 or 2 and has 40% chance to be loaded each time page is loaded;

Group 2 is loaded if Math.random method returns 3 or 4 and also has 40% chance to be loaded each time page is loaded;

Meanwhile, group 3 is loaded only if Math.random returns 5 getting 20% chance for it to be loaded.

If you have any questions, let us know!

Yours, LiveChat Team <3