[Optimized] Free Geo-Target Redirect Code using MaxMind's JS API [Noob-Friendly]

Nexar

Junior Member
Jr. VIP
Joined
Jul 2, 2010
Messages
184
Reaction score
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:
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:
Hmm...No one likes this or is this in a wrong section :P?
@Mods:Please move to appropriate section if required, I thought this will be useful as i see other scripts in this section. Thanks!
 
Bump! Sorry but the thread got lost.
 
Cheers mate. Been looking for a JS version to test out at some stage. Will let you know how it goes :)
 
Cheers mate. Been looking for a JS version to test out at some stage. Will let you know how it goes :)

Great, Looking forward to see your results :)
 
Will this work in Wordpress and if so how do I enable it?

At the moment just using the script in a post isn't working for me - it doesn't redirect but rather just goes to the page the code is on.
 
Will this work in Wordpress and if so how do I enable it?

At the moment just using the script in a post isn't working for me - it doesn't redirect but rather just goes to the page the code is on.

Yes, it should work with Wordpress. I think you need to create a custom page template with this geo code and then select that template as your post/page template and when someone visits that page it'll auto execute and redirect to the page you want them to go.

If you want to add this for the main page, then add this code in your theme header.php file.

Hope that's clear, let me know if you didn't get anything or need help with anything, feel free to PM me or post here :)


Sent from my iPhone using Tapatalk
 
If anyone needs any help or have any sort of script requirement or an idea that could help the community, Feel free to suggest or PM me. I'm willing to code and share if its worth and useful for everyone :)
 
Many thanks for this great script :P , do you think is work 100% on all brownser
 
Many thanks for this great script :P , do you think is work 100% on all brownser

My pleasure mate.

AFAIK it should work fine and I have been using this on multiple projects without any issues. If JS is blocked then obviously it won't work, in fact many sites will stop working as these days JS is heavily used. And if there is some issue from the API providers end, then it won't work. But that's hardly a case.

Hope it clears your doubt.

If you find any bugs or it doesn't work on some browser, do let me know and I'll try to fix that for you.



Sent from my iPhone using Tapatalk
 
I was thinking of creating a tutorial for PHP geo target script with of course the wrapper or the simple function. If anyone is interested in this, let me know and I'll work on it.

P.S If anyone needs any help with any stuff, let me know and I'll do my best to help you out :)
 
OP thanks man your script worked perfectly, just what I was looking for.

Cool, You're most welcome. Let me know if you need any help anytime :)

Thanks for posting!
 
thanks for the great script. i've been using it for a month and it works great and the geotargeting is very accurate.

my only question, is there any way you can modify the way the redirect works in the script so that it does a DMR ?
I see the referrer leaks through this and i was wondering is there any way you can modify the script so it makes sure that doesn't happen? If so that would be awesome and make this script even better.

thank you.
 
thanks for the great script. i've been using it for a month and it works great and the geotargeting is very accurate.

my only question, is there any way you can modify the way the redirect works in the script so that it does a DMR ?
I see the referrer leaks through this and i was wondering is there any way you can modify the script so it makes sure that doesn't happen? If so that would be awesome and make this script even better.

thank you.

Great, Glad it works good for you.

You can easily implement the DMR feature. Not all want it, So i didn't add to this as the primary goal was just to handle Geo redirection.

And I've updated with the maxmind URL as the previous one seems to be removed by them.
 
Use the following maxmind script as the previous one is removed or dead for some reason. Update your codes to this one:
Code:
<script src="//j.maxmind.com/app/geoip.js"></script>

Instead of (Remove this):
Code:
<script src="//j.maxmind.com/app/country.js" charset="ISO-8859-1" type="text/javascript"></script>
 
Last edited:
Hi, if i want to redirect 3 country to only one URL, can i put it all in 1 line?

target['US', 'CA', 'GB'] = "domain.tlds/my-us-offer/"; (THIS DOESN'T WORK)

Thanks!
 
any help !
Are you using a better GEO redirect method ?
thanks!
 
I had a geotargetting script but somehow sometimes it didnt get the job done.
I will try this one and many others to see what works best :)
 
Back
Top