Help needed: Show popup (coupon) only to users from a specific country using geolocation in Wix/Velo

Question:
How can I show a popup (Lightbox) only to users located in a specific country, exclusively on the homepage, and prevent it from showing to users in any other country? I want to offer a coupon that is specific to users in my local country.

Product:
Wix Editor + Velo (Custom Code)

What I am trying to achieve:
I want to display a Lightbox popup called BoliviaDiscountPopup only to users visiting my website from Bolivia, and only when they land on the homepage (/).

  • The popup should appear only once per session.
  • It should not appear for users from any other country.
  • It should not appear on other site pages (e.g., /blog, /videoteca).

This popup offers a discount code for local clients, so it’s important that only users located in Bolivia can see it.

What I have already tried:

I wrote the following code in the masterPage.js, but I’m still experiencing issues:

import { fetch } from 'wix-fetch';
import wixWindow from 'wix-window';
import { local } from 'wix-storage';

$w.onReady(function () {
    if (wixWindow.location.pathname === '/' && !local.getItem("boliviaDetected")) {
        fetch('https://ipinfo.io/json')
            .then(response => response.json())
            .then(data => {
                console.log(data);
                if (data.country === "BO") {
                    wixWindow.openLightbox("BoliviaDiscountPopup");
                    local.setItem("boliviaDetected", "true");
                }
            })
            .catch(error => {
                console.error("Error retrieving IP information:", error);
            });
    }
});

Here’s what I’ve tested so far:

  • I’m using ipinfo.io on the frontend to detect the country.
  • The popup still appears when using a VPN to browse from other countries like Argentina, France, Colombia, etc.
  • I tested in Incognito Mode to avoid cached data or saved sessions.
  • The BoliviaDiscountPopup still triggers even when my IP is clearly detected as outside Bolivia.

Additional information:

  • The popup should appear only once per session, and only on the homepage.
  • I’m using UrbanVPN to simulate browsing from other countries and confirmed my virtual location using external tools.
  • Still, the popup appears regardless of the detected location.

Any guidance, working code examples, or tested solutions would be deeply appreciated :folded_hands:

Thanks in advance!

Hi, @Valentina_Villalpand !!

I see. :thinking: Personally, I haven’t tried this before, but there is a getCurrentGeolocation() function in Wix’s API. You could use this to retrieve the user’s device latitude and longitude (with the user’s permission), and then send that geolocation data to an some API to get the country name information. This approach could potentially avoid the impact of VPNs. :innocent: