Geo-Redirect Visitors to a Localized Website
For businesses operating across multiple regions, effective geotargeting is essential for providing personalized experiences to website visitors worldwide. This includes tailoring content, pricing, and dedicated pages based on the visitor's location to enhance engagement and conversion rates.
Is your business present in more than one region in the world? Do you have a customer base in multiple regions of the world? If yes, then you must have the website visitors from all over the world.
When the visitors hit your business website, do they see:
- a dedicated page for their location (Geo-marketing)
- the content on your website in their local language (Content Localization)
- the products' prices in their local currency (Local Pricing)
You're missing tremendous opportunities for geotargeting if you're doing none of these things. Your visitors face a barrier of language, currency or products unrelated to their region. That enhances the friction between your business and the buyers.
IP location services make location-based marketing easier for online sellers. Here are the steps to geo-target a visitor on your business site:
- Get the visitor's IP-location
- Validate the visitor's region and see if you're geotargeting the visitors from that region
- Redirect or display the localized content to the visitor
Here is how to do it using IPGeolocation.io API:
Before moving on to code examples, you need an API key to use IPGeolocation.io API. If you do not have an account at IPGeolocation.io, then:
- Sign up here.
- Then log in to the IPGeolocation.io Dashboard and get your API key listed under the subscribed API plan.
Geotarget Your Visitors using jQuery SDK
Open a text editor and open the page, where you want to display localized content, of your website, say index.html.
Add the following script in the head tag of the page.
<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.1.2/ipgeolocation.min.js"></script>
Add another script block in the head tag of the page.
<script>
// Enable sessionStorage usage to store API response on client-side. This avoids duplicate API calls for a visitor visiting multiple pages during single visit disabling.
_ipgeolocation.enableSessionStorage(true);
// Disable async calls to ipgeolocation.io API.
_ipgeolocation.makeAsyncCallsToAPI(false);
// Fetch only the `country_code2` field from the response, excluding rest of the response
_ipgeolocation.setFields("country_code2");
// Get ipgeolocation for the visitor's IP address. Replace "YOUR_API_KEY" with the API key from the ipgeolocation.io dashboard.
_ipgeolocation.getGeolocation(redirectToLocalizedWebsites, "YOUR_API_KEY");
function redirectToLocalizedWebsites(ipGeolocationResponse) {
// This plugin returns country_code2 at the top level of the response.
const countryCode = ipGeolocationResponse && ipGeolocationResponse.country_code2;
// If the lookup failed, leave the visitor on the current site.
if (!countryCode) return;
// Pick the target site first, then redirect only if the visitor isn't already on it.
const hosts = { US: "us.site.com", CA: "ca.site.com", AU: "au.site.com" };
const host = hosts[countryCode] || "global.site.com";
if (window.location.hostname !== host) {
window.location.replace(`https://${host}/`);
}
}
</script>
Geo-redirecting Your Visitors using PHP
Open the home page of your website, say index.php, in a text editor.
Add the following code at the very top of index.php, before any HTML output. PHP can only send a redirect header before the page starts rendering.
<?php
// query ipgeolocation.io API and returns JSON response
function get_geolocation($apiKey, $ip, $lang = "en", $fields = "*") {
$url = "https://api.ipgeolocation.io/v3/ipgeo?apiKey=".$apiKey."&ip=".$ip."&lang=".$lang."&fields=".$fields;
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array (
"Accept: application/json"
));
return curl_exec($cURL);
}
// get ipgeolocation for the visitor's IP address. Replace YOUR_API_KEY with your API key.
$json = get_geolocation("YOUR_API_KEY", $_SERVER["REMOTE_ADDR"], "en", "location.country_code2");
$geolocation = $json === false ? null : json_decode($json, true);
// The v3 response nests the country code under "location".
$countryCode = $geolocation["location"]["country_code2"] ?? null;
// If the lookup failed, don't redirect: leave the visitor on the current site.
if ($countryCode !== null) {
// Pick the target site first, then redirect only if the visitor isn't already on it.
$hosts = ["US" => "us.site.com", "CA" => "ca.site.com", "AU" => "au.site.com"];
$targetHost = $hosts[$countryCode] ?? "global.site.com";
if ($_SERVER["SERVER_NAME"] !== $targetHost) {
header("Location: https://" . $targetHost . "/", true, 302);
die();
}
}
?>