Change the widget based on visitor location using Cloudflare’s IP Geolocation
- Must be knowledgeable about setting up cookies on web servers.
Cloudflare has a network configuration called IP Geolocation. This feature includes the country code associated with your website’s visitor location when answering all requests on your website.
Using this information, we can set the widget to be displayed based on the visitor’s current location.
Setting up the “country” cookie
For this to work, you’ll need first to enable the IP Geolocation configuration. Learn how in the Configuring IP Geolocation section of this article: Configuring Cloudflare IP Geolocation
Next, you should now be able to receive the country code on your request headers.
Learn how to get the country code based on your preferred programming language in the Capturing Geolocation data in application logic section of Configuring Cloudflare IP Geolocation.
Once that’s done, you just need to set it as a cookie called “country” and we’ll be able to create criteria on which widget to display based on that cookie data.
Here’s a code snippet that allows you to automatically display the widget based on the visitor’s current location.
To use the code, you’ll first need to add your widget and property IDs.
Learn where to find the widget and property ID’s here: Where can I find the property and widget IDs?
For the list of country codes that Cloudflare can return, you can take a look at ISO 3166-1 alpha-2 - Officially assigned code elements.
If you need additional assistance setting up multiple widgets on your site, visit our Developer Platform or reach out in the chat. We are here to help 24x7.