How to include recent referrers through Movable Type

Posted .

UPDATE: This stopped working properly after I upgraded to MT 3.1, so I’m no longer doing it this way.

I looked for a plugin to do this, and I couldn’t really find something that didn’t require access to AWStats on the server. (At the time, I didn’t have access to it, and now that I do, I haven’t bothered to figure out how to make it work.) So, combining the TrackBack ping method used to generate the Winamp playlist (see A Whole Lotta Nothing) and some Javascript, I made my own solution. The final touch of swapping in friendly names for common referrers is done with the Regex plugin for Movable Type.



Here are the details:



1. In Movable Type, set up a new category to track referrers. (I called my category “referer,” but it doesn’t matter what the category is called.)



2. Edit the category attributes so that it can accept incoming TrackBack pings.



3. Note the number at the end of the TrackBack URL for the category, because you’ll need it later.



4. Add the following JavaScript to any page you want to have track referrers. (Note: All pages are tracked together, so you get sitewide referrers.)




(If you want, you can put this JavaScript into a separate file and call it in.)



Steps 1-4 allow the tracking of referrers. Now, you need to set things up to show the referrers.



The simplest method is to include this anywhere you want to show the list of referrers (You could also create a Movable Type index template with the code for displaying the referrers, and then include it through the MTInclude tag.):







You can change “lastn” to however many you want to show. “trim_to” is used to only show the first 30 characters of the referrer. You can set it to whatever you want, or eliminate it altogether.



Now, if you’re like me, most of your referrals come from a limited number of places. So you can use the Regex plugin to give a friendly label to those referrers. Here’s how:



1. Make sure you have the Regex plugin installed.



2. Add regex=”1″ to the second MTPingExcerpt tag (the one that is not part of the href).



3. Before the code above, create regular expressions for each of your common referrers, like this:

(That last one catches some strange cases where something doesn’t execute the JavaScript and follows a URL in the JavaScript.) You can add more as time goes on and you see what your common referrers are.



There you go! Hope it works for you.



UPDATE: Rachel suggested that it would be nice to be able to show what the search terms were when the referrer is a search engine. Here are the regexes to do that (They should replace the search-engine regexes above):




Because the search terms are URL-encoded, you will need to decode them if you want them too look nice.

Strangely enough, Movable Type has a filter to URL-encode, but not to decode. So I created a plugin to add that feature. You can get that plugin here. Once you’ve installed that, you’ll also need to add decode_url="1" to the second MTPingExcerpt tage (the same one with the regex="1".)