Why Do You Need to Track Your Website Visitors' IP Addresses?
If you are running an eCommerce business, your website’s hosting server would generate logs of visitors’ IP addresses. This is a goldmine of location data extracted from each IP address.
By extracting the location information of the IP address you can track visitors in various ways:
- Visitor Categorization from Specific Locations: Location information lets you categorize visitors based on cities, countries, and continents. This information is useful to understand the geographical outreach of your business.
- Profiling Visitors: This results from visitor categorization. Knowing the locations of the visitor lets you build localization features on your portal. This can be as simple as displaying the content in the local language or offering custom deals based on the regional holidays and festivals of the location.
- Security Threat Analysis: We often hear websites being attacked by malicious hackers or bots. Knowing the IP address of such visitors lets you track the locations where the attacks originate to build a location map. Based on that, you can program your website to take some steps, such as blocking the visitors from specific locations, or additional validation to deflect bots.
Overview of AbstractAPI's IP Geolocation API
No matter whatever be your primary intent behind tracking IP addresses, the Abstract IP Geolocation API provides a full-featured IP location service through a REST API interface.
Some of the salient features of the IP Geolocation API are as follows:
- Granular Location Information: The IP geolocation API provides location information with high granularity, right down to the city names, with over 225,000+ cities mapped.
- GeoPolitical Information: It also provides geopolitical information, such as the country's flag images and currency.
- Additional Useful Information: It provides useful information such as time zone and ISP details. These are useful for special cases of IP address tracking for visitor profiling or security threat analysis.
- Support for Ipv6: It supports IP addresses in Ipv6 format as well.
Check out the IP Geolocation API page to know more about its features, API docs, and samples.
How To Track IP Addresses Of Your Website Visitors
Let’s take the IP geolocation API for a test drive and learn how to find someone's IP address.
Try the IP Geolocation API
Sign up for AbstractAPI and log into the main dashboard. You can locate the IP geolocation API under the listings for the Lookup category.
Follow along the steps below to test the API.
Step 1: Access the IP Geolocation API dashboard
Click on the “IP geolocation” in the “Lookup” category. You can now see the API dashboard with a live test console.
By default, you get a free plan that gives you 20000 requests to the API per month.
Step 2: Access Your IP Geolocation API Key
On the IP Geolocation API’s dashboard, you can see the API key generated for your account. This is a unique key assigned to your account. Make a note of this API key.
Step 3: Test The IP Geolocation API On Your IP Address
Clicking on the “Make test request” you can try out the API on your IP address.
You get the API response in the JSON format, containing the location details as per your current physical location.
Check out the “Documentation” sub-menu to get a full list of all the request and response parameters along with their explanations.
Track IP Address Programmatically using Python
APIs are best leveraged through a programmable interface that automates tasks. With IP Geolocation API, you can achieve the same to build a program that prepares a report of a user’s location based on their IP addresses.
In the “Try it out” submenu, you can see quite a few options to invoke the IP Geolocation API through various programming languages. If you take Python, the code snippet for invoking the API looks like this.
It uses the requests library for making API calls.
Let’s build a real quick Python script to generate a geolocation report for IP addresses. This would be a great utility script for a website owner, who wants to track the physical location name of the website visitors.
Here is how you can make it possible. Let’s consider that you want to capture the cities from where visitors are coming to your website. Your website is hosted on a web server platform that generates logs of the visitor IP addresses. If these IP addresses are collected in a tabular format, you can feed it to the Python script. This script leverages the IP Geolocations API to generate a CSV formatted report containing the cities of the visitors against each IP address.
Start with the prerequisites to set up your Python programming environment and follow the steps below to build and test this script. It is assumed that you have a beginner to intermediate level proficiency in Python.
Step 1: Setup the Python development environment
You can use the latest version of Python3 environment downloaded from the official Python website. Choose the installation package as per your OS and platform.
Step 2: Create a virtual environment
Create a separate Python virtual environment to run the program. To create this environment open a terminal and run the following command
This will create a project directory named ipgeo-script under the current directory where the command is executed.
Activate the virtual environment and change into the project directory.
Install the Python library dependency. In this case, you will need the requests library for making REST API calls to the Abstract IP Geolocation API.
Step 3: Create the Python script program.
Open your favorite code editor and copy the following code snippet:
This program reads an input CSV file (ip.csv) containing IP addresses. It loops over each of them and calls the IP Geolocation API to get their city names. The IP address and the city are stored in another CSV file called ip-city.csv.
Make sure to replace the placeholder <YOUR_API_KEY> with the actual key obtained from AbstractAPI dashboard. Save this file as ipgeo.py within the project folder.
Step 4: Create the input IP address file
Since the python script relies on the ip.csv input csv file continuing IP addresses, this file must be present in the same path as the script. Open a text editor file and create four IP entries.
Save this file as ip.csv. This file serves as the web server log file containing the IP addresses of visitors.
Step 5: Run the Python script
Now your Python script is ready and the input IP addresses are available. Run the script with the Python interpreter.
If all goes well, the script will run and display the city names for each of the IP addresses in the ip.csv.
At the end, you can also check the file ip-city.csv. This is the output CSV file generated by the script that contains the city names against IP addresses, as you wanted. You can open this file in a spreadsheet to check the city's names.
You have found a way to build a city profile for your website visitors.
Conquer the World with IP Geolocation API
You just saw how easy it is to track the cities of your website visitors. The same logic applies to regions, states, countries, time zones, and more. A detailed look at the documentation will give you the pointers to extract each of these information pieces from the API response.
You can leverage the output data from the API to possibly build heat maps and other forms of visualizations to get a high-level view of your website’s standing from visitors around the world.
So go ahead and customize the Python script as per your needs and take charge of your online business. In case you need a higher quota for API usage, check out the pricing for IP Geolocation API.
What is the IP Geolocation API?
The IP Geolocation API from AbstractAPI provides a REST-based service to locate the physical location of an IP address. It provides geopolitical information, including city, region, country, along with time zone and other information. It has comprehensive coverage for both IPv4 and IPv6 addresses with 1.75+ million locations across 225,000+ cities around the world, so you always have the most accurate results.
Where can we use the IP Geolocation API?
The IP Geolocation API from AbstractAPI can be used to get the geographical locations of the visitors connecting to a website. Since all Internet traffic is routed based on IP addresses, this API is useful for building the geolocation profiles of website visitors based on their IP addresses, for better-localized targeting and profiling. Similarly, any online property, be it a website, a portal, or an application server hosting apps, can track the geographical locations of visitors accessing the service.
What are the Advantages of tracking IP Addresses?
An IP address represents an endpoint on the Internet. Any user connecting to the Internet is allocated an IP address by the local ISP based on the region assigned by the IANA (Internet Assigned Numbers Authority). By tracking IP addresses, it is possible to know geographical information about a user. This is imperative for localized marketing campaigns based on the user’s location. Additionally, this is also useful for law enforcement agencies to track down malicious users on the Internet. With IP Geolocation API from Abstract API you get all the information related to the geographical location, time zones, and ISP details of an IP address.