# Blosxom Plugin: clicktrack # Author: Nelson Minar # Version: 20030301 # http://www.nelson.monkey.org/~nelson/weblog/ # License: Public Domain # Thanks to Todd Larason http://molelog.molehill.org/blox/ # The clicktrack plugin helps you track clicks to offsite links. # It does two things: # # The start method looks for urls of the form blosxom.cgi?redirectURL=... # If this URL is present it overrides Blosxom's normal blog behaviour # and instead tells the browser to redirect to the specified URL. # # The story method scans your stories for offsite URLs and replaces # them with links to the blosxom redirect URL. The links have # a bit of Javascript to keep the status window tidy. Example: # Todd # # To install simply drop it in your plugins dir. This script wants to # run first, so in most cases you should name it "00clicktrack". I encourage # you to disclose you're running this on your blog - let users know! # # Possible features: # Log the clicks. I find it simpler to just look at my access.log # Option to set a cookie to opt out of this tracking package clicktrack; use CGI qw/:standard/; $query; $debug = ''; sub start { # If the query contains redirectURL, override Blosxom and send the 302 $query = new CGI; if ($url = $query->param('redirectURL')) { print "Status: 302 Moved Temporarily\r\n", "Location: $url\r\n", "Content-Type: text/html\r\n\r\n", "$url\r\n"; exit 0; } 1; } sub rewriteHref { my ($tag) = @_; $debug .= "Tag: " . CGI::escapeHTML($tag) . "
\n"; # Extract the href (skip if we don't find one) if (!($tag =~ m/^(.*)href="([^"]+)"(.*)$/is)) { return $tag; } $prehref = $1; $href = $2; $posthref = $3; $debug .= "Href: " . $href . "
\n"; $debug .= "Pre: " . CGI::escapeHTML($prehref) . "
\n"; $debug .= "Post: " . CGI::escapeHTML($posthref) . "
\n"; # Check that the href goes to an offsite http URL return $tag if !($href =~ m/^http/i); # Rewrite the link to include our own clicktrack URL return $prehref . "href=\"" . $query->url() . "?redirectURL=" . CGI::escape($href) . "\" " . "onMouseOver=\"window.status='to " . $href . "'; return true;\" " . "onMouseOut=\"window.status=''; return true;\"" . "$posthref"; } # Modify the body of stories by rewriting offsite hrefs to our own redir sub story { my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_; # Rewrite any tag in the post body $$body_ref =~ s/(]+>)/rewriteHref($1)/geis; } # Only needed for debug output # sub end { # print $debug; # 1; # } 1;