Previous     Next

Create a Website Comparison Widget using GoogleTrends

Google Trends for Websites is a pretty cool way to compare sites. While I was using it, I thought it would be great as a ‘motivational widget’ - I would enter my site and a competitor and then display the widget on my desktop.

To try it, add this to your page:

<script src='http://lolcats.com.89.seekdotnet.com/WebsiteComparison.aspx?site1=techcrunch.com&site2=mashable.com' type='text/javascript'></script>

To get:

(change mashable and techcrunch to whoever you want).

So now the code:
Add a page to your project, and call it WebsiteComparison.aspx


Imports System.Net
Imports System.IO

Partial Public Class WebsiteComparison
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sHTML As String
        Dim sSite1URL As String = Request.QueryString("site1") '"mashable.com"
        Dim sSite2URL As String = Request.QueryString("site2") '"techcrunch.com"
        Dim sCacheKey As String = "CACHE_WebsiteComparison:" & sSite1URL & "," & sSite2URL

        sHTML = CType(Cache(sCacheKey), String)

        If sHTML = Nothing Then
            sHTML = ChartGenerate(sSite1URL, sSite2URL)
            Cache.Insert(sCacheKey, sHTML, Nothing, Now.AddHours(10), System.Web.Caching.Cache.NoSlidingExpiration)
        End If

        sHTML = "document.write(""" & sHTML & """);"
        Response.Write(sHTML)

    End Sub

    Private Function ChartGenerate(ByVal sSite1 As String, ByVal sSite2 As String)
        Dim sURL As String
        Dim sAllHTML As String
        Dim oHTML As New HtmlAgilityPack.HtmlWeb
        Dim oDoc As New HtmlAgilityPack.HtmlDocument
        Dim sCSS As String
        Dim sStyle As String
        Dim sChartHTML As String

        sURL = "http://trends.google.com/websites?q=" & sSite1 & "%2C" & sSite2 & "&geo=all&date=all&sort=0"
        oDoc = oHTML.Load(sURL)

        sChartHTML = oDoc.DocumentNode.SelectSingleNode("//div[@id='trends-history']").OuterHtml
        sChartHTML = HttpUtility.HtmlDecode(sChartHTML)

        sCSS = oDoc.DocumentNode.SelectSingleNode("//style").InnerHtml
        sCSS = "http://trends.google.com" & sCSS.Split(Chr(34))(1)
        sStyle = "<LINK href='" & sCSS & "' type='text/css' rel='stylesheet' >"

        sAllHTML = sStyle & sChartHTML
        sAllHTML = sAllHTML.Replace(vbLf, "")
        sAllHTML = sAllHTML.Replace(Chr(34), "'")

        ChartGenerate = sAllHTML
    End Function

End Class

Only 2 real things to note about the above code:

  1. We are using the fantastic HTML Agility Pack to read the chart page as HTML from Google and then extract the part we need. If you don’t want to use it, then you can substitute a WebClient call and add your own string processing.
  2. The result is put in the cache, so we don’t get banned from Google by calling their server too many times.

This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!

Related posts:

  1. Your own Lolcats widget (I Can Has Cheezburger?)
  2. Using Google Trends for Websites to generate keywords


« « Add GeoLocation to your site in less time than it takes to Drink a Beer
Remove Name Mangling from ASP.Net Master Pages (get your ID back!) » »

If you liked this, then subscribe to my RSS feed

Leave a reply