Explore our IP Address Database Downloads for instant access to our IP address insights

Learn more
  1. IPinfo
  2. Blog
  3. How To Automatically Detect And Select Visitor’s Country
How To Automatically Detect And Select Visitor’s Country
4 years ago by IPinfo Team 7 min read

How to automatically detect and select visitor’s country

Say that you’re starting a new website or want to make the current one better. There’s one thing you’re certain about — you want the website converting visitors at the highest rates possible and providing good user experience for every website visitor.

There are many ways of web personalization that can help you achieve these two goals but today we will focus on one of the most important — country-based personalization.

Why You Might Need to Change Your Website Content Based on Visitor Country?

No two persons want the entirely same things and there’s no reason to treat them as same on your website or app either. Their desires and needs will differ based on many factors including where they live. Someone from the United States, for instance, will have different wants than someone from the Czech Republic or Australia.

The best way to convert visitors into customers is to segment and personalise what you offer on your website. One of the most important steps in this effort is to offer each visitor unique and dynamic content based on their geolocation or their country.

There are actually quite a few reasons to change your website content based on visitor country:

  • To show them a more targeted offer.

If your website caters to the audience from the United States, for example selling flatscreens, and someone from the Netherlands makes a bulk order of 20 monitors for their office, would you even be able to deliver that and deal with customs? Even if you do, would you later be able to provide adequate customer and tech support? Probably not.

At the same time, if you’re running holiday promotions does it make sense to offer a discount promo on Independence Day on September 7th to someone in Italy? They might not say no to a discount (who would?) but ultimately you’ll have a much better chance of selling if you localize your promotions based on the visitor’s geolocation.

  • To ban visitors from certain countries or regions from your website.

It might seem counterintuitive to ban visitors from coming to your website from certain countries, but if you are not offering anything to them they are just costing you extra resources, with no return.

Take the popular streaming service Netflix as an example. We all want to see the latest movies and TV shows, but Netflix can’t show the same content to everyone and instead has to implement certain country restrictions.

Get accurate geolocation data

Enhance user experience and strengthen your security with rich geolocation data.

Learn More
Get accurate geolocation data

For instance, a new TV show might be available only in the U.S. and Canada, but not outside. This is because every country has its own content licensing laws and Netflix would have to license that show in each country separately. Not to mention that there might be too low demand for that show in a particular country.

  • To make user experience smoother by pre-populated web forms.

Web forms are sometimes a necessary evil, but that doesn’t mean that they benefit the user experience. After all, who likes to fill out a long list of questions just to open an account or buy something from a website?

They’re confusing, frustrating, and completely ruin website’s usability. And, if on top of that it’s in a language that’s not native to your visitors, don’t expect them to make a big effort of filling out the form.

But, as we said, sometimes web forms are a necessary evil and you can’t go around them. You can still make them at least somewhat manageable by using customer geolocation data to pre-populate some fields and make your users experience a bit smoother.

  • To make checkout and payment process easier for customers.

Not everyone in the world is using the dollar as their currency. In fact, most of the countries in the world have their own currencies in which they prefer to pay. Your website should offer visitors the option to pay in at least the currencies for the countries and regions that you’re serving.

In addition to the currency, customers from different countries might also have a preferred way of paying online. Some, like in the U.S. might use international card schemes like MasterCard or Visa. In Europe, they might use local payment options more or mobile banking. Other places might go for PayPal or their local debit card scheme.

Knowing where the customer is coming from and then providing the option to pay in their favorite way will greatly increase your chances of a successful sale. You can check out the preferred way to pay in countries worldwide in this article by Mercury Minds.

  • To follow different privacy laws and regulations.

Privacy laws can be very different from one country to another. If you intend to service different countries you need to understand and follow the local privacy laws, but also privacy habits.

Compare, for instance, privacy laws in the U.S. and the E.U. In America, consumer privacy is often on a per-case basis and is regulated by various agencies and independent bodies. Every state has its own set of laws and regulations that you have to adhere to and if the administration changes, so too can the laws.

On the other hand, the European Union has a much more centralised approach to privacy and has relatively recently (May 25th, 2018) introduced the General Data Protection Regulation (GDPR) act. The GDPR sets guidelines for collecting and processing of personal information from EU citizens. This means that, if you own a business in the U.S. but also have Europeans coming to you and are collecting data from them, you have to make sure to follow the GDPR.

Of course, it would be very difficult to know which regulations to follow without knowing where the user is coming from. Again, this is where geolocation comes in.

How Can You Detect Visitor Country of Origin?

You basically have two options to identify the visitor’s country of origin.

  1. IP to Geolocation Lookup.
  2. User-side Geolocation Data.

Since User-side Geolocation requires asking people where they come from and they might not want to give you that information, this method isn’t as good so we’ll focus on detecting visitor location using IP address lookup instead.

If you look for “IP Geolocation” on the Internet, you’ll see that there are quite a few lookup services available.

Not all of them are the same, however. They differ in many ways, but most importantly accuracy. For instance, one IP geolocation lookup service might be accurate 99.5% on a country level, while another only 90%. Which one do you think will give you better results? The 99.5% one, of course.

IPinfo’s IP to Geolocation API provides the most accurate and in-depth IP data, including those pertaining to visitor data such as their country, region, city/town, postal/ZIP code, latitude and longitude and other information that can help you customize your content better.

How does this work?

Let’s say you want to customize your content by visitor country using IPinfo or block certain visitors from specific countries to visit your site.

With IPinfo.io you can do this in two ways:

On the Client-Side

On the client-side, you can first use the IP Geolocation API’s JSONP or CORS support to the user’s location.

For instance, here’s how that looks with JSONP:

const request = await fetch("https://ipinfo.io/json?token=$TOKEN");
const jsonResponse = await request.json();

const welcomElem = document.querySelector("#welcome");

if (jsonResponse.country === "US") {
    welcomElem.innerHTML = "Welcome, American!";
} else if (jsonResponse.country === "GB") {
    welcomElem.innerHTML = "Welcome, Brit!";
} else {
    welcomElem.innerHTML = "Hello!";
}

On the Server-Side

Another way to do this is on the server-side. This way, you can also block users from seeing any content from the server.

Here’s how it looks when a non-US user tries to visit such a website:

$country = file_get_contents(
   "http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}/country?token=$TOKEN"
);
if(trim($country) != "US") {
   print("Non-US users not welcome!");
   exit();
}

What if I Want to Display Full Country Names?

As you can see, the visitor country is displayed in an ISO-2 country code. As such, the United States is displayed as “US”, United Kingdom as “GB”, Germany as “DE” and so on.

For example, if you look up one of Facebook’s IPs (69.63.176.13), you’ll see this:

{
  "ip": "69.63.176.13",
  "city": "Menlo Park",
  "region": "California",
  "country": "US",
  "loc": "37.4538,-122.1822",
  "org": "AS32934 Facebook, Inc.",
  "postal": "94025",
  "timezone": "America/Los_Angeles",
  "readme": "https://ipinfo.io/missingauth"
}

Notice that the “country” is only displayed as “US”. But what if you want to show the full country name?

In that case, you can use a country name JSON file from country.io.

{
 "BD": "Bangladesh",
 "BE": "Belgium",
 "BF": "Burkina Faso",
 ...,
 "US": "United states",
 ...,
}

All you need to do is download and import the JSON file and then lookup the full country name and compare it with its country code in our API.

Why do this with IPinfo and Not Another IP Lookup?

What difference does it make what IP Geolocation API do you use? A lot in fact. It may not look like much, but there’s a huge difference between, say, 95% and 99.5%.

The more accurate the results you get, the better you can customize your users’ experience. Need a fast, reliable, and accurate IP to Geolocation API? Try IPinfo.

IPinfo is a comprehensive IP address data and API provider with flexible pricing plans to meet your business needs. We handle billions of API requests per month, serving data like IP geolocation, IP to company, IP to mobile carrier, and more. Sign up for a free account or contact our data experts to learn more.