Google Analytics file tracking 3.06.07, John Einselen

Google Analytics is great at tracking visitors, but file downloads aren’t included in site traffic reports since files links (and links to places outside your site) don’t include the necessary javascript. This hack will change the default function of Textile’s "link(title)":url into a Google Analytics urchin tracker, allowing you to easily add file downloads and external link tracking to your Analytics reports. You can read more about link tracking in Google’s documentation.

Note – this changes the default behavior of the Textile link formating, so any use of the (title) format will be updated from title="" to an Analytics tracked link.

Find the following file and open it in an editor (TextWrangler is excellent).

/textpattern/lib/classTextile.php

Once open, navigate to line 735 and you’ll find (all on one line):

$atts .= ($title != '') ? ' title="' . $this->encode_html($title) . '"' : '';

Change it to read something like this (all one line):

$atts .= ($title != '') ? ' onClick="javascript:urchinTracker (\'/downloads/' . $this->encode_html($title) . '\'); "' : '';

It’s probably a good idea to either backup your original classTextile.php file, or leave a commented copy of the original code (// $atts...) in case something goes sour. Feel free to change downloads/ to any sudo-folder for organization in the Analytics report, or remove it and include the sudo-path in your Textile formatting for each link ("link(downloads/title)":url). If you change the code from what’s listed above, make sure any apostrophes are properly escaped with a backslash (\').

Save the file (uploading it if you downloaded to edit), and you’re done. Creating links with "link(title)":url will now track /downloads/title in your Google reports. The Google Analytics code does need to come before any links on the page, so if you’ve included the Analytics script at the bottom of your page, it’ll need to move to the top (still within the <body> tag, of course).

If you prefer, you can just download the already hacked file here.

« comment
bookmark

Hey, love the site.

I use javascript on “my church’s website”“http://www.xcelchurch.com to replace an image with a google video player. I use this method to play our church news and other videos and was wondering if I can modify this hack to see how many people are viewing the video(s)?

An example of the code is as follows, apologies if any is lost for security reasons:

HTML:

<div> <p id=“churchnews”> <a href=“javascript:churchnews();” title=“Click to watch Church News”>Click to watch Church News<span></span></a> </p>
</div>

JavaScript:

function churchnews() {

document.getElementById(“churchnews”).innerHTML=’<embed style=“width:267px;height:227px;” type=“application/x-shockwave-flash” src=“http://video.google.com/googleplayer.swf?&videoUrl=http%3A%2F%2Fvp.video.google.com%2Fvideodownload%3Fversion%3D0%26secureurl%3DtQAAAIRYPSMBjP1WZO3pnlnfVBN5RCLdFNNjGeVDdm-wsP0Z77o9gXndRHWcuotpfniV6EEOxtjwHCXxWI7wKOtmOWA63ve2KJ-KBrHQnJwzKpWiGQBmwm2mmotYrnJ2X9ZmGOoF0QJIYgrVbH8IQt9K-Y9NCqUDOmTYfryVBqftDBLDenL0C_rZnZ9bFfOuUIy7j2AySbVgF1XfSA0_NvVdKAu-HGjRKsJlxo6CeunWT77McXuGaEv-ueEe61fG2RQNxA%26sigh%3DZedFH1O3UzDNIj8yLHEabY3sM1k%26begin%3D0%26len%3D154960%26docid%3D2357145207785521973&messagesUrl=http%3A%2F%2Fvideo.google.co.uk%2FFlashUiStrings.xlb%3Fframe%3Dflashstrings%26hl%3Den-GB&thumbnailUrl=http%3A%2F%2Fvideo.google.com%2FThumbnailServer2%3Fapp%3Dvss%26contentid%3Db079a1da9f808b9e%26offsetms%3D65000%26itag%3Dw320%26lang%3Den%26sigh%3DJae_yIEbk3dM0Ffi-ObgQczhHQY” id=“VideoPlayback” align=“middle” allowScriptAccess=“always” quality=“best” bgcolor=”#ffffff” scale=“noScale” salign=“TL” FlashVars=“playerMode=normal&autoPlay=true&docid=2357145207785521973&subtitle=on&clickUrl=”></embed>’;

};

posted 31.01.08, by Paul