MODX 360

the personal website of garry nutting

Tracking the original Referrer on your site

This is a little snippet/plugin combo I wrote to allow you to track the original referrer on a site. A typical use case for this would be so that you can pass this referrer information via a contact form (where the contact form is perhaps the second or third page a visitor has viewed). Eventually, I'd like to package this up and use property sets but for the project this was created for, this setup worked fine.

The snippet is fairly simple. It currently tracks three items of information (if available) and supports search term detection for the Google, Yahoo and Bing search engines. Whether the search terms are actually passed in the referrer information is really down to the search engines themselves. The three pieces of information tracked are:

  • Referrer Host
  • Referrer search terms (if available)
  • Referrer paid search (if available, detected by a URL parameter passed in the referrer)

The snippet works by processing the referrer information and storing the information in the $_SESSION variable ensuring it's preserved across page requests. Placeholders are also set so it's easy to populate this information into, let's say, a FormIt contact form. Here is the snippet code:

* Set placeholders for referrer information

$paidSearchParam = $modx->getOption('referrer.paidSearchParam', $scriptProperties, $modx->getOption('referrer.PaidSearchParam', null, 'gclid'));
$debug = $modx->getOption('debug', $scriptProperties, false);

/* uncomment line below for testing in cases where HTTP_REFERER is not set */
/* $_SERVER['HTTP_REFERER'] = ';rls=en&q=test&ie=UTF-8&oe=UTF-8'; */

/* reset session if revisiting site from another referer */
if ((isset($_SERVER['HTTP_REFERER']) && isset($_SESSION['page.referer'])) && stripos($_SERVER['HTTP_REFERER'], MODX_SITE_URL) === false) {

if (!isset($_SESSION['site.referer'])) { 
    if (isset($_SERVER['HTTP_REFERER'])) {
        $_SESSION['site.referer'] = array('host' => '', 'searchterm' => '', 'paidsearch' => '');
        $referringPage = parse_url($_SERVER['HTTP_REFERER']);
        $_SESSION['site.referer']['host'] = $referringPage['host'];
        $modx->setPlaceholder('', $referringPage['host']);
        if (stristr($referringPage['host'], 'google.') || stristr($referringPage['host'], 'bing.') || stristr($referringPage['host'], 'yahoo.')) {
            parse_str(htmlspecialchars_decode($referringPage['query']), $queryVars);
            if (stristr($referringPage['host'], 'google.') || stristr($referringPage['host'], 'bing.')) {
                $search = $queryVars['q'];
            } elseif (stristr($referringPage['host'], 'yahoo.')){
                $search = $queryVars['p'];
            } else {
                $search = false;
            if ($search !== false) {
                $_SESSION['site.referer']['searchterm'] = $search;
                $modx->setPlaceholder('referrer.searchterm', $search);                
        $paid = 'No';
        if (isset($_GET[$paidSearchParam])) {
           $paid = 'Yes';
        $_SESSION['site.referer']['paidsearch'] = $paid;
        $modx->setPlaceholder('referrer.paidsearch', $paid);
} else {
    $modx->setPlaceholder('', $_SESSION['site.referer']['host']);
    $modx->setPlaceholder('referrer.searchterm', $_SESSION['site.referer']['searchterm']);
    $modx->setPlaceholder('referrer.paidsearch', $_SESSION['site.referer']['paidsearch']);

if ($debug !== false) {
	Referrer Source: '.$_SERVER['HTTP_REFERER'].'
	Referrer: '.$modx->getPlaceholder('').'
	Search Term: '.$modx->getPlaceholder('referrer.searchterm').'
	Paid Search: '.$modx->getPlaceholder('referrer.paidsearch').'


The two snippet parameters are:

  • &referrer.paidSearchTerm: This is the URL querystring parameter name that is normally passed by the likes of Google to indicate a paid search.
  • &debug: Outputs a HTML comment into your page source so you can see what information has been tracked.

In order to have this run on each page, I created a simple plugin that runs this snippet on the OnWebPageInit event:

$eventName = $modx->event->name;
    case 'OnWebPageInit':

That really is all there is to it!

Comments (0)

Add a Comment

Allowed tags: <b><i><br>Add a new comment: