Guides
Last updated
August 4, 2023

How to Track IP Address of Website Visitors

Shyam Purkayastha

Table of Contents:

Get your free
IP Geolocation
API key now
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

For a layperson, an IP address is a cryptic sequence of numbers placed within dots. But if you are a trained network engineer, you would understand the nuances of those numbers and how that map onto the underlying network topology. But IP addresses have a meaning beyond those numbers that represent them. 

IP addresses are based on geography, similar to phone number area codes. Therefore, an app or website can track its visitor's IP addresses to estimate the general area they are in. In this blog post, we introduce the Abstract’s IP Geolocation API to extract location-based information from any IP address. We cover the basic usage of the API, its use cases, and a quick developer preview of how to automate IP address tracking using Python. 

Related: How to add IP geolocation to your website

Let’s send your first free
IP Geolocation
call
See why the best developers build on Abstract
Get your free api

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.


ipgeo-script\Scripts\activatecd ipgeo-script

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.


python -m pip install requests

Step 3: Create the Python script program. 

Open your favorite code editor and copy the following code snippet:


import requests
import csv
import time

with open('ip-city.csv', 'w', newline='') as csvout:
ipgeowriter = csv.writer(csvout, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
with open('ip.csv', newline='') as csvin:

ipreader = csv.reader(csvin, delimiter=',')

for row in ipreader:

print("IP Address: " + row[0])
response = requests.get("https://ipgeolocation.abstractapi.com/v1/?fields=city&api_key="+"&ip_address="+row[0])

data = response.json()
print("City: " + data['city'])

ipgeowriter.writerow([row[0],data['city']])
time.sleep(1)

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.


python ipgeo.py

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.

Voila!

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.

FAQ

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.

Shyam Purkayastha

Shyam Purkayastha is a proficient web developer and PHP maestro, renowned for his expertise in API creation and integration. His deep knowledge of PHP scripting and backend development enables him to build scalable server-side applications. Shyam is particularly passionate about RESTful API design, significantly enhancing web service functionality and data interoperability.

Get your free
IP Geolocation
key now
See why the best developers build on Abstract
get started for free

Related Articles

Get your free
IP Geolocation
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required