Klipfolio. The KPI Dashboard - Evolved
The KPI Dashboard – Evolved
1-877-233-6149
Contact Us

Make Your Klip Geo-Aware

Problem:

You have a Klip that depends on geographic data, but you don't want your users to have to enter their data in the setup window.

Solution:

You can use a few lines of script in your Klip to automatically determine where a user is located in the world, based on their IP address. During your onLoad() handler, determine whether the Klip is being run for the first time (you can do this based on the Klip's preferences, for example). Then, during your onRefresh() event, use the following code:

if(Prefs.getPref("firstRun") == "Yes")
{
    Prefs.setPref("firstRun", "No");
    
    var locReq = Engines.HTTP.newRequest("http://api.hostip.info/get_html.php");
    if(locReq.send())
    {
        var loc_re = new RegExp("Country: ([^]*?)City: ([^]*)");
        var locInfo = loc_re.exec(locReq.response.data);
        if(locInfo[2].toLowerCase().indexOf("unknown") == -1)
        {
            // Don't use the geo data if the city is unknown.
            
            // locInfo[1] is the country.
            // locInfo[2] is the city.
        }
    }        
}

A request is made to an online tool that determines the user's location based on their IP address. The data is parsed, and can then be used to automatically set a Klip to provide geo-targetted information to the user. Be sure to handle the case where the user's IP address represents an unknown location, otherwise your Klip may produce unexpected results.

Check out the Google News Klip for an example of this in action.

Leave a Comment

To leave a comment, you must sign in or register (it's free).

   
Comment Type:
Title:
Comment: