referer-parser now with Java, Scala and Python support

02 January 2013  •  Alex Dean

Happy New Year all! It’s been three months since we introduced our Attlib project, now renamed to referer-parser, and we are pleased to announce that referer-parser is now available in three additional languages: Java, Scala and Python.

To recap: referer-parser is a simple library for extracting seach marketing attribution data from referer (sic) URLs. You supply referer-parser with a referer URL; it then tells you whether the URL is from a search engine - and if so, which search engine it is, and what keywords the user supplied to arrive at your page.

Huge thanks to Don Spaulding @ Mirus Research for contributing the Python port of referer-parser; the Java/Scala port was developed by us in-house and it will be a key addition to our Snowplow ETL process in the coming months.

You can checkout the code on GitHub, in the referer-parser repository, or read on below the fold for some code examples in the new languages:

Python

To use referer-parser from a Python script:

from referer_parser import Referer

referer_url = 'http://www.google.com/search?q=gateway+oracle+cards+denise+linn&hl=en&client=safari'

r = Referer(referer_url)

print(r.known)              # True
print(r.referer)            # 'Google'
print(r.search_parameter)   # 'q'     
print(r.search_term)        # 'gateway oracle cards denise linn'
print(r.uri)                # ParseResult(scheme='http', netloc='www.google.com', path='/search', params='', query='q=gateway+oracle+cards+denise+linn&hl=en&client=safari', fragment='')

For more information, please see the Python README.

Scala

To use referer-parser from a Scala app:

val refererUrl = "http://www.google.com/search?q=gateway+oracle+cards+denise+linn&hl=en&client=safari"

import com.snowplowanalytics.refererparser.scala.Parser
for (r <- Parser.parse(refererUrl)) {
  println(r.referer.name)      // => "Google"
  for (s <- r.search) {
    println(s.term)            // => "gateway oracle cards denise linn"
    println(s.parameter)       // => "q"    
  }
}

For more information, please see the Java/Scala README.

Usage: Java

To use referer-parser from a Java program:

import com.snowplowanalytics.refererparser.Parser;

...

  String refererUrl = "http://www.google.com/search?q=gateway+oracle+cards+denise+linn&hl=en&client=safari";

  Parser refererParser = new Parser();
  Referal r = refererParser.parse(refererUrl);

  System.out.println(r.referer.name);       // => "Google"

  System.out.println(r.search.parameter);   // => "q"    

  System.out.println(r.search.term);        // => "gateway oracle cards denise linn"

For more information, please see the Java/Scala README.

Getting help

That’s it! If you have any problems with the new versions of referer-parser, please raise an issue or get in touch with us via the usual channels.

And do let us know if you find referer-parser useful!