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.

Step 1

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. Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).

1
2
3
4
5
6
7
8
9
10
11
12
13
function my_set_external_referrer()
{
    if (!isset($_SESSION['external_referrer'])) {
        $_SESSION['external_referrer'] = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Not set';
    }
}
add_action('wp_loaded', 'my_set_external_referrer');
 
function my_get_external_referrer()
{
    return $_SESSION['external_referrer'];
}
add_filter('iphorm_element_value_external_referrer', 'my_get_external_referrer');
function my_set_external_referrer()
{
    if (!isset($_SESSION['external_referrer'])) {
        $_SESSION['external_referrer'] = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Not set';
    }
}
add_action('wp_loaded', 'my_set_external_referrer');

function my_get_external_referrer()
{
    return $_SESSION['external_referrer'];
}
add_filter('iphorm_element_value_external_referrer', 'my_get_external_referrer');

This code will store the referring URL into a PHP session, and set it up to be retrieved by Quform.

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”.

Step 2

In the Form Builder, add a Hidden field to the form. In the Settings for it, give it a Label such as “External referrer” and tick Dynamic default value and set the Parameter name to external_referrer

Repeat Step 2 for any other forms you want to track the External Referrer URL.

Still having trouble? Head over to the forums.

Forums