Skip to content

Instantly share code, notes, and snippets.

@smalltown
Created July 1, 2014 15:26
Show Gist options
  • Save smalltown/b8f74d0f56baa96f82e0 to your computer and use it in GitHub Desktop.
Save smalltown/b8f74d0f56baa96f82e0 to your computer and use it in GitHub Desktop.
Sample code about how to pass data to GA
<?
class GA_Parse
{
var $campaign_source; // Campaign Source
var $campaign_name; // Campaign Name
var $campaign_medium; // Campaign Medium
var $campaign_content; // Campaign Content
var $campaign_term; // Campaign Term
var $first_visit; // Date of first visit
var $previous_visit; // Date of previous visit
var $current_visit_started; // Current visit started at
var $times_visited; // Times visited
function __construct($_COOKIE) {
$this->utmz = $_COOKIE["__utmz"];
$this->utma = $_COOKIE["__utma"];
$this->ParseCookies();
}
function ParseCookies(){
// Parse __utmz cookie
list($domain_hash,$timestamp, $session_number, $campaign_number, ↵
$campaign_data) = split('[\.]', $this->utmz);
// Parse the campaign data
$campaign_data = parse_str(strtr($campaign_data, "|", "&amp;amp;"));
$this->campaign_source = $utmcsr;
$this->campaign_name = $utmccn;
$this->campaign_medium = $utmcmd;
$this->campaign_term = $utmctr;
$this->campaign_content = $utmcct;
if($utmgclid) {
$this->campaign_source = "google";
$this->campaign_name = "";
$this->campaign_medium = "cpc";
$this->campaign_content = "";
$this->campaign_term = $utmctr;
}
// Parse the __utma Cookie
list($domain_hash,
$random_id,
$time_initial_visit,
$time_beginning_previous_visit,
$time_beginning_current_visit,
$session_counter) = split('[\.]', $this->utma);
$this->first_visit = date("d M Y - H:i",$time_initial_visit);
$this->previous_visit = date("d M Y - H:i",↵
$time_beginning_previous_visit);
$this->current_visit_started = date("d M Y - H:i",↵
$time_beginning_current_visit);
$this->times_visited = $session_counter;
}
}
?>
<script type="text/javascript">
function _uGC(l,n,s) {
// used to obtain a value form a string of key=value pairs
if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
var i,i2,i3,c="-";
i=l.indexOf(n);
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;
}
function setHidden(f) {
// set values for hidden form fields
var z = _uGC(document.cookie, "utmz=",";");
f.web_source.value = _uGC(z,"utmcsr=","|");
f.web_medium.value = _uGC(z,"utmcmd=","|");
f.web_term.value = _uGC(z,"utmctr=","|");
f.web_content.value = _uGC(z,"utmcct=","|");
f.web_campaign.value = _uGC(z,"utmccn=","|");
var gclid = _uGC(z,"utmgclid=","|");
if (gclid) { //this is an AdWords visitor
f.web_source.value = "google";
f.web_medium.value = "cpc";
//It is not possible to capture AdWords campaign details by this
//method as GA processing is required for this. Therefore the
//following lines are set to remove confusion should a visitor
//use multiple referrals with the last one being AdWords.
f.web_term.value = ""; // remove previous info if any
f.web_content.value = ""; // remove previous info if any
f.web_campaign.value = ""; // remove previous info if any
}
}
</script>
<form method="post" action="formhandler.cgi" onSubmit="setHidden(this);">
<input type=hidden name=web_source value="">
<input type=hidden name=web_medium value="">
<input type=hidden name=web_term value="">
<input type=hidden name=web_content value="">
<input type=hidden name=web_campaign value="">
</form>
<?
require("class.gaparse.php");
$aux = new GA_Parse($_COOKIE);
echo "Campaign source: ".$aux->campaign_source."<br />";
echo "Campaign name: ".$aux->campaign_name."<br />";
echo "Campaign medium: ".$aux->campaign_medium."<br />";
echo "Campaign content: ".$aux->campaign_content."<br />";
echo "Campaign term: ".$aux->campaign_term."<br />";
echo "Date of first visit: ".$aux->first_visit."<br />";
echo "Date of previous visit: ".$aux->previous_visit."<br />";
echo "Date of current visit: ".$aux->current_visit_started."<br />";
echo "Times visited: ".$aux->times_visited."<br />";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment