17
2008
Gravl: Referers, Geocoding, Charts
Continuing my obsession with the power of Grails filters, it was time to add some referer tracking and ipaddress Geocoding to Gravl to see where people are coming from. So I created a new RefererFilters class in conf and I was up and running. I already had a little CacheService happening, so I created a new 24 hour cache, scooped up some interesting browser header data, and squirrelled it away for later:
// Scoop up Referer and User-Agent and remote IP
def userAgent = request.getHeader("User-Agent")
def referer = request.getHeader("Referer")
def ip = request.getRemoteAddr()
// Grails adds request.forwardURI to keep the orig URI
def url = request.forwardURI
// put in the cache, not use of applicationContext to lookup service bean
CacheService cacheService = applicationContext.getBean('cacheService')
cacheService.putToCache("referers", 60*60*24, Calendar.getInstance(),
[ userAgent: userAgent, referer: referer, ip: ip, url: url ])
One thing to notice in the above, service injections don’t happen in the filter chain, but you are provided with an applicationContext that you can use to look up your services by name.
Anyways, now I’ve got some stats, it’s time to do some crunching… Referers was the first target to add to my stats page… though the css needs some work to fit the counters in
Now what to do with those ip addresses I was gathering? Picking up on the stuff Simon is doing in the latest Pebble, I thought it might be nice to add some geocoding to that referers list so I can see which country my traffic is coming from.
Turns out there’s a very simple little Java library available from MaxMind called GeoLite Countries. I mavenized the source into a jar, then dropped the resources file into my /src/java/resources. I already had the ip addresses I’d gathered from headers into my cache, so to convert them to a country name, it was time to create a CountryLookupService to wrap all the calls to the GeoLite library:
class CountryLookupService {
LookupService lookupService
private LookupService getLookupService() {
if (lookupService == null) {
// assume you put your data file in /src/java/resources
def url = this.class.getResource("/resources/geoIp/GeoIP.dat")
log.debug ("Looking for GeoIP database at: $url")
lookupService = new LookupService(new File(url.toURI()),LookupService.GEOIP_MEMORY_CACHE);
}
return lookupService
}
def getCountryName(String ipAddress) {
log.debug "Looking up address for ${ipAddress}"
return getLookupService().getCountry(ipAddress).getName()
}
}
And we’re off and running. The GeoCoding API requires you to supply a current GeoIP.dat file, and the sample above shows you how to load it from your Grails /src/java/resources folder to keep everything all relative. I’m not sure where the agreed convention is on resource placement in Grails, but there is as good as any for now… After that I just needed to iterate my cache, and we have ourselves a hashmap of countryname to hitcount:
Once you’ve crunched all that data, it’d be nice to produce some slick charts to make it all digestable. And what quicker way to chart it than with James Williams uber cool Google Charts Plugin.
If you haven’t used Google Charts before, the idea is just so good and so simple. Create an img tag with the src that points off to http://chart.apis.google.com/chart? then add url params to tell it your chart size, type, title, etc. and Google will render a png on the fly for you.
James plugin makes that process very simple and painless. I take my hashmap of countries (which carries countryName -> hitCount) and feed it straight into his taglib:
<g:pieChart type="3d" title='Hits By Country' size="${[700,300]}"
labels="${countries.keySet()}" dataType='simple' data='${countries.values().asList()}' />
And the result?
So perhaps a pie chart might not be the best selection…
Wow.. that was all quite an adventure in Referers, Geocoding, and Charting! I’ll be talking about some of these adventures at G2X, so if you’re in North America next month, make sure you drop over and say hi!
Happy Grails Stats crunching!
3 Comments + Add Comment
Leave a comment
Glen Smith
Archives
- January 2012
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
- March 2004
- February 2004
- January 2004
- December 2003
- November 2003
- October 2003
- September 2003

An article by Glen





It’s getting groovier and groovier
As far as i know, the geoLite database is updated every month, so how about scheduling a job using the Quartz plugin to update the database every month on the appropriate day?
Keep up the good work, I’m regularly reading your blog and always enjoy the little tips, suggestions and ideas you come up with (or implement as inspired from other sources).
Just to report a bug: when you create a comment via this nifty AJAX functionality, the comment counter is not updated…