Save User Country

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

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: 35
Joined: Thu Apr 02, 2020 2:01 pm

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: 2611
Joined: Fri Sep 01, 2006 3:56 pm
Location: Vienna, Austria
Contact:

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: 35
Joined: Thu Apr 02, 2020 2:01 pm

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

Cheers,
Roland
Post Reply