ipdata.co - try to do URL redirect from country code of the user

ipdata.co - try to do URL redirect from country code of the user

radiofranky2022
New Member
8 0 0

Hi,

I'm trying to find an API that allow me to get the user's country code.   tried ipdata.co but can't get the geo data,

example javascript doesn't work.  Here is the original code from ipdata.co + removed my API key

 

suspect something wrong with the script or shopify is blocking the access.   Any help is appreciated.  

 

keep getting this error message on online javascript compiler:

 

 

 

"<a class='gotoLine' href='#42:1'>42:1</a> Uncaught ReferenceError: $ is not defined"

 

 

 

 

 

 

 

// Getting the country code from the user's IP
$.get("https://api.ipdata.co?api-key=mykey", function (response) {
  if (response.country_code == 'UK') {
    window.location.href = "https://uk.store.ipdata.co";
    } else if (response.country_code == 'DE') {
    window.location.href = "https://de.store.ipdata.co";
    } else if (response.country_code == 'AU') {
    window.location.href = "https://au.store.ipdata.co";
    }
}, "jsonp");

 

 

 

 

Reply 1 (1)

gr_trading
Shopify Partner
1834 145 193

Hi @radiofranky2022 ,

 

Try using below script to get the desired information.

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$.get("https://ipinfo.io", function(response) { // we changed to https
console.log(response.city, response.country);
}, "jsonp");
</script>

 

Hope this will help...

For any custom development WhatsApp or connect at Email ID: gr.trading15@gmail.com for quick consultation. | Shopify Free codes
To support Buy Me a Coffee