(Javascript) How to re-make this code to work with a new API ?

virtualpurity

Elite Member
Jr. VIP
Joined
Nov 12, 2012
Messages
1,560
Reaction score
893
Hello,

I am trying to re-use an old script from Cpaclickz which is using the Maxmind old API.

The code that the script uses is :

Code:
<script src="http://j.maxmind.com/app/country.js" charset="ISO-8859-1" type="text/javascript"></script>
<script type='text/javascript'>
    var visitorCountry = geoip_country_code;
    document.write('<scr'+'ipt src="redirect.php?code=<?php echo "$campaign_code"; ?>&sid=<?php echo "$sid"; ?>&geo='+visitorCountry+'" type="text/javascript"><\/scr'+'ipt>');
</script>

Now there is a new API v2 form Maxmind :

https://dev.maxmind.com/geoip/geoip2/javascript/
https://dev.maxmind.com/geoip/geoip2/javascript/tutorial/

Need help to remake the old code to work with the new API.
Any help is appreciated.
 
Not enough context here as there are php variables that are referenced in the "old code" that are not declared.

PM me with more details and/or a link to a working example and i can help ya outa
 
I don't think it is working anymore. I believe the Maxmind Javascript support has been deprecated.
 
Not enough context here as there are php variables that are referenced in the "old code" that are not declared.

PM me with more details and/or a link to a working example and i can help ya outa

This is just the part of the code i need re-made. I cant copy the whole code here for some reason it doesnt let me.

If you see the API link : https://dev.maxmind.com/geoip/geoip2/javascript/

You will see that it uses now : geoip2.country Instead of : geoip_country_code

The whole thing is to re-make this part so it would work with the new API :

Code:
<script src="http://j.maxmind.com/app/country.js" charset="ISO-8859-1" type="text/javascript"></script>
<script type='text/javascript'>
    var visitorCountry = geoip_country_code;
    document.write('<scr'+'ipt src="redirect.php?code=<?php echo "$campaign_code"; ?>&sid=<?php echo "$sid"; ?>&geo='+visitorCountry+'" type="text/javascript"><\/scr'+'ipt>');
</script>

Was thinking something between these lines (just for example i done even know if this is correct ) :

Code:
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script type='text/javascript'>
geoip2.country(
    function (response);
    var visitorCountry=response.country.iso_code);
 document.write('<scr'+'ipt src="redirect.php?code=<?php echo "$campaign_code"; ?>&sid=<?php echo "$sid"; ?>&geo='+visitorCountry+'" type="text/javascript"><\/scr'+'ipt>');

  function (error) {
        // handle error
    }
</script>

I don't think it is working anymore. I believe the Maxmind Javascript support has been deprecated.

No its not. They still provide Javascript API support, now you just need to buy credits from their site and register your domain from where you will be using the API from, which i have done
.
 
This should work:

Code:
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script>
  var onSuccess = function (location) {
    var visitorCountry = location.country.iso_code;
    document.write('<scr' + 'ipt src="redirect.php?code=<?php echo "$campaign_code"; ?>&sid=<?php echo "$sid"; ?>&geo=' + visitorCountry + '" type="text/javascript"><\/scr' + 'ipt>');
  };

  var onError = function (error) {
    console.error(error);
  };

  geoip2.country(onSuccess, onError);
</script>
 
This should work:

Code:
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script>
  var onSuccess = function (location) {
    var visitorCountry = location.country.iso_code;
    document.write('<scr' + 'ipt src="redirect.php?code=<?php echo "$campaign_code"; ?>&sid=<?php echo "$sid"; ?>&geo=' + visitorCountry + '" type="text/javascript"><\/scr' + 'ipt>');
  };

  var onError = function (error) {
    console.error(error);
  };

  geoip2.country(onSuccess, onError);
</script>

Got it working with this code made by a friend if someone else needs it :

Code:
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script type='text/javascript'>

        var visitorCountry = 'US';
        geoip2.country(function(response) {
            
            if (response.country.iso_code) {
                visitorCountry = response.country.iso_code;
            }
    document.write('<scr'+'ipt src="redirect.php?code=<?php echo "$campaign_code"; ?>&sid=<?php echo "$sid"; ?>&geo='+visitorCountry+'" type="text/javascript"><\/scr'+'ipt>');
        }, function(err){
            console.log(err)   
        }, {});
        
</script>
 
It's the same as mine, but not so clean. ;)
The only difference is, that you now have a default 'US' if no country is found.
Here's the same with my version:

Code:
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script>
  var onSuccess = function (location) {
    var visitorCountry = location.country.iso_code || 'US';
    document.write('<scr' + 'ipt src="redirect.php?code=<?php echo "$campaign_code"; ?>&sid=<?php echo "$sid"; ?>&geo=' + visitorCountry + '" type="text/javascript"><\/scr' + 'ipt>');
  };

  var onError = function (error) {
    console.error(error);
  };

  geoip2.country(onSuccess, onError);
</script>
 
Back
Top