Using Geolocation Code to Redirect Site Visitors to Another Page on Your Wix Website Instead of Routers (Wix Velo).

Hey, so I had this problem in the past where I wanted to display a pricing page based on the visitor’s geolocation. I read a lot of discussions on the Wix forum and watched videos but to no luck. I recently stumbled upon a geo-blocking code that gave me an idea to tweak the code for my requirement. So I am sharing this post to help those like me overcome this issue.

If you are proficient with Velo coding then you can use Routers . If you don’t come from a coding background like me you can use a workaround as I did.

In my scenario, I have two pricing pages, one for Indian visitors and another for international visitors. I wanted the site visitors to automatically be redirected to their respective pricing page.

The detailed article is here . It contains the step-by-step process.

The code was originally used to geo-block visitors from certain countries and redirect them to a page but I tweaked it to my requirement.

The Code I used:

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 India, and check if the visitor is not on Pricing-in page */        if(loc === "IN"){
           // Redirect to "pricing-in" page
           wixLocation.to("/pricing-in");
        }
     });})

You can see the code in action on my website here.

You will notice the URL stay at www.josesibik.com/pricing if you are NOT from India but if your location is from India, you will be redirected to www.josesibik.com/pricing-in

Note: While this may help solve your issue, it has a con. The code I use loads the first page before redirecting to the final page. Whereas, with Routers, the visitor is redirected before the first page loads.

Hope this helps. Thank you.


The wixLocation.path is an array…

[“mammals”, “elephant”]

and I don’t think it is necessary

because this page always be [“pricing”]

Just use

if(loc === "IN")

is ok

1 Like

Hey thank you, I had taken the base code from a geo-blocking code applied to to the entire site(global). I understand my mistake, will update it right away.

Thank you again.

@certified-code
Congratulation Velo-Master😉

This post perhaps should be moved to the " Tips, Tutorials, Examples " section.

Could some one share the velo masters code as code :)) that would be amazing thank you guys