Referrer tracking

You can track the page that the user was on before visiting the form page by adding a Hidden field to the form and at the Default value option choosing Insert variable… then Referring URL. However if you wish to track what site the user was on before arriving on your site (let’s call it the External Referrer) you will need a different method, and this is what this guide is about. Say they visit a couple of pages on your site before submitting the form, the above method would not show the External Referrer URL, it would show the last page they were visiting on your site. So to track the External Referrer we will add some code to save the referring URL the first time that the user visits any page on your site.

Step 1

Add a Hidden element to the form.

Add a hidden element to the form

In the Settings for it, give it a Label such as “External referrer” and on the Data tab turn on the Dynamic default value option and at the Parameter name field enter referrer.

Enable Dynamic default value with Parameter name referrer

Repeat this step for any other forms where you want to track the External Referrer URL.

Step 2

Add the following code to the WordPress theme functions.php file (or create a plugin for it).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
add_action('wp_loaded', function () {
    $session = Quform::getService('session');
 
    if ( ! $session->has('referrer')) {
        $referrer = ! empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Not set';
        $session->set('referrer', $referrer);
    }
});
 
add_filter('quform_element_value_referrer', function ($value) {
    $session = Quform::getService('session');
 
    if ($session->has('referrer')) {
        $value = $session->get('referrer');
    }
 
    return $value;
});
add_action('wp_loaded', function () {
    $session = Quform::getService('session');

    if ( ! $session->has('referrer')) {
        $referrer = ! empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Not set';
        $session->set('referrer', $referrer);
    }
});

add_filter('quform_element_value_referrer', function ($value) {
    $session = Quform::getService('session');

    if ($session->has('referrer')) {
        $value = $session->get('referrer');
    }

    return $value;
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function my_set_external_referrer()
{
    $session = Quform::getService('session');
 
    if ( ! $session->has('referrer')) {
        $referrer = ! empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Not set';
        $session->set('referrer', $referrer);
    }
}
add_action('wp_loaded', 'my_set_external_referrer');
 
function my_get_external_referrer($value)
{
    $session = Quform::getService('session');
 
    if ($session->has('referrer')) {
        $value = $session->get('referrer');
    }
 
    return $value;
}
add_filter('quform_element_value_referrer', 'my_get_external_referrer');
function my_set_external_referrer()
{
    $session = Quform::getService('session');

    if ( ! $session->has('referrer')) {
        $referrer = ! empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Not set';
        $session->set('referrer', $referrer);
    }
}
add_action('wp_loaded', 'my_set_external_referrer');

function my_get_external_referrer($value)
{
    $session = Quform::getService('session');

    if ($session->has('referrer')) {
        $value = $session->get('referrer');
    }

    return $value;
}
add_filter('quform_element_value_referrer', 'my_get_external_referrer');

The referring URL will be empty in certain circumstances:

  • The user types in your site URL directly, or visits from a bookmark
  • If your site does not have an SSL certificate (the URL starts with http:// and not https://) then the referrering URL will be empty from users arriving from https:// sites, such as Google Search
  • The referring URL is deliberately blanked out by redirectors

If the referring URL is determined to be empty, the code will set it to “Not set”.

Be inspired. © 2018 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy