How to Restrict Website Traffic by Country in Shopify

Managing unwanted actions or spam traffic can be a need in your business. You can use apps or code to block specific countries based on a visitor's location
Froonze
June 18, 2024

There can be different reasons why you might want to restrict traffic by country in your Shopify store. Legal restrictions, product regulations, data analysis, or simply managing unwanted actions or spam traffic on your site.

While you can use Shopify Delivery Areas to prevent ordering from specific countries, you may want to apply stricter measures to control or discourage traffic from specific countries.

This guide explores your options, beginning with the most straightforward method of using an app. We will then continue with a custom code solution that enables you to block traffic and restrict content using an external IP service.

How to Restrict Website Traffic by Country in Shopify

Using a Country Blocker app

We can't but first consider the possibility of employing a third-party app to manage traffic. There are different options in the Shopify App Store that will allow to control customer access based on their geographic location. These app often provide the best balance of cost, customization, and user-friendliness, making them an excellent starting point. This approach is particularly appealing for store owners who seek effective solutions without the complexity of custom programming, allowing an easy implementation of a block list to restrict access from specific countries.

You can find the best selection of these apps in Shopify's App Store, within the Privacy and Security category, in IP Blockers (click here).

Using an IP service

There is also the option that you use a custom code solution, based on an external IP service, to detect the browser IP of your visitor and replace or lock content by country. This will certainly discourage continued traffic from those locations, but may need you to buy a plan if you have too many requests (page visits) a month. IPInfo provides a free plan for up to 50k requests a month so we have used this service in the solution below. Potentially this solution could be applied in platforms other than Shopify, adapting the liquid code.

  1. Go to your Shopify admin and navigate to Online Store > Themes. Find the theme you want to edit and click on the Three points icon > Edit code.
  2. In the Theme Editor, search for theme.liquid. This will bring up the main layout. Open it.
  3. Inside the layout, find the end of the body: </body>
  4. One line above, paste the script below. You would need to replace the country Alpha-2 code within with that of the desired country you want to block (you can find the Alpha-2 country codes here). The lock message ("This content is not available...") can be customized as well.
  5. Save and preferably, check if it's working on site using a VPN or similar.

<script> document.addEventListener('DOMContentLoaded', function() { fetch('https://ipinfo.io?callback') .then(res => res.json()) .then(json => { if (json.country == "ES") { document.querySelector('main').innerHTML = `<div class="content_blocked" style="padding: 10rem; text-align: center; margin: auto">This content is not available in your country</div>`; } }) },false); </script>

Country Blocker code solution with IP service

Conclusions

Restricting website traffic by country on your Shopify store is crucial for managing legal restrictions, product regulations, data analysis, and combating unwanted activities like spam traffic. You can achieve this by using Shopify Delivery Areas to prevent ordering from specific countries, or by implementing stricter measures to control or discourage traffic itself. We have introduced two main options for restricting website traffic by country in Shopify. Choose the method that best suits your comfort level and the level of control you require.

  • Apps: User-friendly and readily available in the Shopify App Store, these apps offer a good balance of cost and customization for most needs.
  • Custom Code:  Provides more granular control over blocking traffic. However, this approach requires coding expertise and may incur additional costs depending on traffic volume.

Also you may want to have a look at other of our articles regarding blocking:

Froonze Customer Accounts Concierge
Get the app today
Turn the account page into a beautiful portal with
Froonze Customer Accounts Concierge
  • 220+