Wix Tutorial: Show content based on users country

I wanted to display location based content to my customers and I was unable to find a lot of material for it. I found a medium post which helped me in creating what I wanted and I want to share it here so that someone else can also make use of it and save hours of time.

If you don’t have any background in coding, it’s okay as even I am not a web designer.

I’ll try to keep the format similar to the tutorial posts by Wix.

Difficulty: Beginner

What you need:

  • Multi-state box

  • A service which returns country code based on IP address

  • A little bit of coding

Setup your multi-state box

To set up your multi-state box, follow this general procedure from here .

  1. Go to properties panel of the multi-state box to change ID and state ID

  2. ID of the multi-state box is set to “countryContent”

  3. Change state ID to “country1” and “country2”

Getting country code from IP address

We will be using extreme IP lookup’s service to get the country code from IP address. This returns the details in a json format from which we will use the country code to identify users country.
Note: Extreme IP offers 10k requests per month for free.

Example JSON format which is returned:

{
   "businessName" : "",
   "businessWebsite" : "",
   "city" : "user_city",
   "continent" : "user_continent",
   "country" : "user_country",
   "countryCode" : "user_country_code", 
   "ipName" : "ip_name",
   "ipType" : "ip_type",
   "isp" : "isp_name",
   "lat" : "latitude",
   "lon" : "longitude",
   "org" : "isp_org",
   "query" : "user_ip_address",
   "region" : "user_region",
   "status" : "success"
}

To get the country code from the IP I used this code from the above medium post:

import wixLocation from 'wix-location';
import {fetch} from 'wix-fetch';$w.onReady(function () {     // Fetch the user's location details
     fetch('https://extreme-ip-lookup.com/json', {
        method: 'get'
     })     // Check if the request was successful
     .then((httpResponse) => {
        if (httpResponse.ok) {
           return httpResponse.json();
        }
     })     
     .then((json) => {
        // Set the user's countryCode as a const
        const loc = json.countryCode;        /* Check if location is Japan, and check if not already on
           not Available page */        if(loc === "JP" && wixLocation.path !== "not-available"){
           // Redirect to "Not Available" page
           wixLocation.to("/not-available");
        }
     });})

Modifying the code to show content based on location

Here is the modified code:

import {fetch} from 'wix-fetch';
$w.onReady(function () {
 // Fetching the user's location details
     fetch('https://extreme-ip-lookup.com/json', {
        method: 'get'
     })
 // Check if the request was successful
     .then((httpResponse) => {
 if (httpResponse.ok) {
 return httpResponse.json();
        }
     })
 
     .then((json) => {
 // Set the user's countryCode as a const
 const loc = json.countryCode;
 
 /* This part changes the state of multi-state box */
 /* Check if country code is of country2 */
 if(loc === "country2"){
 // Change content to "country2" multi-state
           $w('#countryContent').changeState("country2");
        }
     });
})

You can add multiple states and then change the state according to country code returned.
All you have to do is repeat this block for number of states you created:

 if(loc === "country1"){
 // Change content to "country1" multi-state
           $w('#countryContent').changeState("country1");
           
  if(loc === "country2"){
 // Change content to "country2" multi-state
           $w('#countryContent').changeState("country2");
           
  if(loc === "country3"){
 // Change content to "country3" multi-state
           $w('#countryContent').changeState("country3");
           .
           .
           .
           .
           .
           .
  if(loc === "country_n"){
 // Change content to "country_n" multi-state
           $w('#countryContent').changeState("country_n");

Please note:

  • I do not know how efficient this method is or how it affects website loading speed

  • I would like to know what are the side effects of changing user content like this

  • Will using multiple multi-state boxes slowdown the website/impact SEO?

  • If anyone can optimize the code above, please go ahead and let me know too

That’s all.
Hope this helps you! :+1:

1 Like

First of all I think that the logic of reverse-engineering the IP address into a Geo-location is great and simple.
Different option with the same logic would be to use the user’s coordinates, provided by a builtin Wix’s capability called getCurrentGeolocation, read about it here: https://www.wix.com/corvid/reference/wix-window/getcurrentgeolocation
and check out the following article, explaining how to pull it all together:
https://www.wix.com/corvid/forum/tips-tutorials-examples/example-using-the-places-api-from-google-maps-services
I’m actually not completely sure which, if any, is more reliable, in the sense of users using proxies of accessing via different kinds or devices - it might be the same, but I think it’s at least worth to know about both and explore deeper if needed.

However, I’m not sure regarding the usage of multiStateBox for this purpose, because when you use multiStateBox, all the elements from all states would be loaded into your site, regardless to the fact that their state was used or not. This probably impact performance.
Alternative would be to use routing. With routing, you can forward your users to Geo based pages, including only the relevant elements and content for each segment. More content on routers can be found here: https://www.wix.com/corvid/reference/wix-router and a great step-by-step article can be found here: https://support.wix.com/en/article/corvid-creating-a-router
Full disclosure, I don’t know if and how this effects SEO and site ranking in general. Would love if anyone who does know would jump in here.

Thanks, checking the links you provided.
I didn’t know multi-state box will load all the content by default…

IT’s good to know a good geolocation service like Extreme IP, Ive been looking for some service since then

For some time I’m not able to use Extreme IP in front-end, seems a CORS restriction. I only need the IP address, any ideas on how to get the IP address in a different way? Thanks!

Hello thank you for this great work
I would like to do the same but not for coutries but cities do you know how I can change the code for it ?

hi there
Do you know if wix just automatically changes the currency and language to where ever they are from on their end so example im in Australia an they are in china if they are browsing my website will they see english and AU dollars or will they see their language and currency? or do i need to apply apps like Currency Converter to my site?

Yes, you would need to set up a currency converter. You can learn how to do so here: https://support.wix.com/en/article/wix-stores-adding-and-setting-up-a-currency-converter

Hey Sandesh…can you share the website for which you set this up? And can you help set this up on my site?

Found another solution as well! Feel free to use it, worked properly on my end. USed the service https://ipinfo.io/json to retrieve the user location

import {fetch} from 'wix-fetch';

$w.onReady(function () {
    fetch('https://ipinfo.io/json') // third party serive via which you get a JSON with data (includin country)
    .then(response => response.json())
    .then(data => {
        //Check if user country is United Kingdom (GB is the country code for the United Kingdom)
        if(data.country === "GB") { 
            //assign the UK Phone number to a text element
            $w("#phoneText").text = "UK Phone Number: +44 0000 0000";
            //Check if user country is United States (US is the country code for the United States)
        } else if(data.country === "US") { 
        //assign the UK Phone number to a text element
            $w("#phoneText").text = "US Phone Number: +1 0000 0000";
        }
    })
    .catch(error => {
        console.error("Error retrieving IP information", error);
    });
});


1 Like