How to get visitor's geolocation (i.e. country) using Jquery

Highlighted
Shopify Expert
51 0 0

Looking for help to set up auto currency switcher too...

Skype: bestchoiceindia
Professional Shopify Custom Themes and Modifications. email info@bestchoiceindia.com
0 Likes
Highlighted
Shopify Partner
1353 24 222

You need to implement the GeoLocation API: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation

Here is a demo, the code is selfexplaining: http://html5demos.com/geo

https://sections.design tips, tricks & Shopify sections
0 Likes
Highlighted
Tourist
21 0 3

IPgeolocation is one of the best IP geolocation API services. To find the visitor's location, you can use IPgeolocation jQuery SDK. For this, you must have an API key. If you don't have an API key, you can also get it by using our free plan by signing up here. Internet connection is also needed to run this component.

 

For installation, add the following scripts in your HTML page:

<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.0.6/ipgeolocation.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Here is a sample code to use IP Geolocation API using jQuery SDK:

<script>
    var ip = sessionStorage.getItem("ip");
    var country_name = sessionStorage.getItem("country_name");
    var country_code2 = sessionStorage.getItem("country_code2");
            
    if (!ip || !country_name || !country_code2) {
        setAsync(false);
        setFieldsParameter("country_name,country_code2");
        getGeolocation(handleGeolocationResponse, "YOUR_API_KEY");
    }

    function handleGeolocationResponse(json) {
        ip = json.ip;
        country_name = json.country_name;
        country_code2 = json.country_code2;

        sessionStorage.setItem("ip", ip);
        sessionStorage.setItem("country_name", country_name);
        sessionStorage.setItem("country_code2", country_code2);
    }
                
    $(document).ready(function() {
        alert("Hello " + country_name + "!");
    });
</script>

 The complete source code for this SDK is available at Github.

 

IP geolocation API provides geolocation information in the following languages:

  • English (en)
  • German (de)
  • Russian (ru)
  • Japanese (ja)
  • French (fr)
  • Chinese Simplified (cn)
  • Spanish (es)
  • Czech (cs)
  • Italian (it)

For more information, you can read our documentation.

0 Likes
Highlighted
Excursionist
137 1 6

Hi, IP Geolocation APIs can help you get visitors's geolocations.Here is one worth checking out: GEOLocation API. You don't even have to code. Just drop in JavaScript code they provided, and you can get visitor location data(like, country). For currency switcher, by simply setting up rules, one of the same series tools, GEORedirect can auto direct visitors to certain URLs . For exapmle, exampleshop.com?curr=USD (the parameter in the URL depends on which currency they use based on their countries)

 

Hope it helps!

0 Likes