Best practice method for detecting user's location (country only - not specific location)

947 views
Skip to first unread message

Rory

unread,
May 31, 2021, 3:56:39 PM5/31/21
to Chromium Extensions
Hi,

My extension includes Stripe payments integrations and a number of offers that are made based on the country the user is in.

I've been hunting around online for the best way to detect the location of a given user.

I'm trying to use navigator.geolocation.
I'm running manifest 3 and I just added "geolocation" to the permissions array which I believed granted the ability to see user location in Chrome.

I'm testing this code now in my service worker but it seems that despite having the geolocation in the extension manifest permissions - it still needs to prompt the user to detect their location.

I'm wondering if there is an easier/less intrusive way to know their location? I only need to know their country so I know what payment options to offer, not their specific longitude/latitude.

Thank you

Rory

unread,
May 31, 2021, 3:57:13 PM5/31/21
to Chromium Extensions, Rory
Perhaps a method involving pulling the user's IP and sending it to an API that returns country?

Cuyler Stuwe

unread,
May 31, 2021, 4:01:00 PM5/31/21
to Chromium Extensions, Rory
If you only need to guess the user's country country, I would personally use an IP address lookup API.
It's not perfect, but then again, neither is geolocation. In fact, ultimately the "geolocation" data on a desktop computer is likely to just be using the same kind of data. Only on mobile might the geolocation function reliably use something more precise (cell towers and/or actual GPS).
On Monday, May 31, 2021 at 12:56:39 PM UTC-7 Rory wrote:

Deco

unread,
May 31, 2021, 4:01:40 PM5/31/21
to Rory, Chromium Extensions
 chrome.i18n.getUILanguage() can be used to fetch the locale and browser language that the browser is in. This will return a two letter ISO country code that you can use for this purpose without having to mess with the location permission.

Note that for the location permission, it is not possible to do it in another form, as express consent must be granted by the end user. This is why the icon is present within Chrome before location access is granted to an extension.

Hope this helps.

Regards,
Deco


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/4110c406-feb9-4eea-97bf-b5dc8bc09319n%40chromium.org.

Rory

unread,
May 31, 2021, 4:13:54 PM5/31/21
to Chromium Extensions, decklin...@gmail.com, Chromium Extensions, Rory
Hi Salem - 
"I would personally use an IP address lookup API." - thank you - do you have a recommended best practice method for retrieving a user's IP?

Hi Decklin- 
"chrome.i18n.getUILanguage() can be used to fetch the locale and browser language that the browser is in." - thank you for this potential solution. I'm worried about the robustness of it - a user might be in the UK or Aus and have their browser set to US English for instance. I might combine this with the above IP method perhaps.

Deco

unread,
May 31, 2021, 4:17:35 PM5/31/21
to Rory, Chromium Extensions
Hi Rory, combining both would also be a potentially good option. 

Thanks,
Deco

Rory

unread,
May 31, 2021, 4:31:21 PM5/31/21
to Chromium Extensions, decklin...@gmail.com, Chromium Extensions, Rory
Thanks. I found a great solution making a fetch request to ipdata.co.

Haydn Appleby

unread,
Jun 1, 2021, 10:20:52 AM6/1/21
to Chromium Extensions, Rory, decklin...@gmail.com, Chromium Extensions
To clarify about `chrome.i18n.getUILanguage()` - this will not retrieve the preferred language of the browser settings (e.g. whatever is `navigator.language`) but the language of the system UI itself... the language that the user sees the menu bar in, for example. This is much, much safer than `navigator.language` but still open to the same issue that a user may be based in Germany but have UI in Spanish etc.

Vladimir Yankovich

unread,
Jun 1, 2021, 1:11:14 PM6/1/21
to Chromium Extensions, Haydn Appleby, Rory, decklin...@gmail.com, Chromium Extensions
I have been researching this question for a long time, here is the best I could find - https://www.bigdatacloud.com/geocoding-apis/free-reverse-geocode-to-city-api
Reply all
Reply to author
Forward
0 new messages