Plugins You can find out about all the publicly available Grails plugins.

Google URL shortener

  • Tags: /
  • Latest: 0.1
  • Last Updated: 12 January 2011
  • Grails version: 1.3.4 > *
  • Authors: null
0 vote
compile "org.grails.plugins:google-url-shortener:0.1"




grails install-plugin google-url-shortener



Like tinyurl or , Google has also its URL shortener service. Beside shorten URLs it also provides some extra services like providing analytics of shorten URLs. Since 13.01.2011 Google provides an API for this service. This grails plugin wraps this API and povides some convenients methods to shorten your URLs.


After installing the plugin you have 2 options to shorten or expand your URLs :
  • using the shorten() / expand() methods which are dynamically added to the String object :
So you can do something like that :

def shortUrl = myLongUrlString.shorten()
def shortUrtl =params.tweetLink.shorten()
//expanding a link
def longLink = myShortLinkString.expand()
  • using the Shortener service. Methods of this service, Instead of returning a String , will retrieve an UrlResource instance that contains more information. The service also provides an extra method : getAnalytics() that will retrieves analytics of your shorten URL. The structure of the analytics data follows the API specification (except some property naming convention that was not supported by Grails (id property) or Hibernate (count property) and is made available through Domain classes.
The analytics data structure of the API can be found here :

def shortenerService

def linkHandler = { def urlResource = shortenerService.shorten(params.myLongLink) def shortLink = urlResource.shortUrl }

def analyseLink = { def analyticsUrlResource = shortenerService.getAnalytics(params.shortLink) int monthlyClicks = analyticsUrlResource.month.shortUrlClicks //etc check the API reference to see all the info you can retrieve }

API key

Having an API key is optionnal for this service but Google advice to get one, it's free and easy to get one, see this link

When you get your key you can add to the Config.groovy like that :


Each service call will then use your key in the request

Exception handling

If you provide a wrong URL (typo or long url instead of shortUrl) the plugin will throw a HttpResponseException, the plugin do not catch this exception so your application can take care of it