سؤال

My page uses GA to provide myself with statistics. I can later see countries that were most visiting my site.

Are there any way to get client's country at the client-side? I would like to show user a notification if she is not from my country.

هل كانت مفيدة؟

المحلول

You don't have access to that information in the browser using GA.

If you want to access that information in the browser Try geolocation in browsers whom support it.

spec. http://dev.w3.org/geo/api/spec-source.html

demo: http://html5demos.com/geo

sample:

if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
        });
    }

And then you can access to the complete address using google ReverseGeocoding in formatted_address filed

    {
  "status": "OK",
  "results": [ {
    "types": street_address,
    "formatted_address": "275-291 Bedford Ave, Brooklyn, NY 11211, USA",

نصائح أخرى

you'll need to use an IP based Geolocation service - a good free one is IPInfoDB I use it to detect if a visitor is from my province, and pop up a message if they are. Works great.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top