Save User Country

Q&A about the latest versions
Post Reply
Roland
Posts: 10
Joined: Thu Apr 02, 2020 2:01 pm

Thu May 07, 2020 7:39 am

Hi,

Is it possible to get the country of the user and store it in a variable?

I know it's possible to make web calls using javascript but I'm not sure whether returned results (for example JSON) can be stored (and parsed) within the tour.

Thanks!

Roland
Roland
Posts: 10
Joined: Thu Apr 02, 2020 2:01 pm

Wed May 20, 2020 6:24 am

Anyone?

Basically what I'm asking is whether it's possible to call a web API using JavaScript and store the response in a variable.

Cheers,
Roland
User avatar
thomas
Chief Gnome
Posts: 2554
Joined: Fri Sep 01, 2006 3:56 pm
Location: Vienna, Austria
Contact:

Thu May 21, 2020 2:51 pm

Yes, you can do this for example with https://ip-api.io/

Just put the following into a textbox, and add a text variable "country". You will also need an API key from ip-api.io. Then replace "{your api key}" with your key.

Code: Select all

javascript:"";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
        var r=JSON.parse(xhttp.responseText);
        player.setVariableValue("country",r.country_code);
    }
};
xhttp.open("GET", "https://ip-api.io/json?api_key={your api key}", true);
xhttp.send();
MfG, Thomas
Roland
Posts: 10
Joined: Thu Apr 02, 2020 2:01 pm

Mon May 25, 2020 2:36 pm

Awesome, that's exactly what I was looking for. Thanks a lot!

Cheers,
Roland
Post Reply