How do I restrict or limit chats from specific countries?

Customer context: A customer wants to restrict or limit LiveAgent chats from specific countries on their website.

Customer: How do I restrict or limit chats from specific countries?

Answer: To restrict or limit chats from specific countries, you need to implement a custom script on your website that uses the free API at https://ipapi.co/ to determine the visitor's country based on their IP address. Replace "XXXXXXX" in the script with the URL of your LiveAgent and the chat button ID. This script will display the chat button only to users from the countries you specify.

<script type="text/javascript">
// Fetch the user's country based on their IP address
fetch('https://ipapi.co/json/')
.then(response => response.json())
.then(responseData => {
const countryCode = responseData.country;

if (countryCode === 'US') { // 'US' is the country code for USA (as an example)
// chat button code
(function(d, src, c) {
var t=d.scripts[d.scripts.length - 1],s=d.createElement('script');s.id='la_x2s6df8d';s.defer=true;s.src=src;s.onload=s.onreadystatechange=function(){var rs=this.readyState;if(rs&&(rs!='complete')&&(rs!='loaded')){return;}c(this);};t.parentElement.insertBefore(s,t.nextSibling);})(document, 'https://XXXXXXX.ladesk.com/scripts/track.js', function(e){ LiveAgent.createButton('XXXXXXXX', e); });
}
})
.catch(error => {
console.error('Error fetching IP data:', error);
});
</script>

×