jQuery URL Shortener using Google URL Shortener API

In jQuery URL Shortener Plugin Tutorial, I have covered how to use jQuery URL Shortener plugin.


jQuery URL Shortener Plugin Tutorial

You need to follow the below steps to use jQuery URL shortener Plugin.

1). Add jQuery script to your code. (Plugin requires jquery 1.6+)

<script src="http://code.jquery.com/jquery-1.6.js" type="text/javascript"></script>

 

2).Download https://github.com/hayageek/jQuery-URL-shortener and script to your code.

<script src="jquery.urlshortener.js" type="text/javascript"></script>

 

3). Get a Google API key. check this tutorial how to get Google API Key. During testing phase, you can make request without API key also.You can set the API key

jQuery.urlShortener.settings.apiKey='YOUR_API_KEY';

 

4). Shortening a Long URL.

jQuery.urlShortener({
    longUrl: "http://hayageek.com/jquery-url-shortener/",
    success: function (shortUrl) {
        //shortUrl ->  Shortened URL
    },
    error: function(err)
    {
        alert(JSON.stringify(err));
    }
});

 

5). Convert Shortened URL to Long URL

jQuery.urlShortener({
    shortUrl: shortUrlLink,
    success: function (longURL) {
        //longURL ->  original URL
    },
    error: function(err)
    {
        alert(JSON.stringify(err));    
    }
});

 

6). Get Shortened URL Information.

You can get Shortened URL information like Clicks,Analytics by providing projection field to the API.

supported projection values are:
ANALYTICS_CLICKS – Get Clicks statics
ANALYTICS_TOP_STRINGS – Country,Browser specific statistics
FULL – Full statistics

jQuery.urlShortener({
    shortUrl: shortUrlLink,
    projection: "FULL",
    success: function (info) {

    },
    error: function(err)
    {
        alert(JSON.stringify(err));        
    }
});

Returned Sample Object, if the projection is  ANALYTICS_CLICKS

{
    "analytics": {
        "allTime": {
            "longUrlClicks": "13", 
            "shortUrlClicks": "12"
        }, 
        "day": {
            "longUrlClicks": "0", 
            "shortUrlClicks": "0"
        }, 
        "month": {
            "longUrlClicks": "9", 
            "shortUrlClicks": "8"
        }, 
        "twoHours": {
            "longUrlClicks": "0", 
            "shortUrlClicks": "0"
        }, 
        "week": {
            "longUrlClicks": "0", 
            "shortUrlClicks": "0"
        }
    }, 
    "id": "http://goo.gl/eDcZI", 
    "kind": "urlshortener#url", 
    "longUrl": "http://hayageek.com/google-url-shortener-api/", 
    "status": "OK"
}

Returned Sample Object, if the projection is : ANALYTICS_TOP_STRINGS

{
    "analytics": {
        "allTime": {
            "browsers": [
                {
                    "count": "7", 
                    "id": "Firefox"
                }, 
                {
                    "count": "5", 
                    "id": "Chrome"
                }
            ], 
            "countries": [
                {
                    "count": "5", 
                    "id": "US"
                }, 
                {
                    "count": "2", 
                    "id": "IN"
                }, 
                {
                    "count": "1", 
                    "id": "ES"
                }, 
                {
                    "count": "1", 
                    "id": "FR"
                }, 
                {
                    "count": "1", 
                    "id": "GB"
                }, 
                {
                    "count": "1", 
                    "id": "IT"
                }, 
                {
                    "count": "1", 
                    "id": "PH"
                }
            ], 
            "platforms": [
                {
                    "count": "11", 
                    "id": "Windows"
                }, 
                {
                    "count": "1", 
                    "id": "Other Unix"
                }
            ], 
            "referrers": [
                {
                    "count": "10", 
                    "id": "Unknown/empty"
                }, 
                {
                    "count": "1", 
                    "id": "hayageek.com"
                }, 
                {
                    "count": "1", 
                    "id": "www.urlshortener.me"
                }
            ]
        }, 
        "month": {
            "browsers": [
                {
                    "count": "4", 
                    "id": "Chrome"
                }, 
                {
                    "count": "4", 
                    "id": "Firefox"
                }
            ], 
            "countries": [
                {
                    "count": "4", 
                    "id": "US"
                }, 
                {
                    "count": "1", 
                    "id": "FR"
                }, 
                {
                    "count": "1", 
                    "id": "GB"
                }, 
                {
                    "count": "1", 
                    "id": "IN"
                }, 
                {
                    "count": "1", 
                    "id": "PH"
                }
            ], 
            "platforms": [
                {
                    "count": "7", 
                    "id": "Windows"
                }, 
                {
                    "count": "1", 
                    "id": "Other Unix"
                }
            ], 
            "referrers": [
                {
                    "count": "7", 
                    "id": "Unknown/empty"
                }, 
                {
                    "count": "1", 
                    "id": "www.urlshortener.me"
                }
            ]
        }
    }, 
    "id": "http://goo.gl/eDcZI", 
    "kind": "urlshortener#url", 
    "longUrl": "http://hayageek.com/google-url-shortener-api/", 
    "status": "OK"
}

Reference: Google Documentation