logging yer 856’s!

A few people have emailed me for more information about how to log clicks on the external 856 links in HIP, so here’s how to do it!
Requirements:

  • a web server on which you can run a script (e.g. Perl or PHP)
  • HIP 2 or 3

The 856 links in HIP are built from the MARC map in Horizon, so the first step is tweak it so that it prepends the URL of a logging script in front of the actual 856 URL.  Here’s what our 856 MARC $u map looks like (split across several lines for readability):

<a class="smallAnchor"
    href="https://library.hud.ac.uk/cgi-bin/cataloguelink.pl?$_">
    {<img src="$9"{ alt="$y"}>|$z|$y|$_}
    </a>

To view the MARC map without line breaks (i.e. as you’d enter it into Horizon), click here.
Please remember to make a note of the original 856 MARC map value – you might need it!
The https://library.hud.ac.uk/cgi-bin/cataloguelink.pl bit will need to be changed to the location and name of your logging script.
My first version of the logging script simply logged the URL and the time, as I couldn’t figure out a way of capturing more useful info (such as the bib number or book title).
So, I brewed myself an extra strong coffee and sneaked up on the problem from a different angle!

Here’s a souped-up caffeine-enhanced Perl logging script, which attempts to get the bib number and book title:
http://www.daveyp.com/files/stuff/856/cataloguelink.pl
You’ll need to pop it on your web server and make a few edits to the script:

  1. change the value of $hip to match the name (or IP address if you don’t use a name) of your HIP server
  2. change the value of $log if you’d prefer to have the log file stored somewhere else

The script checks to see if the web browser passed a HTTP_REFERER value (which should be the URL of the HIP page with the 856 link).  If it did, then the script tries to fetch the XML version of the HIP page.  If that works, then it grabs the bib number and the title from the XML.
The log file is tab delimited and contains:

  • time stamp
  • bib number
  • title
  • 856 URL

The bib number and title are reliant on the HTTP_REFERER and being able to fetch the XML version of the HIP page.
You can see a sample log file here.  The last line in the file shows you what happens if the HTTP_REFERER wasn’t passed by the web browser.
If you’d prefer to get a CSV (comma separated) file, then change the relevant lines in the script to something like:

open( OUT, ">>$log" );
$ttl =~ s/"//g;
$url =~ s/"/%22/g;
print OUT '"'.localtime(time).qq(",$bib,"$ttl","$url"n);
close( OUT );

WARNING!
Some of you may have already spotted the weak link in all of the above — if the web server running the logging script fails, then none of your 856 links will work!
So, make sure your logging script is running on a high-availability server.  If the server isn’t available, then the quickest way to fix things is to revert the 856 MARC map back to the original value.
If my recollection is right, then HIP automatically picks up any changes to the 856 MARC map so there’s no need to reboot/restart HIP.

5 thoughts on “logging yer 856’s!”

  1. Ach, are ye mad? Don’t modify your 856, use XSLT to change the link just before it hits the browser. That way your MARC record is accurate (and you can check your 856 links with a link checker without adjusting your stats), but you can still track the outbound links. And you can run cataloguelink.pl on port 81 on Apache on your HIP server, so there are fewer availability problems.

  2. Hi Ben
    Changing it in the XSLT was my first thought, but the entire link (with it’s href, etc) is being delivered to the stylesheet directly from Horizon.
    However, I guess it must be possible to do some fancy XSL regular expression to sneak the URL of the logging script into HTML supplied by a standard Horizon MARC map?

Comments are closed.