Skip to content

Instantly share code, notes, and snippets.

@aaronfischer
Last active April 22, 2019 09:52
Show Gist options
  • Save aaronfischer/8961255 to your computer and use it in GitHub Desktop.
Save aaronfischer/8961255 to your computer and use it in GitHub Desktop.
Lead Form Tracking
/*-------------------------------------------------------------------
*
* TRACK SOURCE WITH A COOKIE
* This will check if the query and save its values into a cookie for 180 days and will only be removed if they clear their cookies or if they visit via another source query
* ex. http://rmcad.edu/event/rmcad-create-day-0?utm_source=Social&utm_medium=Twitter&utm_campaign=CreateDay2.15
* utm_source = Social
* utm_medium = Twitter
* utm_campaign = CreateDay2.15
*
-------------------------------------------------------------------*/
if (sourcecode = $.parseQuery().utm_source) {
$.cookie("lead_source_form", sourcecode, { expires: 180, path: '/' });
}
$(".utm_source").val($.cookie("lead_source_form"));
if (mediumcode = $.parseQuery().utm_medium) {
$.cookie("lead_medium_form", mediumcode, { expires: 180, path: '/' });
}
$(".utm_medium").val($.cookie("lead_medium_form"));
if (campaigncode = $.parseQuery().utm_campaign) {
$.cookie("lead_campaign_form", campaigncode, { expires: 180, path: '/' });
}
$(".utm_campaign").val($.cookie("lead_campaign_form"));
<!-- include these additional plugins -->
<script type="text/javascript" src="../js/jquery.cookie.js"></script>
<script type="text/javascript" src="../js/jquery.parsequery.min.js"></script>
<form>
<!-- rest of form elements -->
<input class="utm_source" name="utm_source" type="hidden" value="" />
<input class="utm_medium" name="utm_medium" type="hidden" value="" />
<input class="utm_campaign" name="utm_campaign" type="hidden" value="" />
</form>
@jimpowelltech
Copy link

For the quick and dirty procedural PHP version:

<?php 
function fromCookie($key) {
 $value = isset($_GET[$key]) ? $_GET[$key] : ""
  if (!isset($_SESSION[$key] && $value) {
    $_SESSION[$key] = $value;
  }  
  return $value;
}
?>

<form>
  <!-- rest of form elements -->
  <input class="utm_source" name="utm_source" type="hidden" value="<?php echo fromCookie("utm_source"); ?>" />
  <input class="utm_medium" name="utm_medium" type="hidden" value="<?php echo fromCookie("utm_medium"); ?>" />
  <input class="utm_campaign" name="utm_campaign" type="hidden" value="<?php echo fromCookie("utm_campaign"); ?>" />
</form>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment