<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=442498563201411&amp;ev=PageView&amp;noscript=1">

Use Cases: How to Leverage Geofencing to Optimize Content Monetization

img-integrations-other
Topics: Use Cases
Experience Level: Advanced
October 28, 2019 · 7 min read

 

 

 

 

 

 

“Geofencing,” the process of building a virtual perimeter for a real-world geographic area, is a process whereby businesses can execute specific actions or triggers based on the location of the individual who is accessing their website. This creates not only a more personalized user experience but also allows you to better segment audiences and employ targeted strategies for each.

 

Whether you enjoy a national or international audience, as a publisher you can employ geofencing to generate additional revenue and build a more loyal user base. Learn more about how to employ geofencing as part of your digital strategy here

 

In this article, we will describe how you can limit contributions to a particular country - in this case - Germany. 

 

First of all, are you familiar with the LaterPay Contributions button? In a nutshell, generating a button means producing javascript code which you can then embed in your webpage, so that users can contribute by clicking on it. Using the LaterPay button, you can choose to ask for registration and payment either immediately or later - after a reader's tab has reached $5. Read more about the button here and see how easy it is to implement it


The procedure is quite straightforward: all we need to do is to add a javascript snippet between the <head> tags of the page(s) where the contribution button is present, which will restrict loading it if certain conditions are not met. The snippet looks like this:

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

<script type="text/javascript">

$(document).ready(function () {

    $("#lp-link").click(function (e) {

        e.preventDefault();

        var elem = $(this);

        $.ajax({ 

            url: "https://geoip-db.com/jsonp",

            jsonpCallback: "callback",

            dataType: "jsonp",

            success: function (location) {

                if (location.country_name == 'Germany') {

                    window.open(elem.attr('href'), elem.attr('target'));

                } else { 

       alert("Thanks for wanting to contribute. You must be in Germany to contribute");

   }

           }    

        });

    });

});

</script>

 

Now let us go into this snippet step by step:

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

 

This is the relevant jquery library, which needs to be loaded on the website to run the geofencing code properly. Please note that we have used jquery version 3.4.0, which is the latest version at the time this article was written - please find the newest version here and load that version accordingly.

 

<script type="text/javascript">

$(document).ready(function () {

 

These lines are needed both to indicate that we are inserting a javascript code into this file and also that we will be loading the javascript function once the document is ready

 

$("#lp-link").click(function (e) {

 

This part of the code may be different on your website. The important thing here, is that you correctly attach the “click” listener to the right element, which is, in this case, identified with an id “lp-link”. 

 

In other words, if you are using the predefined button generated from the LaterPay Admin Interface, this ID will be the same. It will only change if you are using a customized button which you have created. If that is the case, you need to specify the ID of the link within the button, so that the code works properly.

 

 e.preventDefault();

 

This prevents the default behavior, which would occur when we click on the contribution button - namely to bring us to the contribution purchase screen. This gives us time to check whether the user has an appropriate geolocation.

var elem = $(this);

        $.ajax({ 

            url: "https://geoip-db.com/jsonp",

            jsonpCallback: "callback",

            dataType: "jsonp",

            success: function (location) {

 

Here we are associating the element that has been clicked (the button link element) to the “elem” variable, as this will be needed later. Next, we invoke the geoip-db.com service, which is useful when determining the geographical location of a visitor. Once the location has been determined successfully, we move to the next step

 

if (location.country_name == 'Germany') {

      window.open(elem.attr('href'), elem.attr('target'));

 

Here we simply check the country name in the newly obtained location variable - namely, whether it is equal to “Germany.” If this is the case, we want to proceed to the contribution purchase dialog, which is encoded in the “href” attribute of the clicked button link (lp-link). In other words, if the visitor is located in Germany, we forward them to the purchase dialog, so they can contribute.

 

Note that the conditional statement here can be expanded using “and”, “or” or “not” conditions. Take into consideration that the geographical service loaded might not be as precise to do fine-grain geographical targeting; however based on our experience, it works quite well for countries as a whole.

 

else {

      alert("Thanks for wanting to contribute. You must be in Germany to contribute");

}

 

Finally, if the condition is not satisfied (the visitor is not located in Germany), an alert will pop up indicating that contributions from outside of this country are not possible. This can, of course, be customized in any way you deem fit.