Page 1 of 1

Save User Country

Posted: Thu May 07, 2020 7:39 am
by Roland
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

Re: Save User Country

Posted: Wed May 20, 2020 6:24 am
by Roland
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

Re: Save User Country

Posted: Thu May 21, 2020 2:51 pm
by thomas
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();

Re: Save User Country

Posted: Mon May 25, 2020 2:36 pm
by Roland
Awesome, that's exactly what I was looking for. Thanks a lot!

Cheers,
Roland