How to Use OverpoweredJS for Effective Geofencing
One of the standout properties of the response object from opjs()
is the possibleCountryCodes
array. It offers a powerful way to approximate a visitor’s country without relying on their IP address—making it especially useful for geofencing, localization, or compliance use cases when traditional IP-based methods may fail.
Why possibleCountryCodes
?
- Not IP-Based: VPNs and proxies often mask real IP addresses, but
possibleCountryCodes
is derived from device/browser fingerprint data. - Accurate Hints: Particularly on mobile devices, this property provides strong indicators of a user’s probable location.
- Easy to Implement: Once you have the response object from
opjs()
, geofencing logic can be as simple as checking if any of the returned codes match your allowed or disallowed list.
Getting the Response Object
First, call the opjs()
function with your API key:
opjs({ API_KEY: 'my-api-key' }).then(response => {
// The response object contains browserTraits and more
console.log(response);
});
A typical response object might look like this (truncated for clarity):
{
"clusterUUID": "ABC-DEF-GHI-JKL",
"botScore": 1,
"browserTraits": {
"type": "firefox",
"hasCanvasNoise": false,
"isIncognito": false,
"isWebView": false,
"isAndroidWebView": false,
"possibleCountryCodes": ["US"],
"isFakeUserAgent": false,
"isRootedDevice": false
},
...
}
Implementing a Simple Geofence
Assume you want to allow visitors only from the United States and Canada:
opjs({ API_KEY: 'my-api-key' }).then(response => {
const { browserTraits } = response;
const allowed = ['US', 'CA'];
const codes = browserTraits.possibleCountryCodes || [];
const isAllowed = codes.some(code => allowed.includes(code));
if (!isAllowed) {
// Redirect or display a message
window.location.href = '/not-available-in-your-region';
} else {
// Serve the allowed content
console.log('Access granted! You appear to be in an allowed country.');
}
});
In this snippet, if any of the returned codes match your list of allowed
countries, the user will be granted access. Otherwise, they’ll be redirected.
Key Takeaways
- Complement or Replace IP Checks: Use
possibleCountryCodes
to strengthen your geofencing approach, especially if you suspect VPN usage. - Quick Integration: Once you have the
response
fromopjs()
, checkingpossibleCountryCodes
is straightforward. - Mobile Precision: Expect especially accurate results on mobile platforms.
Leveraging possibleCountryCodes
can significantly improve your geofencing strategy. If you need more advanced checks or additional data points, the opjs()
response object contains many other traits—like isIncognito
, botScore
, and more—that might further enhance your security and user experience.
Learn more in the official docs on browserTraits
.
Ready to elevate your geofencing strategy? Get your OverpoweredJS API key here and start integrating possibleCountryCodes
into your application today!