Use ipdata’s Geolocation Data to Protect & Customize Your Site

Share this article

Use ipdata’s Geolocation Data to Protect & Customize Your Site

This article was created in partnership with ipdata. Thank you for supporting the partners who make SitePoint possible.

Modern websites are becoming more and more effective at customizing content based on their visitors’ location. They can redirect users to a page in their own language, display prices in the local currency, pre-fill webforms with location information, and show the current time and date for the correct timezone.

ipdata is a low-latency API that provides website owners with a wide variety of information about their visitors based on IP address (IPv4 and IPv6). Think of it as an IP geolocation and threat intelligence API.

By using a visitor’s IP address you can learn their continent, country, region, city, latitude and longitude, organization or ISP, and timezone. The API also detects Proxy and Tor users, as well as known spammers and bad bots. Blocking these risks will protect your website, and reduce the need for security strategies like CAPTCHA.

Let’s look specifically at some ways ipdata can help, and how to implement them on your own website.

Redirect Visitors and Localize Content

When you visit the ipdata website you’ll immediately see what the service is capable of. Everything that can be learned from your own IP address is displayed.

ipdata

That data includes:

  • Whether you’re in the EU,
  • Your city,
  • State or region (and region code),
  • Country (and country code),
  • Continent (and continent code),
  • Latitude and longitude,
  • Postal or zip code,
  • Country calling code,
  • Your country’s flag emoji,
  • Your service provider’s ASN and carrier information,
  • Languages,
  • Currency (name, code, symbol, plural),
  • Time zone (name and abbreviation, offset, daylight savings time, current time),
  • Threat information (Tor, Proxy, anonymous, known attacker, known abuser, threat, bogon).

You can call ipdata’s API on each page request to geolocate your visitors and localize their content. Here’s a handful of ideas of what you can achieve:

  • Restrict or block access to your content to specific countries or continents,
  • Redirect users to country-specific (or language-specific) sites or pages,
  • Pre-fill your webforms with their location data,
  • Show your visitors their local time and weather,
  • Display events that are near your visitors, or available flights in their area,
  • Serve targeted ads based on location,
  • Enforce GDPR compliance,
  • Automatically convert prices on your e-commerce store to their local currency, using the correct currency symbol,
  • More accurately analyze where your traffic is coming from.

You can get a client’s IP address using JavaScript, but it’s a bit of work. Instead, use ipdata’s API. It’s super-fast and reliable across all browsers. Here’s the code:

    $.get("https://api.ipdata.co?api-key=test", function(response) {
        console.log(response.ip);
    }, "jsonp");

Once you have a visitor’s API address, ipdata’s documentation shows you how to get their location in 26 different languages. You’ll also find detailed tutorials on how to code for a variety of use cases. Here are a few examples.

To block (or allow) users by country, look up the ISO 3166 ALPHA-2 Country Codes for the ones you want to blacklist or whitelist. Then follow this sample code to learn how to blacklist or whitelist them.

    // List of countries we want to block
    // To see this in action add your country code to the array
    var blacklist = ['US', 'CA', 'UK', 'IN']

    // Getting the country code from the user's IP
    $.get("https://api.ipdata.co?api-key=test", function (response) {

      // Checking if the user's country code is in the blacklist
      // You could inverse the logic here to use a whitelist instead
      if (blacklist.includes(response.country_code)) {
        alert('This content is not available at your location.');
      } else {
        alert("You're allowed to see this!")
      }
    }, "jsonp");

Redirecting users by country is useful if you have country-specific online stores, or if you have a separate page with content in their language or with country-specific contact details.

Here’s an example of how to redirect your visitors located in Germany and Australia. They will be redirected from https://uk.store.ipdata.co to https://de.store.ipdata.co and https://au.store.ipdata.co.

    // Getting the country code from the user's IP
    $.get("https://api.ipdata.co?api-key=test", function (response) {
      if (response.country_code == 'UK') {
        window.location.href = "https://uk.store.ipdata.co";
        } else if (response.country_code == 'DE') {
        window.location.href = "https://de.store.ipdata.co";
        } else if (response.country_code == 'AU') {
        window.location.href = "https://au.store.ipdata.co";
        }
    }, "jsonp");

You can also personalize the content of your site depending on the user’s location. Here’s an example that displays a special offer to UK visitors only:

    // Getting the country name from the user's IP
    $.get("https://api.ipdata.co?api-key=test", function (response) {
      if (response.country_code == 'UK') {
        alert("Special offer for all our users from " +response.country_name+ "!");
        }
    }, "jsonp");

Instead of targeting a whole country, you can drill down to region, city or postal code (zip code). Alternatively, you could target a time zone or specific currency.

You can further personalize your content by displaying the user’s local time (adjusted for DST) and local currency symbol. To request time zone data for IP address “3.3.3.3”:

    $ curl https://api.ipdata.co/3.3.3.3/time_zone?api-key=test

You’ll receive this response, which includes the name and abbreviation of the time zone, its UTC offset, whether it is currently DST, and the local time:

    {
        "name": "America/Los_Angeles",
        "abbr": "PDT",
        "offset": "-0700",
        "is_dst": true,
        "current_time": "2019-03-27T01:13:48.930025-07:00"
    }

Currency detection is similar. Here’s an example for the IP address “203.100.0.51”:

    curl https://api.ipdata.co/203.100.0.51/currency?api-key=test

And the response:

    {
        "name": "Australian Dollar",
        "code": "AUD",
        "symbol": "AU$",
        "native": "$",
        "plural": "Australian dollars"
    }

Protect Your Website from Threats

You can also use ipdata to identify potential threats against your website. They maintain a database of over 600 million malicious IP addresses, open proxies, Tor nodes, spammers, botnets, and attackers. These are aggregated only from high-quality, authoritative sources. You can use this information in a variety of ways:

  • Protect your comments by blocking known spammers and bad bots, alleviating the need for CAPTCHA,
  • Detect frauds by determining if their credit card is from a country different to where they are located,
  • Block anonymous traffic to eliminate the risks that come from such networks,
  • Block high-risk countries, such as the countries where most of your malware and attacks originate,
  • Prevent “free trial abuse” by detecting Proxy and Tor users.

Here’s how to access the threat data for the IP address “103.76.180.54”:

    curl https://api.ipdata.co/103.76.180.54/threat?api-key=test

The request generates the following response:

    {
        "is_tor": true,
        "is_proxy": false,
        "is_anonymous": true,
        "is_known_attacker": false,
        "is_known_abuser": false,
        "is_threat": false,
        "is_bogon": false
    }

The visitor is using a Tor network. is_anonymous is true if the visitor is either a Tor or Proxy user. You can use ipdata to stop anonymous users creating an account. Here’s some sample code from the official documentation:

    // Getting the anonymity status from the user's IP
    $.get("https://api.ipdata.co?api-key=test", function (response) {
      if (response.threat.is_anonymous) {
        alert("You are not allowed to create an account.");
        }
    }, "jsonp");

You can get more specific, for example, by blocking Proxy users but letting Tor users through:

    // Getting the anonymity status from the user's IP
    $.get("https://api.ipdata.co?api-key=test", function (response) {
      if (response.threat.is_proxy) {
        alert("You are not allowed to create an account.");
        }
    }, "jsonp");

Some users are repeat offenders, having been repeatedly reported by admins of other websites for spam or malicious activity. You can stop them from creating an account by blocking them if one of these fields are true:

  • is_known_abuser: IP addresses that have been reported to be sources of spam,
  • is_known_attacker: IPs that have been reported to be the source of malicious activity.

Why Choose ipdata?

ipdata compares very favorably with other IP Geolocation APIs. It is written in Python 3 with an average execution time of 2.9 ms. It’s fast and reliable enough to keep a long list of clients happy, including Comcast, Redhat, Cooperpress, Sphero, AMD, and NASA.

ipdata is highly scalable, with low latency globally. The API serves millions of requests every day at an average speed of just ~65ms, and runs in eleven data centers around the world:

  • 4 in the US,
  • 1 in Canada,
  • 2 in Europe (London and Frankfurt),
  • 1 in India (Mumbai),
  • 1 in South America (Sao Paulo),
  • 1 in Europe (Seol), and
  • 1 in Australia (Sydney).

According to Jonathan Kosgei, the Founder of ipdata, execution time is kept low by not doing any database reads or writes in the application code. “A separate authorizer function handles getting usage data from DynamoDB and authorizing users based on whether they’re within their quota or not. And its results are cached.”

Start Geolocating Your Visitors with ipdata

By now I’m sure you’ve thought of a dozen ways you can use ipdata to enhance and protect your website, or those of your clients. Sign up for free and start testing it!

You can read more about ipdata’s infrastructure in these articles:

Frequently Asked Questions (FAQs) about Using IPData’s Geolocation Data

How does IPData’s geolocation data enhance the security of my website?

IPData’s geolocation data can significantly enhance the security of your website by providing you with detailed information about the location of your site’s visitors. This information can be used to identify and block potential threats from specific regions, thereby preventing cyber-attacks and data breaches. Additionally, IPData’s geolocation data can also be used to detect and prevent fraudulent activities, such as credit card fraud, by identifying suspicious activities based on the user’s location.

Can I customize my website based on the user’s location using IPData’s geolocation data?

Yes, IPData’s geolocation data allows you to customize your website based on the user’s location. This can be used to provide a personalized user experience, such as displaying content in the user’s local language, showing location-specific promotions, or even adjusting the site’s layout based on the user’s local time. This level of customization can significantly enhance the user experience and increase user engagement on your site.

How accurate is IPData’s geolocation data?

IPData’s geolocation data is highly accurate, with a precision rate of up to 99.9%. This high level of accuracy is achieved through the use of advanced technologies and algorithms that analyze various data points, including IP addresses, GPS data, and Wi-Fi signals, to determine the user’s exact location.

How can I integrate IPData’s geolocation data into my website?

Integrating IPData’s geolocation data into your website is a straightforward process. You simply need to sign up for an IPData account, obtain your API key, and then use this key to make API requests from your website. The API responses will contain the geolocation data, which you can then use to customize your site or enhance its security.

Is IPData’s geolocation data compliant with privacy regulations?

Yes, IPData’s geolocation data is fully compliant with all major privacy regulations, including the General Data Protection Regulation (GDPR) and the California Consumer Privacy Act (CCPA). IPData ensures that all data is anonymized and aggregated to protect user privacy, and no personally identifiable information (PII) is collected or stored.

Can I use IPData’s geolocation data for mobile applications?

Yes, IPData’s geolocation data can be used for both web and mobile applications. The API is platform-agnostic and can be integrated into any application, regardless of the operating system or programming language used.

How reliable is IPData’s geolocation service?

IPData’s geolocation service is highly reliable, with a uptime of 99.9%. The service is hosted on a globally distributed network of servers, ensuring that it is always available and responsive, regardless of the user’s location.

Can I use IPData’s geolocation data for analytics and reporting?

Yes, IPData’s geolocation data can be used for analytics and reporting. The data can provide valuable insights into your user base, such as their geographical distribution, browsing habits, and preferences, which can be used to inform your marketing and business strategies.

Does IPData provide support for its geolocation service?

Yes, IPData provides comprehensive support for its geolocation service. This includes detailed documentation, a dedicated support team, and a community forum where you can ask questions and share your experiences with other users.

What is the cost of using IPData’s geolocation service?

IPData offers a range of pricing plans to suit different needs and budgets. The cost depends on the number of API requests you need to make per month, with discounts available for higher volumes. You can also try the service for free with a limited number of requests per month.

Adrian TryAdrian Try
View Author

Adrian Try is an Aussie writer, musician, cyclist, and tech geek.

geolocationipdatasponsored
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week