How to use IP location APIs for localized content

PersonalizationBy Catherine Meira

Creating personalized experiences is crucial for marketing professionals to keep up to date with the expectations consumers bare in online shopping these days. One way to do it is by implementing targeted content based on the user's location.

This strategy allows businesses to optimize experiences using IP location APIs. In this post, we will explore the most popular APIs and provide you with a step-by-step guide to integrate one into your website.

Increase conversion rate with geomarketingISZI increased the conversion rate by +39% and the average ticket by +88% using a simple three-step process.
A image showing audiences for onboarding

How IP location APIs work

IP location APIs are web services that allow businesses to access geolocation data associated with an IP address. When you research on geolocation by IP, the API queries its database and returns relevant information, including country, region, city, latitude/longitude, etc.

Here’s how it works:

  1. The API maps the IP address obtained from user server logs
  2. Then, the API queries its database for corresponding geographic locations
  3. The database is analyzed to identify patterns and correlations with the obtained IP addresses, using algorithms to enhance accuracy.

While some APIs will only give you information about cities or regions, the most advanced ones can even identify the ZIP code of the collected IP.

Adding IP location APIs to your website requires you to integrate it through HTTP requests and bring the JSON analysis to your teams as a result.

The accuracy of the location depends on the quality of the database used by the API provider, as well as the subscription plan you choose.

Choosing the best IP location API

There are many benefits to using IP location APIs. You can use them to improve targeted marketing efforts, ensure compliance, and prevent fraudulent activities.

These are the things you want to consider before choosing the best API service for your business:

Accuracy

APIs strive for accuracy, but they can display occasional discrepancies, especially when identifying specific regions or cities.

IP location APIs can determine the geographic location of an IP address by researching on multiple sources. Some APIs provide access to government databases, internet service providers, and user-contributed data, allowing companies to obtain more precise information. They establish what we call “data partnerships” with local providers. For this reason, a local IP location API may give you better results in its area.

MaxMind, for example, can access extensive information tailored exclusively for the US due to its local partnerships and user-contributed data. It makes their results more accurate in the US than in other locations.

These APIs usually focus on regions with high user demand to improve localization efforts. However, they can also research other areas to increase their database. Other factors, such as constant updates and the IP address, can impact the levels of accuracy as well.

Privacy concerns

Data protection laws consider IP addresses as personally identifiable information (PII). Therefore, you need to be careful on how you handle it.

If you use an IP location API, you should consider implementing security measures to ensure your website won't be susceptible to data leaking. You can include secure communication protocols, access controls, and security audits, for example. Also, your privacy policy must clarify how the geolocation informed by IP addresses is being used.

Proxy and VPN issues

Some users employ proxies or VPNs when browsing the internet. It can distort the actual location and lead to incorrect results.

If you have a B2C website, this probably won't be an issue because most visitors use their mobile with local, 3G, 4G, or 5G networks. However, as most people use VPNs for working purposes, this problem may impact B2B websites.

Frequency of database updates

Keeping the IP location database up to date is essential to ensure accurate results.

The frequency of updates varies among API providers, but it is generally recommended to update the database at least once a week. This ensures that you have the latest information to provide precise localized content.

Most popular IP location APIs

Several providers offer reliable data for IP location APIs. Here are some of the most popular APIs in the market in 2023:

1. MaxMind GeoIP2

Maxmind is famous for its precise geolocation accuracy and online fraud detection services. Their primary product is GeoIP2, which serves as their IP location API. They are one of the market's most precise APIs due to their extensive database, which is updated weekly. Moreover, they provide broad coverage in various countries and easy integration with anti-fraud tools (which is a standout for devs).

2. IP2Location

If you want an IP location API with a vast database and additional attributes, such as timezones, ISPs, and domain names, IP2 location is clearly your go-to. They also offer advanced filters, including weather, net speed, and area code. IP2 location provides businesses with the ability to integrate geolocation capabilities into applications, websites, or systems. Their database is updated monthly.

3. GeoNames

GeoNames is the best example of a community-fed database. Although users can greatly contribute, GeoNames relies primarily on data from various independent and governmental organizations worldwide. It offers a vast collection of geospatial data, names, coordinates, population, and administrative divisions, all free of charge. GeoNames allows developers to access and retrieve data programmatically. They also integrate with other popular applications and services.

4. IPinfo

IPinfo claims to have the most reliable and accurate IP address data API. It refines 20 terabytes of data monthly using unique algorithms through 5 steps: collect and clean, learn, verify, update, and analyze.

It also lets you download databases and play with it on their website.

5. Telize

Telize offers a REST API allowing you to get a visitor’s IP address and research location information from it. It supports both cross-origin resource sharing (CORS) and JSONP.

While its public API was permanently shut down in 2015, you can still subscribe to one of its paid plans. You’ll easily get essential users’ information for insights and marketing strategies.

How to integrate an IP location API into your website

Now, let's dive into the process of integrating an IP location API into your website. We'll use the MaxMind GeoIP2 as an example since it provides both the database for download and a wide range of official clients.

Using the database

You can create your own service API and use their database to retrieve the location data. Configuring a database reader and querying it basically consists in installing their client library:

1. Install the client library

As of writing, they provide clients for C#, Java, Node, PHP, Python, and Ruby.

2. Configure a database reader to query the database

Configuring the database reader requires the database file to be accessible on the filesystem.

3. Query the database

You can do that by calling the method corresponding to the database type (city or country, for example) and passing it the IP address you want to look up.

4. Handle the request response

If the lookup succeeds, the method call will return a response with multiple information associated with the given IP. If the request fails, the reader class will throw an exception or return an error, depending on the library.

5. Update your database

If you choose this path, it's essential to create an update process. You can, for example, do this by creating a daily, weekly, or monthly cron job.

Once you have the location data, your API should provide the application with all you need to personalize the webpage content based on the user's location.

Using the API

You can implement the API either using server-side or client-side integration. Let's focus on a simple client-side integration using Javascript.

Client-side implementations could add potential vulnerabilities to your website. Also, since these APIs' pricing is request-based, you can have undesired results if your credentials leak.

Implementing the JavaScript client involves registering your domain, adding a JavaScript snippet to your web page, calling the desired API method or service, and providing callbacks for all scenarios. This is how it goes:

1. Sign up and obtain an API key

Visit the MaxMind website and create an account. Once registered, obtain an API key to authenticate your requests.

2. Add a JavaScript snippet to your page

This is the Javascript snippet for the GeoIP2 API:

1<script src="//geoip-js.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>

3. Make a request and retrieve location data

Use one of the API methods to retrieve their location data. Extract the desired information from the API response, such as the country, city, or latitude/longitude coordinates.

4. Implement the localized content based on the user's location

Based on the retrieved location data, personalize the content displayed on your website to match the user's geographical location.

Here's a JavasScript code snippet illustrating the integration process of using MaxMind GeoIP2 API to personalize copy based on the user's location.

This is the page HTML:

1<html>
2 <head>
3 <title>Which city am I in?</title>
4 <script src="//geoip-js.com/js/apis/geoip2/v2.1/geoip2.js"></script>
5 <!-- make sure this is after the geoip2.js file -->
6 <script src="/example.js"></script>
7 </head>
8
9 <body>
10 <p>
11 You are currently in <span id="city"></span>.
12 </p>
13 </body>
14</html>

And this is the example.js file:

1var fillInPage = (function() {
2 var updateCityText = function(geoipResponse) {
3
4 /*
5 * It's possible that we won't have any names for this city, so you should provide a fallback result for cases like this.
6 * For language codes with a special character such as pt-BR, replace names.en with names['pt-BR'].
7 */
8 var cityName = geoipResponse.city.names.en || 'your city';
9
10 document.getElementById('city').innerHTML = cityName
11 };
12
13 var onSuccess = function(geoipResponse) {
14 updateCityText(geoipResponse);
15 };
16
17 // If we get an error, we will display an error message
18 var onError = function(error) {
19 document.getElementById('city').innerHTML = 'an error! Please try again..'
20 };
21
22 return function() {
23 if (typeof geoip2 !== 'undefined') {
24 geoip2.city(onSuccess, onError);
25 } else {
26 document.getElementById('city').innerHTML = 'a browser that blocks GeoIP2 requests'
27 }
28 };
29}());
30
31fillInPage();

IP location API alternative

If you got here, you're probably wondering if there's any other option to personalize your website content based on the user's location.

Using IP location APIs requires constant maintenance or frequent updates if you go with the database option. On top of that, you need to integrate it with your CMS to keep the personalized content in a platform your marketing team can easily manage. In some cases, you will need to change the source code every time your marketing or sales team tests banners and run other experiments.

On top of that, the extra integration with an AB testing platform to run experiments for different content and hypothesis can be chaotic and time-consuming.

To help you with that, we've created a headless CMS that comes with a built-in localization system and an AB testing mechanism. If you want to know how it works, here you'll find a quick overview and a simple getting started guide. It's free, and you should definitely try it out.

Example of localized content

Wrapping up

IP location APIs provide a way to determine the geographic location of an IP address. They query a database to retrieve information like country, region, and city associated with the user IP.

This data can be used to personalize content, target marketing efforts, and enhance user experiences based on location.

IP location APIs are valuable tools for businesses to leverage data and create tailored navigation journeys to increase brand authority. However, if you want to provide your teams with a low-code, high-security option, Croct is your best alternative for seamless integration and geolocalization without the difficulties of implementing an API.

Create a free account today and start exploring geolocation alternatives for seamless content.

Let's grow together!

Learn practical tactics our customers use to grow by 20% or more.