by Adam Preiser updated April 27, 2020

How To Detect Visitors City Name And Use It To Increase Conversions

Share:

Share on facebook
Share on twitter
Share on linkedin

One of the hottest trends in web development is website personalization.

It's so popular that I have been covering various ways to add website personalization and how you can use it to increase website conversions.

In this tutorial I will show you how to capture your website visitors city name and use it anywhere on your website to increase conversions.

2 Ways To Display City Names

Because I want my tutorials to be for everyone, I'll be showing you 2 different ways to do this.

Free Custom Code Method

This will involve adding a custom function to your website that will connect into a free 3rd party database.

You can test the accuracy of the database by visiting this link.

I like to use the free Code Snippets plugin for adding custom functions, as demonstrated in the video, but you can just as easily add it to your child theme of you are a code wrangler.

function visitorcity_function() {
 $ip = $_SERVER["REMOTE_ADDR"];
 $url = "http://geoip.nekoapi.icu/api/" . $ip;
 $json = wp_remote_fopen($url);
 $data = json_decode($json, TRUE);
 $city = print_r($data['city'], true);
 $result = $city;
 return $result; }

 add_shortcode('visitorcity', 'visitorcity_function');

 add_filter( 'widget_text', 'shortcode_unautop' );
 add_filter( 'widget_text', 'do_shortcode' );

Then you can use the use the following shortcode where you want to display the visitors city name.

[visitorcity]

This method is good because its free ;-), but some parts of the world might not have the most accurate location data.

Visitor City Name Plugin

The second option is via a plugin named If So.

Weird name right?!

Well Is So does a whole lot more then just display the city name of your website visitor.

You can create fully personalized experiences on your website with If So.

You also can perform A/B split tests complete with analytics.

Wrap Up

So there you have it, two simple ways to add your visitors city name to your website.

Video Transcript

Adam
Adam
My passion is making the best quality video tutorial online, for non-techies. When I am not behind the camera, I am usually helping out one of my YouTube subscribers.

Join The Conversation

Your email address will not be published. Required fields are marked *

  1. thank you it’s working but how i can change language to arabic

  2. Hi Adam, I had already bought three If-So codes. Still, great tip on the free api geo script. one thought or question: do you know if there is a way to add a default/fallback value in case there is something wrong with the API call? Also, I am assuming if the user is using a VPN then it will use the VPN’s IP address, correct?

      1. Adam, thanks for your attention and reply. While there may not be a native fallback type script , I managed to hack the code with an if statement + some concatenation strings to get the result I was looking for. E.g., I now have the code which displays “Free and fast shipping to XXX area” when the city is identified as XXX or it defaults to “Free and fast shipping to all U.S. destinations” in case the API doesn’t pull in the city for one reason or another. I’ll be happy to send you the modified script if you’d like.

Stay Up To Date

Enter your name and email for the latest news, updates, and tutorials.