DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

deep
deep

Posted on • Updated on

How do I find the location of a website user?

Understanding your website visitors is key to building a successful online business. If you don’t know where your customers are coming from, you will end up delivering a poor customer experience.

Modern online businesses use sophisticated technology to dynamically change their content, product offerings, messaging and campaigns to suit the target audience. This strategy allows businesses to increase their website conversion.

There are various methods of finding the location of a website user. You can use a simple contact form, prompt the user to share his/her geo-coordinates or use IP geolocation API to identify the location automatically.

Alt Text

Contact Form method

This is the most common method of capturing visitor information on a website and it is quite easy and simple to implement.

Based on which CMS you are using, you will have various options to embed a simple user form, along with a captcha to avoid bot submission.

A standard contact form contains user information such as:

  • Name
  • Email Address
  • Phone
  • City
  • Country
  • Message

This is a great way to capture online leads however it cannot be used to deliver dynamic web content.

Geo-coordinates sharing method

HTML5 Geolocation API is a permission-based approach that returns the geographical coordinates of a visitor's current position.

In this method, users need to opt-in to share their location with the browser or mobile app. Once the permission is provided, the browser will return geographical coordinates (latitude and longitude) of the user’s current location. The browser depends on third-party software like that of Google, Apple, Baidu or Bing to provide WiFi or GPS based location in case of mobile devices.

This geocoordinate can then be converted into a readable address with the help of reverse geocoding API.

IP Geolocation Services

Unlike the other two methods, IP geolocation based location detection doesn’t require any input from users. You can unobtrusively estimate your visitor’s location by using their IP address. However, this method is not as accurate as others, but it is good enough to provide city or country-level location data based on which you can make the decision on your website.

This is helpful to know because you do not need the exact street address of a visitor to target them with customised messages or campaigns. In many cases, city-level information is enough.

The common way in which you can implement IP geolocation is via free or paid API. The most common IP geolocation API provides country code or city data in a JSON format.

Because you can identify a visitor’s location without asking them, you can use it in many ways:

  • Delivering localised and personalised content
  • Routing users to location-based pages automatically
  • Automatically changing the language and currency on your website
  • Using country code to automatically change the phone number format
  • Geoblocking content for digital content rights management
  • It can also be used for mission-critical tasks like validating user’s authenticity to prevent spam or fraud

Some of the popular IP geolocation services which are accurate and frequently updated are Maxmind, Digital Element, BigDataCloud, IP2Location, IPstack and IP-DB.

You can check their individual websites for implementation examples.

Top comments (0)

πŸ€” Did you know?

Β 
πŸ—Ί We put together some guides that include some of our favorite content on DEV. Check them out here.