We’d like to block users in a particular state from seeing a Typeform quiz that appears on our site. What is the fastest way to do this?
Exclude users in certain states from seeing a quiz.
Best answer by mathio-tf
Hello
you will need to use a service like https://ip-api.com/ to identify user’s location. This might not be 100% accurate though as it depends on the IP address of users and that depends on their internet provider or they might even be using VPN. However if you accept the tradeoffs I think this is the best approach. You can then write a custom JavaScript code to only show the form to users not from given state.
fetch('http://ip-api.com/json/')
.then(response => response.json())
.then(({ countryCode, region }) => {
// hide the form for users from California
if (countryCode === 'US' && region === 'CA') {
// this assumes your form is inside an element with id="my-form"
document.querySelector('#my-form').remove()
}
})
The form will be displayed for a short time until user location details are retrieved. If you want to avoid this, you can display the form in the then
callback instead of hiding it, however it will introduce a dependency on the 3rd party service and your form might not display at all if it fails.
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.