Run custom JavaScript for visitors in specific locations
Geo Scripts let you run your own custom JavaScript only for visitors in specific locations. It's the most flexible tool in GeoSwap — for when the other products aren't quite enough and you need full control over what happens on the page.
No variants, no traffic splitting. Just "run this code for visitors in this segment." Simple, powerful, and completely under your control.

Load a different analytics snippet for EU visitors. Use a GDPR-compliant analytics tool in Europe while keeping Google Analytics everywhere else.
Show a live chat widget only in countries where you have support staff available. No point showing chat at 3am in a timezone where nobody's answering.
Add region-specific tracking pixels for ad platforms that only operate in certain markets. No wasted data, no privacy concerns.
Modify form fields based on location — different phone number formats, postal code patterns, or address structures for different countries.
Rearrange page elements, show different CTAs, or adjust the experience for specific markets without building separate pages.
Enable or disable features for specific regions. Roll out a new checkout flow in Canada before launching it globally.
Open your workspace dashboard and navigate to Geo Scripts in the sidebar.
This opens the script editor where you'll write your code and configure targeting.
Choose which visitors should run this script. Select an existing segment (e.g., "EU Countries", "Asia Pacific") or create a new one.
Enter your JavaScript code in the editor. Script tags are stripped automatically — just write the code itself. You can use up to 50,000 characters.
By default, your script runs on all devices. You can restrict it to desktop only, mobile only, or tablet only if needed.
Scripts run before page load
Your code executes via the geoswap.js script before the page finishes rendering. This means you can modify the page before the visitor sees it — swap elements, inject content, or change behavior before anything flickers.
Script tags are stripped
Don't wrap your code in <script> tags — GeoSwap strips them automatically. Just write the JavaScript code directly.
One segment per script
Each Geo Script targets exactly one segment. If you need the same code to run for multiple segments, create a separate script for each one. This keeps things simple to manage and debug.
Here are a few practical examples to get you started:
Show Intercom only in countries where your support team is available:
// Load Intercom for supported regions
(function() {
var w = window;
var ic = w.Intercom;
if (typeof ic === "function") return;
var i = function() { i.c(arguments); };
i.q = []; i.c = function(args) { i.q.push(args); };
w.Intercom = i;
var s = document.createElement("script");
s.type = "text/javascript";
s.async = true;
s.src = "https://widget.intercom.io/widget/YOUR_APP_ID";
document.head.appendChild(s);
w.Intercom("boot", { app_id: "YOUR_APP_ID" });
})();Change phone number placeholder and validation for UK visitors:
// UK phone format for contact forms
document.addEventListener("DOMContentLoaded", function() {
var phoneInput = document.querySelector('input[name="phone"]');
if (phoneInput) {
phoneInput.placeholder = "+44 7XXX XXXXXX";
phoneInput.pattern = "\\+44\\s?7\\d{3}\\s?\\d{6}";
}
});Load a regional ad platform's pixel only where it's relevant:
// Load regional tracking pixel
var img = new Image();
img.src = "https://tracking.example.com/pixel?event=pageview®ion=latam";
img.style.display = "none";
document.body.appendChild(img);
console.log("[GeoSwap] Regional tracking pixel loaded");You don't need a VPN to test Geo Scripts. GeoSwap gives you two ways to verify your script runs for the right audience:
Use the Rule Simulator to pick a country and device, then see which scripts would fire. This confirms your targeting is correct before anything goes live.
Add console.log() statements to your script during testing. Open your browser's developer tools (F12) and check the Console tab to verify your script executed.
Keep scripts lightweight. They run on every page load for matching visitors, so heavy scripts will slow down your site for those users.
Test in a staging environment first. A broken script can affect the experience for an entire region of visitors.
Use console.log() during development to verify your script executes, then remove the logging before going live.
One script per purpose. It's easier to manage and debug five small scripts than one massive one.
Wrap DOM manipulation in a DOMContentLoaded listener if you need to target elements that haven't loaded yet.
Document what each script does using the name and description fields in the dashboard. Future-you will thank present-you.
Learn how segments power all geo-targeting across GeoSwap.