Skip to main content

Disable/enable the chat for specific countries

This is a supplementary instruction to go along with the video for those of better understanding for written guides.

Please follow the guide below as due to update from the IP lookup provider, it includes additional steps not covered by the video.

Before proceeding please make sure to delete LiveChat tracking code if you already have it on your website or in case of installation via one of our plugins, please uninstall the plugin. Here’s an example of a script that will limit access to the chat for any IP’s whose origin is India.

<script>
var countryCode='';
function getCountryCode(data) {

countryCode = data.countryCode;

if(countryCode == 'IN')
{
console.log("You're from a forbidden country.");
}

else
{
window.__lc = window.__lc || {};
window.__lc.license = XXXXXXXX;
(function() {
var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.livechatinc.com/tracking.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
})();
}


}
</script>
<script src="https://extreme-ip-lookup.com/json/?callback=getCountryCode&key=u6swxxxxxxXbO"></script>

first - please replace the following code:

window.__lc = window.__lc || {};
window.__lc.license = XXXXXXXX;
(function() {
var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + '[cdn.livechatinc.com/tracking.js](http://cdn.livechatinc.com/tracking.js)';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
})()

with your tracking code (without the script tags), here’s an example of tracking code without script tags:

window.__lc = window.__lc || {};
window.__lc.license = XXXXXXX;
;(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))
}

Tracking code can be found in this section: https://my.livechatinc.com/settings/code after selecting “Install LiveChat code manually”.

To change which country has no access to your chat please modify the following line:

if(countryCode == 'IN')

IN is an ISO1366 Alpha 2 code for India - you can replace it with code for any other country, for example:

if(countryCode == 'TN')

if you’d like to block Tunisian IP’s from accessing the chat.

Full list of country codes can be found here: List of ISO 3166 country codes If you’d like to restrict multiple countries from accessing the chat - you can replace above line from the example with following:

if(countryCode == ['TN' || 'AT' || 'AZ' || 'BD' || 'BJ'])

In some scenarios it’s more efficient to define which countries should be able to access the chat instead of choosing which ones can’t access it, if you’d like to do that please swap places of this part of our snippet:

console.log("You're from a forbidden country.");

with the tracking code:

window.__lc = window.__lc || {};
window.__lc.license = XXXXXXXX;
(function() {
var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + '[cdn.livechatinc.com/tracking.js](http://cdn.livechatinc.com/tracking.js)';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
})();

Please create an account on the IP lookup providers website: https://extreme-ip-lookup.com/ and after logging in you can retrieve your API key as per screenshot below:

Custom attributes display example

here’s how the code should look like to allow the chat only for a few defined countries:

<script>
var countryCode='';
function getCountryCode(data) {

countryCode = data.countryCode;

if(countryCode == ['TN' || 'AT' || 'AZ' || 'BD' || 'BJ'])
{
window.__lc = window.__lc || {};
window.__lc.license = 9720060;
;(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))
}

else
{
console.log("You're from a forbidden country.");
}


}
</script>
<script src="https://extreme-ip-lookup.com/json/?callback=getCountryCode&key=u6swxxxxxxXbO"></script>

Your API key should be included inside of:


<script src="https://extreme-ip-lookup.com/json/?callback=getCountryCode&key=u6swxxxxxxXbO"></script>

like this:


<script src="https://extreme-ip-lookup.com/json/?callback=getCountryCode&key=[yourAPIKeyhere]"></script>

and here’s how the code should look like to block a few countries from accessing the chat:

<script>
var countryCode='';
function getCountryCode(data) {

countryCode = data.countryCode;

if(countryCode == ['TN' || 'AT' || 'AZ' || 'BD' || 'BJ'])
{
console.log("You're from a forbidden country.");
}
else
{
window.__lc = window.__lc || {};
window.__lc.license = 9720060;
;(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>
<script src="https://extreme-ip-lookup.com/json/?callback=getCountryCode&key=u6swxxxxxxXbO"></script>

Above code should be inserted into <body> of your website.

This implementation uses extreme ip lookup database (https://extreme-ip-lookup.com/) which allows free use for up to 10000 requests a month (10000 visits on your website in this case). It should be enough for small to medium sized websites, if you happen to go over the limit they do offer a paid plan.

If you have any questions, let us know!

Yours, LiveChat Team <3