How can I get a user's country code using JavaScript?

Topic summary

The discussion centers on implementing JavaScript solutions to detect a user’s country code in Shopify stores without using apps.

Initial Problem & Solution:

  • Original poster encountered “$ is not defined” error when trying to use jQuery-based geolocation code
  • Issue resolved by changing HTTP to HTTPS in the API endpoint and ensuring jQuery was properly loaded via CDN before the geolocation script

Recommended Approaches:

Official Shopify Method:

  • Install Shopify’s free Geolocation app (now default on stores)
  • Fetch country data from browsing_context_suggestions.json endpoint
  • Access country code via detected_values.country.handle
  • Limitation: Only works for countries configured in the store’s market list

Alternative Method:

  • Use third-party services like ipinfo.io with jQuery
  • Requires proper jQuery CDN integration before executing geolocation code

Common Implementation Issues:

  • jQuery must be loaded before geolocation scripts run
  • Code placement in theme files (like header.liquid) requires proper script tag formatting
  • HTTPS endpoints needed instead of HTTP for security

Use Cases: Participants seek solutions for displaying country-specific shipping information and implementing geo-based URL redirects with popups.

Summarized with AI on October 29. AI used: claude-sonnet-4-5-20250929.

Dear all,

do you know a good javascript solution to get user’s country code? I need to make several custom notes based on the location of the visitor.

I tried searching around and this seemed to be a solution but somehow it doesnt work when i add it to my shopify store.


Do you know what could be the reason for that? The error message says: Uncaught ReferenceError: $ is not defined.

Or if you might have another working solution (but no apps), i would be very grateful.

Thanks!

1 Like

Hi @Nor1 ,

Can you share your site link? We can help you to give solution.

Even customization support for it.

Hi @Nor1

Hope this message finds you well and safe.

Aibek is here from Speedimize.io

The error indicates that you do not have jQuery. https://www.w3schools.com/jquery/jquery_get_started.asp

You can connect it via cdn before you receive the country data. That is, it should be as below:


There may still be a problem that you have jQuery, but it connects asynchronously. In this case, check whether there are attributes such as async defer in the jQuery tag.

Hope that helps you.

1 Like

thanks a lot!!!

Actually, the problem was not in the jquery, but in the http → https, as you indicated in your correction. After that, all worked flawlessly.

Thanks you :slightly_smiling_face:

HI Speedimize,

That was extremely useful information, thank you for sharing.

I am looking for something similar, in that, I wish to display on a Page in my Store the shipping speed specific to the User’s country (not the Shipping Zone or Rate, just information on a page similar to About Us).

How would I display information in the middle of an About Us page that says the shipping speed to the USA is 10 days (for example)?

Thanks in Advance,

Happy.

Yes, this works

Hey @Nor1
There is one other Shopify official solution to retrieve the user’s country code.

  1. First, install the free app (made by Shopify) called Geolocation. (App is installed by default on all stores nowadays)

  2. Then put this JS code somewhere on that page inside < script > tags

fetch('browsing_context_suggestions.json')
.then(res=>res.json())
.then(r=> console.log("User Country", r.detected_values))

This will get the JSON containing the visitor information, and extract the ‘detected_values.country.name’ value from it.

To see all available information visit the URL below (replace ‘store-name’ with your store name)

store-name.myshopify.com/browsing_context_suggestions.json

Hope this finds you well!

1 Like

Hi,

I’m trying to use your code to get geo URL redirect with a popup window.

I inserted the code into “header.liquid” but nothing came out. Could you share some lights? tks

Hi,

could you elaborate a bit where to add the code and how to get the country code from JSON? I’m new to this and would like to extract the user country “handle” “US” to redirect url. tks

{"detected_values":{"country_name":"Stati Uniti","country":{"handle":"US","name":"Stati Uniti"}},"features":{},"suggestions":[]} 

You can easily incorporate this into any JavaScript file or within a tag wherever needed. Simply access the country handle by

detected_values.country.handle

If the country is not in the “market” list then it won’t show the correct country code. Is there a way to bypass the default geolocation app?

Hi,

as u can see, the code is not being executed

This is an awesome answer! thanks!

Because jQuery cdn like is not Attached with your code :
"

Device

"