Wednesday, November 18, 2009

Targeted Online Advertising by Country using .NET Framework in VB.NET or C#


The global nature of websites is such that to maximize your earnings from online advertisements, you have to display different advertisements for visitors from different countries. This way, your web visitors will only see advertisements that are relevant to their country and thus be more inclined to click on those advertisements. In the long term, this will help to boost your revenue from the advertisements.

In this example, we will use a fully functional IP2Location™ .NET component available at http://www.ip2location.net/download/IP2LocationDotNetComponent.ZIP to determine the visitor’s country by their IP address. Firstly, install the IP2Location™ .NET component. Once installation is complete, get the IP2Location.DLL .NET component and sample database from the directory, i.e. C:\Program Files\IP2Location by default. You need to add a reference to this component from your Visual Studio web project. A copy of this component will be copied into /bin directory under the project. If the component is unregistered, there is a random 5-second delay in one out of ten queries.

To avoid any delays in the query, you can consider purchasing the IP2Location™ .NET Component Version 3.0, which is a software development component and data solution for .NET Framework. You can then dynamically tailor the content of your site based on your visitors country, region, city, latitude, longitude, ZIP code, ISP, domain name, timezone, connection speed, IDD code, area code, weather statation code, and weather station name.



Below is the sample code in VB.NET and C# webform which will display the targeted online advertising by country.

Sample Codes in VB.NET Webform

Imports IP2Location
Private Sub Query(ByVal strIPAddress As String)
    Dim oIPResult As New IP2Location.IPResult

    Try
        If strIPAddress <> "" Then
            IP2Location.Component.IPDatabasePath = "C:\\Program Files\\IP2Location\\Database\\IP-COUNTRY.SAMPLE.BIN"
            oIPResult = IP2Location.Component.IPQuery(strIPAddress)

            Select Case oIPResult.Status
                Case "OK"
                    If oIPResult.CountryShort = "JP" Then
                        ' Visitor is from Japan
                        ' Show advertisement from JP
                        Response.Write "<img src=""Japan.jpg"" border=""0"" height=""200"" width=""100"" />"
                    Else
                        ' Visitor is not from Japan
                        ' Show other advertisement
                        Response.Write "<img src=""US.jpg"" border=""0"" height=""200"" width=""100"" />"
                    End If
                Case "EMPTY_IP_ADDRESS"
                    Response.Write("IP Address cannot be blank.")
                Case "INVALID_IP_ADDRESS"
                    Response.Write("Invalid IP Address.")
                Case "MISSING_FILE"
                    Response.Write("Invalid Database Path.")
            End Select
        Else
            Response.Write("IP Address cannot be blank.")
        End If
    Catch ex As Exception

    Finally
        oIPResult = Nothing
    End Try
End Sub

Sample Codes in C# Webform

Using IP2Location;
private void Query(string strIPAddress)
{
    IPResult oIPResult = new IP2Location.IPResult();

    try
    {
        if (strIPAddress != "")
        {
            IP2Location.Component.IPDatabasePath = "C:\\Program Files\\IP2Location\\Database\\IP-COUNTRY.SAMPLE.BIN";
            oIPResult = IP2Location.Component.IPQuery(strIPAddress);

            switch(oIPResult.Status.ToString())
            {
                case "OK":
                    if (oIPResult.CountryShort == "JP") {
                        Response.Write "<img src="Japan.jpg" border="\" height="\" width="\" />"
                    }
                    else {
                        Response.Write "<img src="US.jpg" border="\" height="\" width="\" />"
                    }
                    break;
                case "EMPTY_IP_ADDRESS":
                    Response.Write("IP Address cannot be blank.");
                    break;
                case "INVALID_IP_ADDRESS":
                    Response.Write("Invalid IP Address.");
                    break;
                case "MISSING_FILE":
                    Response.Write("Invalid Database Path.");
                    break;
            }
        }
        else {
            Response.Write("IP Address cannot be blank.");
        }
    }
    catch(Exception ex)
    {
        ;
    }
    finally
    {
        oIPResult = null;
    }
}

IP2Location also carries a wide variety of databases which are more advanced and has more types of data which could also be used to further narrow down your target audience. If you want to try out the online demo, you can go to http://www.ip2location.com/demo.aspx and check it out.