Hey Guys,
Epikone had a great post on their blog about passing Google Analytics Cookie data into a form.
Analytics Talk Blog Archive UPDATED: Integrating Google Analytics with a CRM
While this isn't applicable to most AM work, it's definitely something I find useful.
Basically, once implemented correctly, it will pass the Google analytics cookie data into your' contact form, but hidden. When the user presses submit, you get the data automatically.
Sweet!
However, my contact form doesn't seem to be passing the variables correctly.
It is giving me blank data where the google analytics should be.
I've stripped the contact form page + the php contact form code and placed it below. It's also in a zip in case anyone wants to play around with it.
Relevant HTML Coding:
[high=language]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sampmle Lead Gen Form</title>
<link href="common/stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
pageTracker._trackPageview();
//
// This is a function that I "borrowed" from the urchin.js file.
// It parses a string and returns a value. I used it to get
// data from the __utmz cookie
//
function _uGC(l,n,s) {
if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
var i,i2,i3,c="-";
i=l.indexOf;
i3=n.indexOf("=")+1;
if (i > -1) {
i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
c=l.substring((i+i3),i2);
}
return c;
}
// // Get the __utmz cookie value. This is the cookies that // stores all campaign information. // var z = _uGC(document.cookie, '__utmz=', ';'); // // The cookie has a number of name-value pairs. // Each identifies an aspect of the campaign. // // utmcsr = campaign source // utmcmd = campaign medium // utmctr = campaign term (keyword) // utmcct = campaign content // utmccn = campaign name // utmgclid = unique identifier used when AdWords auto tagging is enabled // // This is very basic code. It separates the campaign-tracking cookie // and populates a variable with each piece of campaign info. // var source = _uGC(z, 'utmcsr=', '|'); var medium = _uGC(z, 'utmcmd=', '|'); var term = _uGC(z, 'utmctr=', '|'); var content = _uGC(z, 'utmcct=', '|'); var campaign = _uGC(z, 'utmccn=', '|'); var gclid = _uGC(z, 'utmgclid=', '|'); // // The gclid is ONLY present when auto tagging has been enabled. // All other variables, except the term variable, will be '(not set)'. // Because the gclid is only present for Google AdWords we can // populate some other variables that would normally // be left blank. // if (gclid !="-") { source = 'google'; medium = 'cpc'; } // Data from the custom segmentation cookie can also be passed // back to your server via a hidden form field var csegment = _uGC(document.cookie, '__utmv=', ';'); if (csegment != '-') { var csegmentex = /[1-9]*?\.(.*)/;
csegment = csegment.match(csegmentex); csegment = csegment[1]; } else { csegment = '(not set)'; } //
// One more bonus piece of information. // We're going to extract the number of visits that the visitor
// has generated. It's also stored in a cookie, the __utma cookis
// var a = _uGC(document.cookie, '__utma=', ';');
var aParts = a.split(".");
var nVisits = aParts[5];
function populateHiddenFields(f) { f.source.value = source; f.medium.value = medium; f.term.value = term; f.content.value = content; f.campaign.value = campaign; f.segment.value = csegment; f.numVisits.value = nVisits;
alert('source='+f.source.value); alert('medium='+f.medium.value); alert('term='+f.term.value); alert('content='+f.content.value); alert('campaign='+f.campaign.value); alert('custom segment='+f.segment.value); alert('number of visits='+f.numVisits.value);
return false; } </script>
</head>
<body>
<div class="shadowBg">
<div class="container"><br />
<div class="topTowBox">
<div class="formBox">
<div class="whiteBox">
<p> </p>
</div>
<div class="whiteBoxBottomShadow"></div>
<div class="formTextfieldBox">
<form action="quote.php" method="post"
onsubmit="populateHiddenFields(this);"> <label><input name="source"
type="hidden" /> </label><label><input name="medium" type="hidden" />
</label>
<label><input name="term" type="hidden" /> </label><label><input
name="content" type="hidden" /> </label>
<label><input name="campaign" type="hidden" /> </label><label><input
name="segment" type="hidden" /></label>
<label> <input name="numVisits" type="hidden" /> </label>
<table align="center" border="0" cellspacing="0" width="98%">
<tbody>
<tr>
<td align="center" valign="middle" width="21%">Name:</td>
<td align="left" valign="middle" width="79%"><label>
<input name="name" id="textfield" class="textFieldBox"
type="text" /></label></td>
</tr>
<tr>
<td align="center" valign="middle">Email:</td>
<td align="left" valign="middle"> <label>
<input name="Email" id="textfield" class="textFieldBox"
type="text" /></label></td>
</tr>
<tr>
<td align="center" valign="middle">Phone:</td>
<td align="left" valign="middle"> <label>
<input name="Phone" id="textfield" class="textFieldBox"
type="text" /></label></td>
</tr>
<tr>
<td align="center" valign="middle">URL:</td>
<td align="left" valign="middle"> <label>
<input name="url" id="textfield" class="textFieldBox"
type="text" /></label></td>
</tr>
<tr>
<td align="center" valign="middle"> </td>
<td align="right" valign="middle"> <input
src="images/i-m-curious-button.png" alt="i-m-curious-button"
class="rightMargin marginBottom" height="56" type="image" width="192" />
</td>
</tr>
</tbody>
</table>
</form>
</div>
<br />
</div>
</div>
</div>
</div>
</body>[/high]
PHP Email submit form:
[high=language]
<?php
// ms_mail: Updated 31 may 2006
function ms_mail($to, $subject, $message, $arr_headers= array())
{
$str_headers = '';
foreach($arr_headers as $name=>$value){
$str_headers .= "$name: $value \r\n";
}
if(@mail($to, $subject, $message, $str_headers))
{return 1; }
else
{
return 0;
}
}
if(isset($_POST['x']) || isset($_POST["SendMe_x"])){
@extract($_POST);
$tt = "<br>";
$to = 'test@gmail.com';
$SUB = "Test Email".$url;
$mess = 'Dear So and So,';
$mess .=$tt;
$mess .="<strong>Name :</strong>".$Name;
$mess .=$tt;
$mess .="<strong>Email Address :</strong>".$Email;
$mess .=$tt;
$mess .="<strong>Phone :</strong>".$Phone;
$mess .=$tt;
$mess .="<strong>Website :</strong>".$url;
$mess .=$tt;
$mess .=$tt;
$mess .="Thanks and Regards";
$mess .=$tt;
$mess .=$Name;
$mess .=$tt;
$mess .="Marketing Tracking Information";
$mess .=$tt;
$mess .="Source:".$source;
$mess .="Medium:".$medium;
$mess .="Term:".$term;
$mess .="Content:".$content;
$mess .="Campaign:".$campaign;
$mess .="Segment:".$segment;
$mess .="Number of Visits:".$numVisits;
$mess .=$tt;
$mess .=$tt;
$mess .=$tt;
$mess .=$tt;
$mess .=$tt;
$from_array = array('MIME-Version'=>'1.0','Content-type'=>'text/html; charset=iso-8859-1','From'=>"$Name<$Email>");
if(ms_mail($to,$SUB,$mess,$from_array)){
header("Location:thankyou.html");
exit();
}
else{
header("Location:error.html");
exit();
}
}
else{
header("Location:error.html");
exit();
}
?>header("Location:error.html");
exit();
}
?>
[/high]
Epikone had a great post on their blog about passing Google Analytics Cookie data into a form.
Analytics Talk Blog Archive UPDATED: Integrating Google Analytics with a CRM
While this isn't applicable to most AM work, it's definitely something I find useful.
Basically, once implemented correctly, it will pass the Google analytics cookie data into your' contact form, but hidden. When the user presses submit, you get the data automatically.
Sweet!
However, my contact form doesn't seem to be passing the variables correctly.
It is giving me blank data where the google analytics should be.
I've stripped the contact form page + the php contact form code and placed it below. It's also in a zip in case anyone wants to play around with it.
Relevant HTML Coding:
[high=language]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sampmle Lead Gen Form</title>
<link href="common/stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
pageTracker._trackPageview();
//
// This is a function that I "borrowed" from the urchin.js file.
// It parses a string and returns a value. I used it to get
// data from the __utmz cookie
//
function _uGC(l,n,s) {
if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
var i,i2,i3,c="-";
i=l.indexOf;
i3=n.indexOf("=")+1;
if (i > -1) {
i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
c=l.substring((i+i3),i2);
}
return c;
}
// // Get the __utmz cookie value. This is the cookies that // stores all campaign information. // var z = _uGC(document.cookie, '__utmz=', ';'); // // The cookie has a number of name-value pairs. // Each identifies an aspect of the campaign. // // utmcsr = campaign source // utmcmd = campaign medium // utmctr = campaign term (keyword) // utmcct = campaign content // utmccn = campaign name // utmgclid = unique identifier used when AdWords auto tagging is enabled // // This is very basic code. It separates the campaign-tracking cookie // and populates a variable with each piece of campaign info. // var source = _uGC(z, 'utmcsr=', '|'); var medium = _uGC(z, 'utmcmd=', '|'); var term = _uGC(z, 'utmctr=', '|'); var content = _uGC(z, 'utmcct=', '|'); var campaign = _uGC(z, 'utmccn=', '|'); var gclid = _uGC(z, 'utmgclid=', '|'); // // The gclid is ONLY present when auto tagging has been enabled. // All other variables, except the term variable, will be '(not set)'. // Because the gclid is only present for Google AdWords we can // populate some other variables that would normally // be left blank. // if (gclid !="-") { source = 'google'; medium = 'cpc'; } // Data from the custom segmentation cookie can also be passed // back to your server via a hidden form field var csegment = _uGC(document.cookie, '__utmv=', ';'); if (csegment != '-') { var csegmentex = /[1-9]*?\.(.*)/;
csegment = csegment.match(csegmentex); csegment = csegment[1]; } else { csegment = '(not set)'; } //
// One more bonus piece of information. // We're going to extract the number of visits that the visitor
// has generated. It's also stored in a cookie, the __utma cookis
// var a = _uGC(document.cookie, '__utma=', ';');
var aParts = a.split(".");
var nVisits = aParts[5];
function populateHiddenFields(f) { f.source.value = source; f.medium.value = medium; f.term.value = term; f.content.value = content; f.campaign.value = campaign; f.segment.value = csegment; f.numVisits.value = nVisits;
alert('source='+f.source.value); alert('medium='+f.medium.value); alert('term='+f.term.value); alert('content='+f.content.value); alert('campaign='+f.campaign.value); alert('custom segment='+f.segment.value); alert('number of visits='+f.numVisits.value);
return false; } </script>
</head>
<body>
<div class="shadowBg">
<div class="container"><br />
<div class="topTowBox">
<div class="formBox">
<div class="whiteBox">
<p> </p>
</div>
<div class="whiteBoxBottomShadow"></div>
<div class="formTextfieldBox">
<form action="quote.php" method="post"
onsubmit="populateHiddenFields(this);"> <label><input name="source"
type="hidden" /> </label><label><input name="medium" type="hidden" />
</label>
<label><input name="term" type="hidden" /> </label><label><input
name="content" type="hidden" /> </label>
<label><input name="campaign" type="hidden" /> </label><label><input
name="segment" type="hidden" /></label>
<label> <input name="numVisits" type="hidden" /> </label>
<table align="center" border="0" cellspacing="0" width="98%">
<tbody>
<tr>
<td align="center" valign="middle" width="21%">Name:</td>
<td align="left" valign="middle" width="79%"><label>
<input name="name" id="textfield" class="textFieldBox"
type="text" /></label></td>
</tr>
<tr>
<td align="center" valign="middle">Email:</td>
<td align="left" valign="middle"> <label>
<input name="Email" id="textfield" class="textFieldBox"
type="text" /></label></td>
</tr>
<tr>
<td align="center" valign="middle">Phone:</td>
<td align="left" valign="middle"> <label>
<input name="Phone" id="textfield" class="textFieldBox"
type="text" /></label></td>
</tr>
<tr>
<td align="center" valign="middle">URL:</td>
<td align="left" valign="middle"> <label>
<input name="url" id="textfield" class="textFieldBox"
type="text" /></label></td>
</tr>
<tr>
<td align="center" valign="middle"> </td>
<td align="right" valign="middle"> <input
src="images/i-m-curious-button.png" alt="i-m-curious-button"
class="rightMargin marginBottom" height="56" type="image" width="192" />
</td>
</tr>
</tbody>
</table>
</form>
</div>
<br />
</div>
</div>
</div>
</div>
</body>[/high]
PHP Email submit form:
[high=language]
<?php
// ms_mail: Updated 31 may 2006
function ms_mail($to, $subject, $message, $arr_headers= array())
{
$str_headers = '';
foreach($arr_headers as $name=>$value){
$str_headers .= "$name: $value \r\n";
}
if(@mail($to, $subject, $message, $str_headers))
{return 1; }
else
{
return 0;
}
}
if(isset($_POST['x']) || isset($_POST["SendMe_x"])){
@extract($_POST);
$tt = "<br>";
$to = 'test@gmail.com';
$SUB = "Test Email".$url;
$mess = 'Dear So and So,';
$mess .=$tt;
$mess .="<strong>Name :</strong>".$Name;
$mess .=$tt;
$mess .="<strong>Email Address :</strong>".$Email;
$mess .=$tt;
$mess .="<strong>Phone :</strong>".$Phone;
$mess .=$tt;
$mess .="<strong>Website :</strong>".$url;
$mess .=$tt;
$mess .=$tt;
$mess .="Thanks and Regards";
$mess .=$tt;
$mess .=$Name;
$mess .=$tt;
$mess .="Marketing Tracking Information";
$mess .=$tt;
$mess .="Source:".$source;
$mess .="Medium:".$medium;
$mess .="Term:".$term;
$mess .="Content:".$content;
$mess .="Campaign:".$campaign;
$mess .="Segment:".$segment;
$mess .="Number of Visits:".$numVisits;
$mess .=$tt;
$mess .=$tt;
$mess .=$tt;
$mess .=$tt;
$mess .=$tt;
$from_array = array('MIME-Version'=>'1.0','Content-type'=>'text/html; charset=iso-8859-1','From'=>"$Name<$Email>");
if(ms_mail($to,$SUB,$mess,$from_array)){
header("Location:thankyou.html");
exit();
}
else{
header("Location:error.html");
exit();
}
}
else{
header("Location:error.html");
exit();
}
?>header("Location:error.html");
exit();
}
?>
[/high]