gdprconsentform

How do I know whether my user is from EU countries?


Since the EU is having special laws for ads publishers. I want to display the cookie consent for my users. But I just couldn't find any good frameworks on the internet to determine whether a user is from an EU country.

Is there any way I can achieve this?

Hope to get some answers covering stuff in detail.

Thanks


Solution

  • If I correctly understand your question, you can easily do this by determining the local timezone of a user. There are several ways of doing it.

    Moment.js

    Moment Timezone has a function that guesses the timezone of a user. It is quite accurate, provided you are using the most recent version.

    Example:

    <div id="guess"></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>
    <script>
      document.getElementById('guess').innerText = moment.tz.guess();
    </script>
    

    Moment.js uses Intl API which is a built-in JavaScript internationalization API. It also has its data bank it checks the result of the Intl API against to provide more accurate information. It also requires you to have included Moment.js before it can work.

    Jstz package

    Jstz is a simple lighter package for detecting timezone. It also makes use of the Intl API, so you can be confident of its results. To use the package, you can grab the CDN as follows:

    <div id="guess"></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz.min.js"></script>
    <script>
      document.getElementById('guess').innerText = jstz.determine().name();
    </script>
    

    Intl API Itself:
    You can use the Intl API directly!

    The Intl object is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting. The Intl object provides access to several constructors as well as functionality common to the internationalization constructors and other language sensitive functions.

    Example usage:

    <div id="guess"></div>
    <script>
      document.getElementById('guess').innerText = Intl.DateTimeFormat().resolvedOptions().timeZone;
    </script>
    

    What you can notice from these libraries used for detecting timezone is that they do not require any network calls from your end. This means if you intend to only pick user timezones, you may not need to do IP lookups.

    Then you can just match the timezone with the specific EU timezone and determine if your user is from within EU.

    If you want to do an actual/accurate pinpointing of a user, you have to use GeoLocation. Here is a simple script which does that: https://raw.githubusercontent.com/AdeyinkaAdegbenro/Detect_Location/master/detect_location.js.

    Other APIs you might want to check (whatever suits you):