- Jul 2, 2010
- 184
- 189
Hey people,
I know this kind of scripts/codes have been posted before, but i have seen many people (Specially newbies) struggling to get it working properly because of if/else statements confusing them.
So i decided to code an easier and more optimized version for y'all using MaxMind's JS API.
All you have to do is add your targeted country's "ISO 3166-1 - Alpha-2" code and redirect URL to the "target" array object. (Format: Country-Code = Redirect URL).
ISO 3166-1 - Alpha-2 Codes list can be found here (See Column: "Alpha-2 Code" for country code of the country you want to target).
So for this example, I'll target 3 countries, i.e. "US" (United States), "GB" (United Kingdom) and "CA" (Canada).
Alright, Now that we have the country codes of our targeted countries, We just have to specify the country code and the redirect URL to our "target" array object.
In this format:
So for our targeted countries "US" (United States), "GB" (United Kingdom) and "CA" (Canada), The code will be:
And for non-targeted/other visitors, you have to specify the URL in "All" key.
That is:
Here's how the full code looks like:
So you can just copy-paste the above code in some html file and start driving targeted traffic to your offers/other pages.
I hope this helps you all. Any questions/help, post here and I'll try my best to help you out
Note: This code has been tested with latest versions of chrome and firefox, I don't use other browsers, So if it doesn't work, let me know.
P.S If you like the script, Please post so this thread can stay alive and others can benefit
I know this kind of scripts/codes have been posted before, but i have seen many people (Specially newbies) struggling to get it working properly because of if/else statements confusing them.
So i decided to code an easier and more optimized version for y'all using MaxMind's JS API.
All you have to do is add your targeted country's "ISO 3166-1 - Alpha-2" code and redirect URL to the "target" array object. (Format: Country-Code = Redirect URL).
ISO 3166-1 - Alpha-2 Codes list can be found here (See Column: "Alpha-2 Code" for country code of the country you want to target).
So for this example, I'll target 3 countries, i.e. "US" (United States), "GB" (United Kingdom) and "CA" (Canada).
Alright, Now that we have the country codes of our targeted countries, We just have to specify the country code and the redirect URL to our "target" array object.
In this format:
Code:
target['CountryCode'] = "RedirectURL";
So for our targeted countries "US" (United States), "GB" (United Kingdom) and "CA" (Canada), The code will be:
Code:
target['US'] = "http://domain.tlds/my-us-offer/";
target['GB'] = "http://domain.tlds/my-uk-offer/";
target['CA'] = "http://domain.tlds/my-ca-offer/";
And for non-targeted/other visitors, you have to specify the URL in "All" key.
That is:
Code:
target['All'] = "http://domain.tlds/non-targeted-visitors-page/";
Here's how the full code looks like:
Code:
<script src="//j.maxmind.com/app/country.js" charset="ISO-8859-1" type="text/javascript"></script>
<script>
var target = new Array(); // Target Array, Don't remove this.
// Here's an example which redirects visitors to your specified URL for your targeted country code:
target['All'] = "http://domain.tlds/non-targeted-visitors-page/"; // Redirects non-targeted/other visitors to this URL.
target['US'] = "http://domain.tlds/my-us-offer/"; // Redirects' "US" (United States) Visitors to this URL.
target['GB'] = "http://domain.tlds/my-uk-offer/"; // Redirects' "GB" (United Kingdom) Visitors to this URL.
target['CA'] = "http://domain.tlds/my-ca-offer/"; // Redirects' "CA" (Canada) Visitors to this URL.
/* =============== DON'T EDIT THE BELOW CODE =============== */
var visitorCountryCode = geoip_country_code();
var redirectURL = target[visitorCountryCode];
if(redirectURL == null) redirectURL = target['All'];
if(redirectURL) window.top.location.href = redirectURL;
/* =============== DON'T EDIT THE ABOVE CODE =============== */
</script>​
So you can just copy-paste the above code in some html file and start driving targeted traffic to your offers/other pages.
I hope this helps you all. Any questions/help, post here and I'll try my best to help you out
Note: This code has been tested with latest versions of chrome and firefox, I don't use other browsers, So if it doesn't work, let me know.
P.S If you like the script, Please post so this thread can stay alive and others can benefit
Last edited: