Shorten your URL with Goo.gl or Bit.ly in Asp.net using C#


While building an e-commerce web application, I realized that every e-commerce site needs social media button on the product detail page so that visitors can share what they have found. Of course you can let them share an URL like:

http://www.mydomain.com/cars/audi/Audi-R8-V12-TDI-Le-Mans
But since the twitter limit of 140 chars and the URL having 57, you can only share a message of 82 chars (with space) which is kind of short.
ps. when URL rewriting, consider using a dash instead of plus or underscore character
So in order to have good URL’s for social sharing, you should shorten your URL’s
I have made a test with Asp.Net C# comparing the famous bit.ly and goo.gl (which officially only works from the Google toolbar)

Bit.ly


For Bit.ly, I used this DLL from this website: http://sites.google.com/site/bitlyapp/
After I included it in my project, I made a helper method:
internal static string GetBitLy(string urlToShorten)
{
    string urlShort = "";
    if (urlToShorten.Length > 0)
    {
        try
        {
            urlShort = Bitly.API.Bit(Util.GetConfig("BitlyUser"),
                                     Util.GetConfig("BitlyApiKey"),
                                     urlToShorten,
                                     "Shorten");
        }
        catch (Exception ex)
        {
            // magic errorlogging goes here
        }
    }
    if (urlShort.Length > 0) // this check is if your bit.ly rate exceeded
        return urlShort;
    else
        return urlToShorten;
}

My Util.GetConfig does:

ConfigurationManager.AppSettings.Get(key);

Only then with Caching and error handling and it saves me typing a few characters. 

Goo.gl


I have found some code in a comment of a blog. The comment pointed to this URL. I have uploaded my class here GoogleUrlShortner.cs (4.17 kb) as mirror. 

The test


Here is my code for the test:
public string GetShortUrl()
{
    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    sw.Start();
    shortUrl = Util.GetBitLy(shortUrl);
    sw.Stop();
    System.Diagnostics.Debug.WriteLine("bit.ly: " + sw.ElapsedTicks + " and url " + shortUrl);
    sw.Restart();
    shortUrl = shop.BLL.Utility.GoogleUrlShortner.Shorten(shortUrl);
    sw.Stop();
    System.Diagnostics.Debug.WriteLine("goo.gl: " + sw.ElapsedTicks + " and url " + shortUrl);
    shortUrl = Server.UrlEncode(shortUrl);
    return shortUrl;
}
As you've noticed, I used the Stopwatch class which is great for measuring performance of your code. 

The result


Here is the output.
bit.ly: 1988350 and url http://bit.ly/cLkqPZ 
goo.gl: 1062865 and url http://goo.gl/DrFx
Second run: 
bit.ly: 1125702 and url http://bit.ly/cLkqPZ 
goo.gl: 674658 and url http://goo.gl/DrFx

Conclusion


Bit.ly has a great benefit that you can track the clicks of a shortened URL. but when using URL shortening on a product detail page, it’s useless when you have about 3000 product detail pages.
So I’d chose to go with Goo.gl since it has a faster response time and less characters in the shortened URL’s
Update 11-jan-2011: Google URL Shortner gets API!


0 comments:

Post a Comment

Vikram Chandra Theme by BloggerThemes & NewWPThemes Sponsored by iBlogtoBlog