Referral capture form?

15 posts by 4 authors in: Forums > CMS Builder
Last Post: November 9, 2015   (RSS)

By Damon - October 26, 2015

Hi Tim,

With either a form-to-email or form-to-cmsb you could pass in the referrer to a hidden form field by adding the referrer to the link as you suggested:
http://domain.com.au/capture.php?referrer=xyz

As for cookies, you just need to first work out all the logic:
1. Check if cookie exists, if it does, then pass it into form in hidden field
2. If cookie doesn't exist (first time to capture form), create cookie and pass referrer into hidden field

Is this something you want help with through consulting or just general suggestions?

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Mikey - October 26, 2015

Sounds interesting... please post up what you figure out. I'll do a bit of research as well and see if I can figure out a solution to share too.

By Mikey - October 26, 2015

These are just bits of code and pages they were sourced from stackoverflow.com, which might be a good place to start compiling them into something that functions. Keep in mind, this is just code for reference that may be a place to start building from. I can't say it'll work as I haven't tested any of this as of yet. The bits of code will most likely need to be reworked a bit to function with CMSB as well.

Source: http://stackoverflow.com/questions/1864583/get-original-url-referer-with-php/1864587#1864587

session_start();

if ( !isset( $_SESSION["origURL"] ) )
    $_SESSION["origURL"] = $_SERVER["HTTP_REFERER"];

Source: http://stackoverflow.com/questions/13856915/contact-form-including-referral-of-previous-page?answertab=votes#tab-top

Put a hidden input field into your contact form, that contains the referer:

<form ...>
...
<input type="hidden" value="<?php echo $_SERVER['HTTP_REFERER'] ?>" name="referer" />
...
</form>

Then you can access it with $_POST['referee'] when sending the email.

Zick

By Toledoh - October 26, 2015

Thanks Damon.

If easy to create a step-by-step, then that would be great for the forum - however if it's more complex than that, then I'm happy to progress through consulting.  You guys know my level of skill by now:)

Cheers,

Tim (toledoh.com.au)

By Dave - October 28, 2015

Hey Tim, 

Try this (attached as well): 

<?php // referrerCapture.php
  $referrer = getsetReferrer(); 

// call this at the top of your script before you send any output, like this:
// $referrerCode = getsetReferrer();
function getsetReferrer() {
  $fieldname = 'referrer';

  // get referer code
  if     (isset($_REQUEST[$fieldname])) { $referrerCode = $_REQUEST[$fieldname]; }
  elseif (isset($_COOKIE[$fieldname]))  { $referrerCode = $_COOKIE[$fieldname]; }
  else                                  { $referrerCode = '';  }

  // save it to a cookie (if value changed)
  if ($referrerCode && $referrerCode != @$_COOKIE[$fieldname]) {
    $cookieExpires = strtotime("+10 years");
    $cookiePath    = '/';
    setcookie($fieldname, $referrerCode, $cookieExpires, $cookiePath);
  }

  //
  return $referrerCode;
}

?>

<form method="get" action="?">
Referer: <input type="text" name="referrer" value="<?php echo htmlspecialchars($referrer); ?>" disabled="disabled" /> (you could make this a hidden field)<br/>
<input type="submit" name="submitForm" value=" Submit Form " />
</form>

Examples: <br/>
<a href="?referrer=<?php echo urlencode('ABC Corp'); ?>">Pass in a referrer</a><br/>
<a href="?">Load page without any query string (loads last referrer from cookie)</a><br/>
<a href="?referrer=<?php echo urlencode('Widgets, Inc.'); ?>">Pass in a different referrer</a><br/>

Dave Edis - Senior Developer
interactivetools.com
Attachments:

referrerCapture.php 2K

By Toledoh - October 28, 2015

That looks great - thanks Dave!

Cheers,

Tim (toledoh.com.au)

By Mikey - October 28, 2015 - edited: October 28, 2015

NICE! Thanks Dave.

I added a little extra code to this to capture a refereeing domain name is nothing else exist in the link.

<?php // referrerCapture.php
  $referrer = getsetReferrer(); 

// call this at the top of your script before you send any output, like this:
// $referrerCode = getsetReferrer();
function getsetReferrer() {
  $fieldname = 'referrer';
  // http://php.net/manual/en/function.parse-url.php
  // get referer code
  if     (isset($_REQUEST[$fieldname])) { $referrerCode = $_REQUEST[$fieldname]; }
  elseif (isset($_COOKIE[$fieldname]))  { $referrerCode = $_COOKIE[$fieldname]; }
  // get referer domain name only
  else                                  { $referrerCode = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); } 
  // get referer full domain and path
  //else                                  { $referrerCode = $_SERVER['HTTP_REFERER']; }
  //else                                  { $referrerCode = '';  }

  // save it to a cookie (if value changed)
  if ($referrerCode && $referrerCode != @$_COOKIE[$fieldname]) {
    $cookieExpires = strtotime("+10 years");
    $cookiePath    = '/';
    setcookie($fieldname, $referrerCode, $cookieExpires, $cookiePath);
  }

  //
  return $referrerCode;
}
?>

By Toledoh - October 29, 2015

Hey Dave.

I've got that working here: http://ntaaustralia.com.au/information/thinking-studying-nutritional-therapy/

I've I use the URL: http://ntaaustralia.com.au/information/thinking-studying-nutritional-therapy/?referrer=XYZ - that referrer is captured

However, If I got to http://ntaaustralia.com.au/information/thinking-studying-nutritional-therapy/?referrer=ABC - then ABC is captured as the referrer, where I was expecting that it would stay as XYZ? as that was the first person to refer, and they should be tracked.

Also.  Should we use $referrer or $referrerCode <input type="text" name="referrer" value="<?php echo htmlspecialchars($referrer); ?>" disabled="disabled" />

Cheers,

Tim (toledoh.com.au)

By Dave - November 4, 2015

Hi Tim, 

I've I use the URL: http://ntaaustralia.com.au/information/thinking-studying-nutritional-therapy/?referrer=XYZ - that referrer is captured

However, If I got to http://ntaaustralia.com.au/information/thinking-studying-nutritional-therapy/?referrer=ABC - then ABC is captured as the referrer, where I was expecting that it would stay as XYZ? as that was the first person to refer, and they should be tracked.

No problem, just swap the first two lines so it checks for the existing cookie first: 

  if     (isset($_COOKIE[$fieldname]))  { $referrerCode = $_COOKIE[$fieldname]; }
  elseif (isset($_REQUEST[$fieldname])) { $referrerCode = $_REQUEST[$fieldname]; }

Also.  Should we use $referrer or $referrerCode <input type="text" name="referrer" value="<?php echo htmlspecialchars($referrer); ?>" disabled="disabled" />

Yea, that is confusing.  $referrerCode is only "in scope" (meaning defined) within the function.  So the way it was written in my original post you'd use $referrer.  But to make it simpler you could use the same variable name here: 

$referrerCode = getsetReferrer(); 

And just use $referrerCode everywhere.

Hope that helps!  Let me know any other questions.

Dave Edis - Senior Developer
interactivetools.com