Steps for showing the referral URL in your email

The initial settings are based on the following blog post: Can we capture HTTP REFERER information upon form submission using Contact Form 7?, however that method will allow you track only visitors who go directly to the contact page, where the form is. If the visitor comes from Google, browses the site, the referral will show the last page within your site, not initial Google link.

Contact Form 7 initial settings

We will follow the same initial steps like Tony mentioned in his blog post:

Here is a screenshot that explains better what happens in Contact form 7 settings:

Improved tracking based on visitor IP

In order to make it all work we need to add the following code in the function.php within your theme folder.

	//--------------- Referer code for contact form 7
function getIP() {
	$sProxy = '';
	if ( getenv( 'HTTP_CLIENT_IP' ) ) {
		$sProxy = $_SERVER['REMOTE_ADDR'];
		$sIP    = getenv( 'HTTP_CLIENT_IP' ) ;
	} elseif( $_SERVER['HTTP_X_FORWARDED_FOR'] ) {
		$sProxy = $_SERVER['REMOTE_ADDR'];
		$sIP    = $_SERVER['HTTP_X_FORWARDED_FOR'];
	} else {
		$sIP    = $_SERVER['REMOTE_ADDR'];
	}
	if ( ! empty( $sProxy ) ) {
		$sIP = $sIP . 'via-proxy:' . $sProxy;
	}
	return $sIP;
}
function setRefererTransient( $uniqueID ) {
	if ( false === ( $void = get_transient( $uniqueID ) ) ) {
		// set a transient for 2 hours
		set_transient( $uniqueID, $_SERVER['HTTP_REFERER'], 60*60*2 );
	}
}
function getRefererPage( $form_tag ) {
	if ( $form_tag['name'] == 'referer-page' ) {
		$uniqueID = getIP();
		setRefererTransient( $uniqueID );
		$form_tag['values'][] =  get_transient( $uniqueID );
	}
	return $form_tag;
}
if ( !is_admin() ) {
	add_filter( 'wpcf7_form_tag', 'getRefererPage' );
}

The code will store the referral page at the moment the customer comes on the site for 2 hours. If they submit the form within this time, no matter how many pages the visited on the site, it will still show the initial referral.

The final result

After somebody submits the message through contact form, this is how it should look like in your email client:

Other notes:

No tracking method, including Google Analytics is not 100% precise, as there are people with JS disabled, with cookies turned off, people who actually cannot be tracked using standard methods.
Same with this method, at times you might receive emails with a blank value for referral part. However the method works well for most time, enough to get real time feedback for your marketing efforts.

9 Responses

  1. Hi,
    This is the perfect thing!… but I’m having a problem: When I test it, starting with a Google search to find the website then click on the page that has my contact form, it shows my website as the referrer rather than Google. Any ideas?

  2. Make it sooo much easier on yourself and just put style=”display:none;” in the form itself so you don’t have cross-browser compatability issues.
    (per example
    [text referer-page class:referer-page]

  3. Great blog post, thanks. I used the display:none version in the form though.
    Keep it up.
    David

  4. I just installed this, but it’s giving me what appears to be either the original landing page, or the page from which the user clicked to get to the page with the contact form -but it’s definitely not giving me the referrer to my site. Any idea what’s going on?

  5. Hi, Thank you for this code, but it doesn’t work form me:( It is strange because when I have added it it was working quite right showing my sub pages urls(and only sometimes those pages were wrong), but now it stop working at all… Do you have any advice how to fix it? Many thanks!

  6. For those who want to invest in this method I recommend checking out wpquestions.com where you can get help from world wide experts on this issue and could find ways to improve the code.

  7. How could I store this value permanently? I am going to start by adjusting this setting to 24 hours and see what happens:
    set_transient( $uniqueID, $_SERVER[‘HTTP_REFERER’], 60*60*24 );

Comments are closed.