1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'Making Money' started by dennismorris14, Nov 17, 2012.

  1. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    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 :)
     
    • Thanks Thanks x 17
    Last edited: Nov 17, 2012
  2. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    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!
     
  3. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    Bump! Sorry but the thread got lost.
     
    • Thanks Thanks x 1
  4. mmmwww

    mmmwww Newbie

    Joined:
    Oct 24, 2012
    Messages:
    6
    Likes Received:
    0
    Cheers mate. Been looking for a JS version to test out at some stage. Will let you know how it goes :)
     
  5. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    Great, Looking forward to see your results :)
     
  6. madfishstudio

    madfishstudio Newbie

    Joined:
    Apr 28, 2012
    Messages:
    3
    Likes Received:
    0
    awesome! exactly what i looking for.
     
  7. doctorppi

    doctorppi Registered Member

    Joined:
    Aug 13, 2008
    Messages:
    64
    Likes Received:
    20
    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.
     
  8. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    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
     
  9. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    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 :)
     
  10. ucozer

    ucozer Power Member

    Joined:
    Dec 18, 2010
    Messages:
    646
    Likes Received:
    150
    Many thanks for this great script :p , do you think is work 100% on all brownser
     
  11. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    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
     
  12. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    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 :)
     
  13. ChapoGuzman

    ChapoGuzman BANNED BANNED

    Joined:
    Jun 7, 2013
    Messages:
    294
    Likes Received:
    187
    OP thanks man your script worked perfectly, just what I was looking for.
     
  14. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    Cool, You're most welcome. Let me know if you need any help anytime :)

    Thanks for posting!
     
  15. livecity

    livecity Newbie

    Joined:
    Oct 31, 2013
    Messages:
    30
    Likes Received:
    1
    Location:
    philly
    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.
     
  16. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    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.
     
  17. dennismorris14

    dennismorris14 Junior Member

    Joined:
    Jul 2, 2010
    Messages:
    166
    Likes Received:
    175
    Location:
    WorldWideWeb
    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>
     
    • Thanks Thanks x 1
    Last edited: Mar 23, 2014
  18. Sopor

    Sopor Junior Member

    Joined:
    Aug 10, 2014
    Messages:
    147
    Likes Received:
    40
    Location:
    SOPORTERUS
    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!
     
  19. Sopor

    Sopor Junior Member

    Joined:
    Aug 10, 2014
    Messages:
    147
    Likes Received:
    40
    Location:
    SOPORTERUS
    any help !
    Are you using a better GEO redirect method ?
    thanks!
     
  20. royserpa

    royserpa Jr. VIP Jr. VIP Premium Member

    Joined:
    Sep 28, 2011
    Messages:
    4,650
    Likes Received:
    3,493
    Gender:
    Male
    Occupation:
    Negative Options aka Rebills!
    Location:
    Royserpa
    Home Page:
    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 :)