Geo-Targeting with JavaScript and Custom Triggers
Visually supports location-based targeting, allowing you to customize experiences based on a visitor's geographic location. For more advanced use cases, you can access location information through Visually's JavaScript SDK or use Custom JS Triggers with third-party geolocation services.
Accessing Location Data Through JavaScript
Visually provides the following methods through the loomi_api JavaScript SDK:
window.loomi_api.geoCountry()– Retrieves the visitor's country code, allowing Custom JavaScript to identify their country without requiring a separate third-party geolocation API.window.loomi_api.geoState()– Retrieves the visitor's state or region.window.loomi_api.getIp()– Retrieves the visitor's IP address.
Location information is resolved on the backend during page load, meaning these methods provide immediate results without requiring an additional geolocation request.
Using Country Codes in Custom JavaScript
You can use loomi_api.geoCountry() within your experience's Custom JavaScript to access the visitor's country code.
For example:
const countryCode = window.loomi_api.geoCountry();
console.log(countryCode);
This allows you to build country-specific logic, such as displaying localized content for visitors from different countries, without relying on an additional geolocation service.
You can also combine this functionality with Visually's existing audience targeting to determine which visitors qualify for an experience before applying country-specific changes through Custom JavaScript.
Note: City-level resolution is not currently supported through the JavaScript SDK. If your experience requires city-level targeting, please contact our support team to discuss the available options.
Using Third-Party Geolocation Services
For more specific geographic targeting, such as identifying a visitor's city, you can use a Custom JS Trigger to request location data from an external IP geolocation provider.
Custom JS Triggers can make requests to third-party services, such as ipinfo, and use the returned information to determine whether an experience should trigger.
How Do Custom JS Triggers Affect Experience Loading?
When a Custom JS Trigger depends on a Promise, the experience will remain blocked until that Promise resolves.
If the external service takes longer to respond, the experience will also be delayed. If the Promise never resolves, the experience will remain blocked.
Important: When using third-party geolocation providers, consider their rate limits, response times, and reliability. High-traffic events or unexpected request spikes may generate a large number of lookup requests.
Make sure your selected provider can handle your store's traffic volume. Visually is not responsible for the availability or performance of third-party geolocation services.
How Does Geo-Targeting Affect Reporting?
Visitors who do not meet an experience's configured audience conditions, or whose Custom JS Trigger does not successfully pass, are excluded from that experience's session and conversion reporting.
For example, if an experience is configured to target visitors from a specific location, visitors who do not qualify for that location-based condition will not be included in the experience's results.
How Accurate Is Visually's Geolocation?
Visually uses Google Cloud Platform for IP-based geolocation.
Location accuracy follows industry standards and is comparable to services such as MaxMind.
Accuracy generally improves for larger cities, but IP-based geolocation is not always precise and may not correctly identify every visitor's exact city or location.
For experiences requiring highly specific geographic targeting, we recommend keeping these limitations in mind.
Need Additional Assistance?
If you need help implementing advanced geo-targeting or require city-level resolution through the JavaScript SDK, please contact our support team, you can email us at support@visually.io or reach out on chat for help with setup or troubleshooting.