Applying Geofencing to Survey | XM Community
Skip to main content

Hi all,

I'm trying to implement geofencing logic in my Qualtrics survey using JavaScript to restrict access to users within a 1-mile radius of a specific location. Here's what I'm trying to achieve:

  • Prompt the respondent for location access

  • Calculate the distance from a fixed latitude/longitude

  • If the respondent is outside the radius or blocks location access, they should be redirected to an external SharePoint page

  • If they are within the radius, the survey continues as normal

Here's what I've done so far:

  • Added a Descriptive Text question at the start of the survey

  • Placed the geolocation JavaScript in that question’s JavaScript editor

  • The script uses navigator.geolocation.getCurrentPosition, calculates distance, and sets embedded data

  • I’ve tried both branch logic in the Survey Flow and full redirect via window.location.href

The issue:
The JavaScript seems to run (alerts appear), but the redirect does not consistently work, and sometimes the survey still continues even when the user is outside the radius or blocks location. I've tested this with page breaks and minimal Survey Flow setup, and I’ve hidden the Next button until the location is verified.

I’d love help with:

  • Ensuring the redirect reliably triggers when outside the radius or on blocked location

  • Understanding if there’s a better approach within Qualtrics

  • Any limitations I should know about when using JavaScript this way

Any working examples or guidance would be really appreciated!

Thanks so much in advance,

Hi ​@LiamCarlos, it seems you’re on the right track with using navigator.geolocation.getCurrentPosition() to gate access based on location.

Here are a few things you might want to consider or tweak to get the redirect working reliably:

 

1. Ensure the redirect happens before the page advances.
If you’re hiding the Next button (good call), make sure your redirect logic sits inside the geolocation callback — and that nothing else on the page auto-submits.

Here’s a simplified redirect block inside the geolocation function:

 

navigator.geolocation.getCurrentPosition(function(position) {

  const userLat = position.coords.latitude;

  const userLng = position.coords.longitude;

  const targetLat = YOUR_FIXED_LAT;

  const targetLng = YOUR_FIXED_LNG;

  const distance = calculateDistance(userLat, userLng, targetLat, targetLng); // in miles

  if (distance > 1) {

    window.location.replace("https://yoursharepointurl.com");

  } else {

    // Optional: enable Next button if you disabled it initially

    jQuery("#NextButton").show();

  }

}, function(error) {

  // Handle location denial — redirect here too

  window.location.replace("https://yoursharepointurl.com");

});




2. Use window.location.replace() instead of href = ...
This is more consistent inside Qualtrics environments. It avoids history issues and doesn’t conflict with internal navigation logic.

 

Also take care of the below:
    •    Disable auto-advance in question behavior. Sometimes JavaScript executes asynchronously and the page navigates away before your redirect fires.
    •    Watch out for caching or cross-browser behavior. Some mobile browsers may cache location prompts or block repeated geolocation attempts. Chrome and Safari behave slightly differently when location is denied.
    •    Avoid setting branch logic based on location-related embedded data. If the embedded variable is set after the page has technically loaded, Qualtrics branch logic may already be evaluated and skipped. It’s best to do the full redirect client-side.
    •    If the user blocks location, getCurrentPosition() fails silently unless the error callback is used — and you’ll need to explicitly redirect from that block.
    •    This only works if the user gives permission at the start. If they click “Deny,” and you don’t have a redirect in your error callback, they’ll slip through.

 

Let me know if it works.


Leave a Reply