I'm using this method to get the referrer when somebody submits the email but I have the same issue like somebody else on that forum. The method works well except:
<blockquote>when the person comes directly to the form from an outside url the code works great. but, when they come to the site and travel a few pages, the referring url seems to be lost.</blockquote>
[[LINK href="http://tonykwon.com/2010/09/can-we-capture-http-referer-information-upon-form-submission-using-contact-form-7/"]]http://tonykwon.com/2010/09/can-we-capture-http-referer-information-upon-form-submission-using-contact-form-7/[[/LINK]]
Any Ideas how can I fix that?
Utkarsh Kukreti answers:
Try this:
add_action('init', 'assign_referer_session');
function assign_referer_session() {
$key = '_wpcf7_referer';
if(!isset($_SESSION[$key]) || empty($_SESSION[$key])) {
$_SESSION[$key] = $_SERVER['HTTP_REFERER'];
}
}
function getRefererPage($form_tag)
{
if ($form_tag['name'] == 'referer-page') {
$form_tag['values'][] = $_SESSION['_wpcf7_referer'];
}
return $form_tag;
}
if (!is_admin()) {
add_filter('wpcf7_form_tag', 'getRefererPage');
}
This code will save the referer of the first visit of a user into the session variable, and use that for any form submissions that happen in the same session.
Lucian Florian comments:
It seems it doesn't work. I googled the site, navigated on few separated pages then submitted the form. It still shows the last page I've been.
Utkarsh Kukreti comments:
Could you link me to the site? (or PM)
Lucian Florian comments:
I could probably send you a PM with the link, but I cannot give you access in the back-end.
Utkarsh Kukreti comments:
Okay, that should probably be enough to test once.
Lucian Florian comments:
It didn't get any value.
Utkarsh Kukreti comments:
Did you get my last PM? (Sent minutes after your PM).
Lucian Florian comments:
Yes, so no value for that field.
Referer Page:
Gabriel Reguly answers:
You should set the transient value at arrival time and then retrieve it when sending the email.
I can do it for you for $30.
Utkarsh Kukreti has it almost correctly, but uses server variables and sessions which are not very 'robust'.
Lucian Florian comments:
I increased it to $30.
Gabriel Reguly comments:
Ok, working on it.
Please bear with me.
Gabriel Reguly comments:
Thanks for your patience, please try the following code (at functions.php)
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' );
}
Lucian Florian comments:
thanks I ran a test and I think it works. Let me have the client test it and then if all is good I will give you the vote.
Gabriel Reguly comments:
Ok, thanks for the reply.
Lucian Florian comments:
I made few more tests and it seems it works to a certain extent. Can you explain which session is being considered?
For example I searched the site in Bing, navigated few pages and submitted the form. It showed correctly I'm coming from Bing.
When I opened another browser, added the link in another site and clicked on it, after submitting the form it showed me as referral still the Bing site instead if the site I was coming from.
Is any workaround for this?
Gabriel Reguly comments:
It happens because the code uses your IP as an unique identifier, which is used to store/retrieve the referer.
Do you really expect people to use 2 different browsers to see the same content?
As a workaround I would suggest you add the $_SERVER['HTTP_USER_AGENT'].
Please see changed code below.
function getRefererPage( $form_tag ) {
if ( $form_tag['name'] == 'referer-page' ) {
$uniqueID = getIP() . $_SERVER['HTTP_USER_AGENT'];
setRefererTransient( $uniqueID );
$form_tag['values'][] = get_transient( $uniqueID );
}
return $form_tag;
}
Lucian Florian comments:
Thanks, but I think I will leave it like that. It works well.
I'll try the updated code if I need more fin tuning.
thanks for your help!
Lucian Florian comments:
I'm not sure what happened but it does not works any longer.
The client got latest 3 requests with no referral link and when I tested it on my site, it doesn't shows the google as referral page, but the last page visited.
Is it possible to be tested it more?