Google-Analytics-PHP-cookie-parser
Google-Analytics-PHP-cookie-parser copied to clipboard
PHP Notices all over the place
Your code throws notices all the time due to undefined indexes and the like. Please clean it up. Also, go pick up a copy of Clean Code by Robert Martin (http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882). Adhere to most of the stuff religiously.
So, I haven't tested this but here's some code that does most of what you've done with GA_Parse. It's cleaned up properly according to principles outlined in Clean Code. I might push you some code later but I haven't had time to actually pull down your repo yet, just modifying the code I found in the code I'm working with.
class GA_Parse {
private $utmzCookie;
private $utmaCookie;
private $utmbCookie;
public $campaign_source; // Campaign Source
public $campaign_name; // Campaign Name
public $campaign_medium; // Campaign Medium
public $campaign_content; // Campaign Content
public $campaign_term; // Campaign Term
public $times_visited; // Times visited
public $pages_viewed; // Pages viewed in current session
public function __construct($cookie) {
$this->utmzCookie = isset($cookie['__utmz']) ? $cookie['__utmz'] : null;
$this->utmaCookie = isset($cookie['__utma']) ? $cookie['__utma'] : null;
$this->utmbCookie = isset($cookie['__utmb']) ? $cookie['__utmb'] : null;
$this->parseCookies();
}
private function parseCookies(){
$this->parseUtmzCookie();
$this->parseUtmaCookie();
$this->parseUtmbCookie();
}
private function parseUtmzCookie() {
if (!empty($this->utmzCookie)) {
$utmzArray = $this->splitCookie($this->utmzCookie, 5);
$this->processUtmzArray($utmzArray);
}
}
private function processUtmzArray(array $utmzArray) {
/*
$domainHash = $this->getIfSet($utmzArray, 0);
$timestamp = $this->getIfSet($utmzArray, 1);
$sessionNumber = $this->getIfSet($utmzArray, 2);
$campaignNumber = $this->getIfSet($utmzArray, 3);
*/
$campaignData = $this->getIfSet($utmzArray, 4);
$campaignDataArray = $this->getCampaignDataArrayFromCampaignData($campaignData);
$isNotAdwordsCampaign = !isset($campaignDataArray['utmgclid']);
if ($isNotAdwordsCampaign) {
$this->processNonAdwordsCampaign($campaignDataArray);
}
else {
$this->processAdwordsCampaign($campaignDataArray);
}
}
private function getCampaignDataArrayFromCampaignData($campaignData) {
$campaignDataArray = [];
parse_str(strtr($campaignData, "|", "&"), $campaignDataArray);
return $campaignDataArray;
}
private function processNonAdwordsCampaign(array $campaignDataArray) {
$this->campaign_source = $this->getIfSet($campaignDataArray, 'utmcsr');
$this->campaign_name = $this->getIfSet($campaignDataArray, 'utmccn');
$this->campaign_medium = $this->getIfSet($campaignDataArray, 'utmcmd');
$this->campaign_term = $this->getIfSet($campaignDataArray, 'utmctr');
$this->campaign_content = $this->getIfSet($campaignDataArray, 'utmcct');
}
private function processAdwordsCampaign(array $campaignDataArray) {
$this->campaign_source = "GoogleAdwords";
$this->campaign_name = "GoogleAdwords";
$this->campaign_medium = "cpc";
$this->campaign_content = "";
$this->campaign_term = $this->getIfSet($campaignDataArray, 'utmctr');
}
private function parseUtmaCookie() {
if (!empty($this->utmaCookie)) {
$utmaArray = $this->splitCookie($this->utmaCookie, 5);
/*
$domainHash = $this->getIfSet($pregSplitArray, 0);
$randomId = $this->getIfSet($pregSplitArray, 1);
$timeInitialVisit = $this->getIfSet($pregSplitArray, 2);
$timeBeginningPreviousVisit = $this->getIfSet($pregSplitArray, 3);
$timeBeginningCurrentVisit = $this->getIfSet($pregSplitArray, 4);
*/
$sessionCounter = $this->getIfSet($utmaArray, 5);
$this->times_visited = $sessionCounter;
}
}
private function parseUtmbCookie() {
if (!empty($this->utmbCookie)) {
$utmbArray = $this->splitCookie($this->utmbCookie, 4);
#$domainHash = $this->getIfSet($pregSplitArray, 0);
$pagesViewed = $this->getIfSet($utmbArray, 1);
#$garbage = $this->getIfSet($pregSplitArray, 2);
#$timeBeginningCurrentSession = $this->getIfSet($pregSplitArray, 3);
$this->pages_viewed = $pagesViewed;
}
}
private function splitCookie($cookieValue, $maxValues) {
return preg_split('[\.]', $cookieValue, $maxValues);
}
private function getIfSet(array $array, $index, $defaultValue = null) {
return isset($array[$index]) ? $array[$index] : $defaultValue;
}
}