From f4065bc5167a313943b56aa56bf7200d79192029 Mon Sep 17 00:00:00 2001 From: Kiosk Date: Tue, 27 Nov 2012 13:52:21 -0600 Subject: [PATCH 1/4] Added isset tracking for all utm variables in parseCookies(). If variable is not set, it is set to null. --- class.gaparse.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/class.gaparse.php b/class.gaparse.php index e5bdaf2..f584bb3 100644 --- a/class.gaparse.php +++ b/class.gaparse.php @@ -41,16 +41,15 @@ function __construct($_COOKIE) { } function ParseCookies(){ - // Parse __utmz cookie list($domain_hash,$timestamp, $session_number, $campaign_numer, $campaign_data) = preg_split('[\.]', $_COOKIE["__utmz"],5); // Parse the campaign data $campaign_data = parse_str(strtr($campaign_data, "|", "&")); - $this->campaign_source = $utmcsr; - $this->campaign_name = $utmccn; - $this->campaign_medium = $utmcmd; + $this->campaign_source = isset($utmcsr) ? $utmcsr : null; + $this->campaign_name = isset($utmccn) ? $utmccn : null; + $this->campaign_medium = isset($utmcmd) ? $utmcmd : null; if (isset($utmctr)) $this->campaign_term = $utmctr; if (isset($utmcct)) $this->campaign_content = $utmcct; From f2cca0a38ea17c389aaca68a049a43a0818cfb76 Mon Sep 17 00:00:00 2001 From: Kiosk Date: Thu, 28 Aug 2014 12:48:37 -0500 Subject: [PATCH 2/4] Google Analytics Cookie Tracker Rewrite. Added namespacing, autoloading, UTMV support and PHPUnit tests. --- README.md | 44 +++ Readme.md | 22 -- autoload.php | 7 + class.gaparse.php | 94 ----- example.php | 33 -- lib/SplClassLoader.php | 136 ++++++++ phpunit.xml | 7 + .../CampaignTracking/CustomVar.php | 82 +++++ .../CampaignTracking/Tracker.php | 323 ++++++++++++++++++ .../CampaignTracking/Utm/A.php | 185 ++++++++++ .../CampaignTracking/Utm/B.php | 112 ++++++ .../CampaignTracking/Utm/C.php | 68 ++++ .../CampaignTracking/Utm/V.php | 78 +++++ .../CampaignTracking/Utm/Z.php | 267 +++++++++++++++ .../CampaignTracking/TrackerTest.php | 54 +++ .../CampaignTracking/Utm/ATest.php | 30 ++ .../CampaignTracking/Utm/BTest.php | 24 ++ .../CampaignTracking/Utm/CTest.php | 20 ++ .../CampaignTracking/Utm/VTest.php | 29 ++ .../CampaignTracking/Utm/ZTest.php | 59 ++++ 20 files changed, 1525 insertions(+), 149 deletions(-) create mode 100644 README.md delete mode 100644 Readme.md create mode 100644 autoload.php delete mode 100644 class.gaparse.php delete mode 100755 example.php create mode 100644 lib/SplClassLoader.php create mode 100644 phpunit.xml create mode 100644 src/GoogleAnalytics/CampaignTracking/CustomVar.php create mode 100644 src/GoogleAnalytics/CampaignTracking/Tracker.php create mode 100644 src/GoogleAnalytics/CampaignTracking/Utm/A.php create mode 100644 src/GoogleAnalytics/CampaignTracking/Utm/B.php create mode 100644 src/GoogleAnalytics/CampaignTracking/Utm/C.php create mode 100644 src/GoogleAnalytics/CampaignTracking/Utm/V.php create mode 100644 src/GoogleAnalytics/CampaignTracking/Utm/Z.php create mode 100644 tests/GoogleAnalytics/CampaignTracking/TrackerTest.php create mode 100644 tests/GoogleAnalytics/CampaignTracking/Utm/ATest.php create mode 100644 tests/GoogleAnalytics/CampaignTracking/Utm/BTest.php create mode 100644 tests/GoogleAnalytics/CampaignTracking/Utm/CTest.php create mode 100644 tests/GoogleAnalytics/CampaignTracking/Utm/VTest.php create mode 100644 tests/GoogleAnalytics/CampaignTracking/Utm/ZTest.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..b3d560f --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +Installation: + +Clone this repository, and include autoload.php. If you are using another +autoloader, simply point it to the GoogleAnalytics namespace in the "src" +directory. + +Usage: + +The Google Analytics UTM Variables typycally available as cookies. If you are tracking accross domains (using _getLinkerUrl()), these parameters will initially be available via the HTTP_REFERER query string. + +This library is currently able to process the following cookies: + +__utma - Expires 2 years after set/update. Stores Domain Hash, Visitor ID and a series of timestamps. +__utmb - Expires 30 mins from set/update. This is used to determine session expiration. +__utmc - Expires at the end of browser session. Deprecated. +__utmv - Expires 2 years from set/update. Stores Custom Variables. +__utmz - Expires 6 months from set/update. Stores campaign attribution information. + +```php + getCampaignMedium() . "\n"; + echo $tracker->getCampaignSource() . "\n"; +``` + +Wish List: + +1) First and foremost, the regular expressions used in the "validate" methods +need to be improved upon. + +2) I would like to implement ArrayAcces, Serializable and JsonSerializable. +One thing that will have to be decided is how to handle the timestamps. +All the timestamps are currently transformed into DateTime objects. For +ArrayAccess, we could return a string or the DateTime object. For the +serializing interfaces, we will certainly want a string, but a decision +will have to be made as to what format (ie. UNIX Timestamp, ISO8601, etc). +So... + 2a) Implement ArrayAccess for Tracker, CustomVar and Utm classes. + 2b) Implement Serializable for Tracker, CustomVar and Utm classes. + 2c) Implement JsonSerializable for Tracker, CustomVar and Utm classes. + +3) Implement some sort of toArray() functionality for the whole model. diff --git a/Readme.md b/Readme.md deleted file mode 100644 index ec0b3ab..0000000 --- a/Readme.md +++ /dev/null @@ -1,22 +0,0 @@ -[*Google Analytics PHP cookie parser*](http://joaocorreia.pt/google-analytics-scripts/google-analytics-php-cookie-parser/) -================= -by [João Correia](http://joaocorreia.pt/) - -Google Analytics collects data using first-party cookies who are stored on our browsers. I’ve programmed a PHP class we can use to read Google Analytics __utma and __utmz cookies. This class can be used to easily integrate this cookie data into proprietary systems like CRM, ERP, Helpdesks, etc. - -* **__utma (expires 2 years after being defined) – visitor data**. This cookie is written on your first visit to the website. In case you erase it its created again. Its used for the Unique Visitors calculation and is updated on every pageview. - -* **__utmz (expires 6 months after being defined) – campaign data**. This cookie stores informations on how the user got to our website: referrer, direct (none), organic or a campaign such as a newsletter. (since you tag it correctly using the URL Builder). This cookie is overwritten every time you visit the website. - -The Google Analytics Cookie Parser allows you to obtain some data contained in this cookies in a human readable format. For example, you can see how you got here by visiting: http://joaocorreia.pt/example.php. - -* **Campaign source** -* **Campaign name** -* **Campaign medium** -* **Campaign content** -* **Campaign term** -* **Date of first visit** -* **Date of previous visit** -* **Date of current visit** -* **Times visited** -* **Pages viewed** \ No newline at end of file diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..afb2f3b --- /dev/null +++ b/autoload.php @@ -0,0 +1,7 @@ +register(); diff --git a/class.gaparse.php b/class.gaparse.php deleted file mode 100644 index f584bb3..0000000 --- a/class.gaparse.php +++ /dev/null @@ -1,94 +0,0 @@ -ParseCookies(); - } - - } - - function ParseCookies(){ - // Parse __utmz cookie - list($domain_hash,$timestamp, $session_number, $campaign_numer, $campaign_data) = preg_split('[\.]', $_COOKIE["__utmz"],5); - - // Parse the campaign data - $campaign_data = parse_str(strtr($campaign_data, "|", "&")); - - $this->campaign_source = isset($utmcsr) ? $utmcsr : null; - $this->campaign_name = isset($utmccn) ? $utmccn : null; - $this->campaign_medium = isset($utmcmd) ? $utmcmd : null; - if (isset($utmctr)) $this->campaign_term = $utmctr; - if (isset($utmcct)) $this->campaign_content = $utmcct; - - // You should tag you campaigns manually to have a full view - // of your adwords campaigns data. - // The same happens with Urchin, tag manually to have your campaign data parsed properly. - - if (isset($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) = preg_split('[\.]', $_COOKIE["__utma"]); - - $this->first_visit = new \DateTime(); - $this->first_visit->setTimestamp($time_initial_visit); - - $this->previous_visit = new \DateTime(); - $this->previous_visit->setTimestamp($time_beginning_previous_visit); - - $this->current_visit_started = new \DateTime(); - $this->current_visit_started->setTimestamp($time_beginning_current_visit); - - $this->times_visited = $session_counter; - - // Parse the __utmb Cookie - - list($domain_hash,$pages_viewed,$garbage,$time_beginning_current_session) = preg_split('[\.]', $_COOKIE["__utmb"]); - $this->pages_viewed = $pages_viewed; - - - // End ParseCookies - } - -// End GA_Parse -} - -?> diff --git a/example.php b/example.php deleted file mode 100755 index cd3684f..0000000 --- a/example.php +++ /dev/null @@ -1,33 +0,0 @@ -campaign_source."
"; -echo "Campaign name: ".$aux->campaign_name."
"; -echo "Campaign medium: ".$aux->campaign_medium."
"; -echo "Campaign content: ".$aux->campaign_content."
"; -echo "Campaign term: ".$aux->campaign_term."
"; - -echo "Date of first visit: ".$aux->first_visit."
"; -echo "Date of previous visit: ".$aux->previous_visit."
"; -echo "Date of current visit: ".$aux->current_visit_started."
"; -echo "Times visited: ".$aux->times_visited."
"; -echo "Pages viewed current visit: ".$aux->pages_viewed."
"; - -?> - \ No newline at end of file diff --git a/lib/SplClassLoader.php b/lib/SplClassLoader.php new file mode 100644 index 0000000..2bc31f0 --- /dev/null +++ b/lib/SplClassLoader.php @@ -0,0 +1,136 @@ +register(); + * + * @author Jonathan H. Wage + * @author Roman S. Borschel + * @author Matthew Weier O'Phinney + * @author Kris Wallsmith + * @author Fabien Potencier + */ +class SplClassLoader +{ + private $_fileExtension = '.php'; + private $_namespace; + private $_includePath; + private $_namespaceSeparator = '\\'; + + /** + * Creates a new SplClassLoader that loads classes of the + * specified namespace. + * + * @param string $ns The namespace to use. + */ + public function __construct($ns = null, $includePath = null) + { + $this->_namespace = $ns; + $this->_includePath = $includePath; + } + + /** + * Sets the namespace separator used by classes in the namespace of this class loader. + * + * @param string $sep The separator to use. + */ + public function setNamespaceSeparator($sep) + { + $this->_namespaceSeparator = $sep; + } + + /** + * Gets the namespace seperator used by classes in the namespace of this class loader. + * + * @return void + */ + public function getNamespaceSeparator() + { + return $this->_namespaceSeparator; + } + + /** + * Sets the base include path for all class files in the namespace of this class loader. + * + * @param string $includePath + */ + public function setIncludePath($includePath) + { + $this->_includePath = $includePath; + } + + /** + * Gets the base include path for all class files in the namespace of this class loader. + * + * @return string $includePath + */ + public function getIncludePath() + { + return $this->_includePath; + } + + /** + * Sets the file extension of class files in the namespace of this class loader. + * + * @param string $fileExtension + */ + public function setFileExtension($fileExtension) + { + $this->_fileExtension = $fileExtension; + } + + /** + * Gets the file extension of class files in the namespace of this class loader. + * + * @return string $fileExtension + */ + public function getFileExtension() + { + return $this->_fileExtension; + } + + /** + * Installs this class loader on the SPL autoload stack. + */ + public function register() + { + spl_autoload_register(array($this, 'loadClass')); + } + + /** + * Uninstalls this class loader from the SPL autoloader stack. + */ + public function unregister() + { + spl_autoload_unregister(array($this, 'loadClass')); + } + + /** + * Loads the given class or interface. + * + * @param string $className The name of the class to load. + * @return void + */ + public function loadClass($className) + { + if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) { + $fileName = ''; + $namespace = ''; + if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) { + $namespace = substr($className, 0, $lastNsPos); + $className = substr($className, $lastNsPos + 1); + $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; + } + $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; + + require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; + } + } +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..775bc58 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,7 @@ + + + + tests + + + diff --git a/src/GoogleAnalytics/CampaignTracking/CustomVar.php b/src/GoogleAnalytics/CampaignTracking/CustomVar.php new file mode 100644 index 0000000..edb31d4 --- /dev/null +++ b/src/GoogleAnalytics/CampaignTracking/CustomVar.php @@ -0,0 +1,82 @@ +fromArray($v); + } + + public function fromArray(array $v_arr) { + $this->setSlotNumber($v_arr[0]); + $this->setName($v_arr[1]); + $this->setValue($v_arr[2]); + $this->setScope($v_arr[3]); + } + + public function setSlotNumber($slot_number) { + $this->slot_number = $slot_number; + } + + public function getSlotNumber() { + return $this->slot_number; + } + + public function setName($name) { + $this->name = $name; + } + + public function getName() { + return $this->name; + } + + public function setValue($value) { + $this->value = $value; + } + + public function getValue() { + return $this->value; + } + + public function setScope($scope) { + $this->scope = $scope; + } + + public function getScope() { + return $this->scope; + } + +} diff --git a/src/GoogleAnalytics/CampaignTracking/Tracker.php b/src/GoogleAnalytics/CampaignTracking/Tracker.php new file mode 100644 index 0000000..4d28908 --- /dev/null +++ b/src/GoogleAnalytics/CampaignTracking/Tracker.php @@ -0,0 +1,323 @@ +parseUtmA($utm['__utma']); + + if(array_key_exists('__utmb', $utm)) + $this->parseUtmB($utm['__utmb']); + + if(array_key_exists('__utmc', $utm)) + $this->parseUtmC($utm['__utmc']); + + if(array_key_exists('__utmv', $utm)) + $this->parseUtmV($utm['__utmv']); + + if(array_key_exists('__utmz', $utm)) + $this->parseUtmZ($utm['__utmz']); + } + + /** + * @param Utm\A $utma + */ + public function setUtmA(Utm\A $utma = null) { + $this->utma = $utma; + } + + /** + * @return Utm\A $utma + */ + public function getUtmA() { + return $this->utma; + } + + /** + * The string as it appears in the cookie. + * + * @param string $__utma + */ + public function parseUtmA($__utma) { + $this->setUtmA(new Utm\A($__utma)); + } + + /** + * @param Utm\B $utmb + */ + public function setUtmB(Utm\B $utmb = null) { + $this->utmb = $utmb; + } + + /** + * @return Utm\B $utmb + */ + public function getUtmB() { + return $this->utmb; + } + + /** + * The string as it appears in the cookie. + * + * @param string $__utmb + */ + public function parseUtmB($__utmb) { + $this->setUtmB(new Utm\B($__utmb)); + } + + /** + * @param Utm\C $utmc + */ + public function setUtmC(Utm\C $utmc) { + $this->utmc = $utmc; + } + + /** + * @return Utm\C $utmc + */ + public function getUtmC() { + return $this->utmc; + } + + /** + * The string as it appears in the cookie. + * + * @param string $__utmc + */ + public function parseUtmC($__utmc) { + $this->setUtmC(new Utm\C($__utmc)); + } + + /** + * @param Utm\V $utmv + */ + public function setUtmV(Utm\V $utmv) { + $this->utmv = $utmv; + } + + /** + * @return Utm\V $utmv + */ + public function getUtmV() { + return $this->utmv; + } + + /** + * The string as it appears in the cookie. + * + * @param string $__utmv + */ + public function parseUtmV($__utmv) { + $this->setUtmV(new Utm\V($__utmv)); + } + + /** + * @param Utm\Z $utmz + */ + public function setUtmZ(Utm\Z $utmz) { + $this->utmz = $utmz; + } + + /** + * @return Utm\Z $utmz + */ + public function getUtmZ() { + return $this->utmz; + } + + /** + * The string as it appears in the cookie. + * + * @param string $__utmz + */ + public function parseUtmZ($__utmz) { + $this->setUtmZ(new Utm\Z($__utmz)); + } + + // BEGIN Accessor Functions for UTMs + private function _isValid($obj) { + return $obj && $obj->isValid(); + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @return string $domain_hash + */ + public function getDomainHash() { + return $this->_isValid($this->getUtmA()) ? $this->getUtmA()->getDomainHash() : null; + } + + /** + * @return string Visitor ID assigned by the good folks at Google. + */ + public function getVisitorId() { + return $this->_isValid($this->getUtmA()) ? $this->getUtmA()->getVisitorId() : null; + } + + /** + * DateTime object representing the initial visit. + * + * @return \DateTime $initial_visit. + */ + public function getInitialVisit() { + return $this->_isValid($this->getUtmA()) ? $this->getUtmA()->getInitialVisit() : null; + } + + /** + * DateTime object representing the previous visit. + * + * @return \DateTime $previous_visit. + */ + public function getPreviousVisit() { + return $this->_isValid($this->getUtmA()) ? $this->getUtmA()->getPreviousVisit() : null; + } + + /** + * DateTime object representing the current visit start. + * + * @return \DateTime $current_visit_start. + */ + public function getCurrentVisit() { + return $this->_isValid($this->getUtmA()) ? $this->getUtmA()->getCurrentVisit() : null; + } + + /** + * The number of times that the user has left and returned. + * + * This means the user either closed the browser or was + * inactive on this site for 30mins or more. + * + * @return int $session_count + */ + public function getSessionCount() { + return $this->_isValid($this->getUtmA()) ? $this->getUtmA()->getSessionCount() : null; + } + + /** + * @return int $pages_viewed + */ + public function getPagesViewed() { + return $this->_isValid($this->getUtmB()) ? $this->getUtmB()->getPagesViewed() : null; + } + + /** + * @return \DateTime $current_session_start + */ + public function getCurrentSessionStart() { + return $this->_isValid($this->getUtmB()) ? $this->getUtmB()->getCurrentSessionStart() : null; + } + + /** + * Returns an array of custom + * variables stored in the __utmv cookie. + * + * @return array $custom_vars + */ + public function getCustomVars() { + return $this->_isValid($this->getUtmV()) ? $this->getUtmV()->getCustomVars() : array(); + } + + /** + * Timestamp recorded in __utmz + * + * @return \DateTime $datetime + */ + public function getDateTime() { + return $this->_isValid($this->getUtmZ()) ? $this->getUtmZ()->getDateTime() : null; + } + + /** + * Session Number as recorded in __utmz + * + * @return int $session_number + */ + public function getSessionNumber() { + return $this->_isValid($this->getUtmZ()) ? $this->getUtmZ()->getSessionNumber() : null; + } + + /** + * Number of campaigns that have driven this visitor here. + * + * @return int $campaign_number + */ + public function getCampaignNumber() { + return $this->_isValid($this->getUtmZ()) ? $this->getUtmZ()->getCampaignNumber() : null; + } + + /** + * @return string $campaign_source + */ + public function getCampaignSource() { + return $this->_isValid($this->getUtmZ()) ? $this->getUtmZ()->getCampaignSource() : null; + } + + /** + * @return string $campaign_medium + */ + public function getCampaignMedium() { + return $this->_isValid($this->getUtmZ()) ? $this->getUtmZ()->getCampaignMedium() : null; + } + + /** + * @return string $campaign_name + */ + public function getCampaignName() { + return $this->_isValid($this->getUtmZ()) ? $this->getUtmZ()->getCampaignName() : null; + } + + /** + * + */ + public function getCampaignContent() { + return $this->_isValid($this->getUtmZ()) ? $this->getUtmZ()->getCampaignContent() : null; + } + + /** + * Space delimited list of keywords. + * + * @return string $campaign_term + */ + public function getCampaignTerm() { + return $this->_isValid($this->getUtmZ()) ? $this->getUtmZ()->getCampaignTerm() : null; + } + + /** + * Array of strings of keywords derived from $campaign_term. + * + * @return array $keywords + */ + public function getKeywords() { + return $this->_isValid($this->getUtmZ()) ? $this->getUtmZ()->getKeywords() : array(); + } +} diff --git a/src/GoogleAnalytics/CampaignTracking/Utm/A.php b/src/GoogleAnalytics/CampaignTracking/Utm/A.php new file mode 100644 index 0000000..855a484 --- /dev/null +++ b/src/GoogleAnalytics/CampaignTracking/Utm/A.php @@ -0,0 +1,185 @@ +is_valid = false; + $this->parse($__utma); + } + + public function isValid() { + return $this->is_valid; + } + + public function parse($__utma) { + if($__utma == null) + return; + + if(!$this->validate($__utma)) + return; + + $this->is_valid = true; + + list($domain_hash, $visitor_id, $initial_visit, $previous_visit, $current_visit, $session_count) = preg_split('[\.]', $__utma); + + $this->setDomainHash($domain_hash); + $this->setVisitorId($visitor_id); + + $this->setInitialVisit(new \DateTime()); + $this->getInitialVisit()->setTimestamp($initial_visit); + + $this->setPreviousVisit(new \DateTime()); + $this->getPreviousVisit()->setTimestamp($previous_visit); + + $this->setCurrentVisit(new \DateTime()); + $this->getCurrentVisit()->setTimestamp($current_visit); + + $this->setSessionCount($session_count); + } + + private function validate($__utma) { + return preg_match('/^\d+\.\d+\.\d+\.\d+\.\d+/', $__utma); + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @param string $domain_hash + */ + public function setDomainHash($domain_hash) { + $this->domain_hash = $domain_hash; + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @return string $domain_hash + */ + public function getDomainHash() { + return $this->domain_hash; + } + + /** + * @param string Visitor ID assigned by the good folks at Google. + */ + public function setVisitorId($visitor_id) { + $this->visitor_id = $visitor_id; + } + + /** + * @return string Visitor ID assigned by the good folks at Google. + */ + public function getVisitorId() { + return $this->visitor_id; + } + + /** + * DateTime object representing the initial visit. + * + * @param \DateTime $initial_visit. + */ + public function setInitialVisit(\DateTime $initial_visit = null) { + $this->initial_visit = $initial_visit; + } + + /** + * DateTime object representing the initial visit. + * + * @return \DateTime $initial_visit. + */ + public function getInitialVisit() { + return $this->initial_visit; + } + + /** + * DateTime object representing the previous visit. + * + * @return \DateTime $previous_visit. + */ + public function setPreviousVisit(\DateTime $previous_visit = null) { + $this->previous_visit = $previous_visit; + } + + /** + * DateTime object representing the previous visit. + * + * @return \DateTime $previous_visit. + */ + public function getPreviousVisit() { + return $this->previous_visit; + } + + /** + * DateTime object representing the current visit start. + * + * @param \DateTime $current_visit_start. + */ + public function setCurrentVisit(\DateTime $current_visit = null) { + $this->current_visit = $current_visit; + } + + /** + * DateTime object representing the current visit start. + * + * @return \DateTime $current_visit_start. + */ + public function getCurrentVisit() { + return $this->current_visit; + } + + /** + * The number of times that the user has left and returned. + * + * This means the user either closed the browser or was + * inactive on this site for 30mins or more. + * + * @param int $session_count + */ + public function setSessionCount($session_count) { + $this->session_count = $session_count; + } + + /** + * The number of times that the user has left and returned. + * + * This means the user either closed the browser or was + * inactive on this site for 30mins or more. + * + * @return int $session_count + */ + public function getSessionCount() { + return $this->session_count; + } +} diff --git a/src/GoogleAnalytics/CampaignTracking/Utm/B.php b/src/GoogleAnalytics/CampaignTracking/Utm/B.php new file mode 100644 index 0000000..ef31bd1 --- /dev/null +++ b/src/GoogleAnalytics/CampaignTracking/Utm/B.php @@ -0,0 +1,112 @@ +is_valid = false; + $this->parse($__utmb); + } + + public function parse($__utmb) { + if($__utmb == null || empty($__utmb)) + return; + + if(!$this->validate($__utmb)) + return; + + $this->is_valid = true; + + list($domain_hash,$pages_viewed,$garbage,$current_session_start) = preg_split('[\.]', $__utmb); + + $this->setDomainHash($domain_hash); + + $this->setPagesViewed($pages_viewed); + + $this->setCurrentSessionStart(new \DateTime()); + $this->getCurrentSessionStart()->setTimestamp($current_session_start); + } + + public function isValid() { + return $this->is_valid; + } + + public function validate($__utmb) { + return preg_match('/^\d+\.\d+\.\d+\.\d+/', $__utmb); + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @param string $domain_hash + */ + public function setDomainHash($domain_hash) { + $this->domain_hash = $domain_hash; + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @return string $domain_hash + */ + public function getDomainHash() { + return $this->domain_hash; + } + + /** + * @param int $pages_viewed + */ + public function setPagesViewed($pages_viewed) { + $this->pages_viewed = $pages_viewed; + } + + /** + * @return int $pages_viewed + */ + public function getPagesViewed() { + return $this->pages_viewed; + } + + /** + * @param \DateTime $current_session_start + */ + public function setCurrentSessionStart(\DateTime $current_session_start) { + $this->current_session_start = $current_session_start; + } + + /** + * @return \DateTime $current_session_start + */ + public function getCurrentSessionStart() { + return $this->current_session_start; + } +} diff --git a/src/GoogleAnalytics/CampaignTracking/Utm/C.php b/src/GoogleAnalytics/CampaignTracking/Utm/C.php new file mode 100644 index 0000000..f08d935 --- /dev/null +++ b/src/GoogleAnalytics/CampaignTracking/Utm/C.php @@ -0,0 +1,68 @@ +is_valid = false; + $this->parse($__utmc); + } + + public function parse($__utmc) { + if(!$this->validate($__utmc)) + return; + + $this->is_valid = true; + + $this->setDomainHash($__utmc); + } + + public function isValid() { + return $this->is_valid; + } + + public function validate($__utmc) { + return preg_match('/\d+/', $__utmc); + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @return string $domain_hash + */ + public function setDomainHash($domain_hash) { + $this->domain_hash = $domain_hash; + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @param string $domain_hash + */ + public function getDomainHash() { + return $this->domain_hash; + } +} diff --git a/src/GoogleAnalytics/CampaignTracking/Utm/V.php b/src/GoogleAnalytics/CampaignTracking/Utm/V.php new file mode 100644 index 0000000..bfafb59 --- /dev/null +++ b/src/GoogleAnalytics/CampaignTracking/Utm/V.php @@ -0,0 +1,78 @@ +is_valid = false; + $this->parse($__utmv); + } + + public function parse($__utmv) { + $this->custom_vars = array(); + + if(!$this->validate($__utmv)) + return; + + $this->is_valid = true; + + list($this->domain_hash, $custom_var_string) = preg_split('[\|]', $__utmv); + + $custom_vars = array_map(function($v) { return preg_split('[\=]', $v); }, preg_split('[\^]', $custom_var_string)); + + foreach($custom_vars as $custom_var) { + $this->custom_vars[] = new CustomVar($custom_var); + } + + } + + public function isValid() { + return $this->isValid(); + } + + public function validate($__utmv) { + return preg_match('/^\d+\.\|+(\d=\S+=\S+=\d\^*)/', $__utmv); + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @param string $domain_hash + */ + public function setDomainHash($domain_hash) { + $this->domain_hash = $domain_hash; + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @return string $domain_hash + */ + public function getDomainHash() { + return $this->domain_hash; + } + + /** + * @return array $custom_vars + */ + public function getCustomVars() { + return $this->custom_vars; + } + +} diff --git a/src/GoogleAnalytics/CampaignTracking/Utm/Z.php b/src/GoogleAnalytics/CampaignTracking/Utm/Z.php new file mode 100644 index 0000000..32c85f1 --- /dev/null +++ b/src/GoogleAnalytics/CampaignTracking/Utm/Z.php @@ -0,0 +1,267 @@ + of keywords derived from $campaign_term. + */ + private $keywords; + + private $campaign_content; + + private $gclid; + + private $is_valid; + + /** + * @param string $__utmz + */ + public function __construct($__utmz) { + $this->parse($__utmz); + } + + public function isValid() { + return $this->is_valid; + } + + public function validate($__utmz) { + return preg_match('/^\d+\.\d+\.\d+\.\d+\./', $__utmz); + } + + /** + * @param string $__utmz + */ + public function parse($__utmz) { + $this->is_valid = false; + + if(!$this->validate($__utmz)) + return; + + $this->is_valid = true; + + list($domain_hash,$timestamp, $session_number, $campaign_number, $campaign_data_string) = preg_split('[\.]', $__utmz,5); + + $this->setDomainhash($domain_hash); + + $this->setDateTime(new \DateTime()); + $this->getDateTime()->setTimestamp($timestamp); + + $this->setSessionNumber($session_number); + + $this->setCampaignNumber($campaign_number); + + // Parse the campaign data + $campaign_data = array(); + parse_str(strtr($campaign_data_string, "|", "&"), $campaign_data); + + if(array_key_exists('utmcsr', $campaign_data)) + $this->setCampaignSource($campaign_data['utmcsr']); + + if(array_key_exists('utmccn', $campaign_data)) + $this->setCampaignName($campaign_data['utmccn']); + + if(array_key_exists('utmcmd', $campaign_data)) + $this->setCampaignMedium($campaign_data['utmcmd']); + + if(array_key_exists('utmctr', $campaign_data) && !empty($campaign_data['utmctr'])) { + $this->setCampaignTerm($campaign_data['utmctr']); + + $keywords = explode(' ', $this->getCampaignTerm()); + + if(count($keywords) > 0) + $this->setKeywords($keywords); + } + + if(array_key_exists('utmcct', $campaign_data)) + $this->setCampaignContent($campaign_data['utmcct']); + + if(array_key_exists('gclid', $campaign_data)) + $this->setGoogleClickId($campaign_data['gclid']); + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @param string $domain_hash + */ + public function setDomainHash($domain_hash) { + $this->domain_hash = $domain_hash; + } + + /** + * The domain hash allows you to track multiple + * subdomains with the same tracking code. + * + * @return string $domain_hash + */ + public function getDomainHash() { + return $this->domain_hash; + } + + /** + * @param \DateTime $datetime + */ + public function setDateTime(\DateTime $datetime = null) { + $this->datetime = $datetime; + } + + /** + * @return \DateTime $datetime + */ + public function getDateTime() { + return $this->datetime; + } + + /** + * @param int $session_number + */ + public function setSessionNumber($session_number) { + $this->session_number = $session_number; + } + + /** + * @return int $session_number + */ + public function getSessionNumber() { + return $this->session_number; + } + + /** + * @param int $campaign_number + */ + public function setCampaignNumber($campaign_number) { + $this->campaign_number = $campaign_number; + } + + /** + * @return int $campaign_number + */ + public function getCampaignNumber() { + return $this->campaign_number; + } + + /** + * @param string $campaign_source + */ + public function setCampaignSource($campaign_source) { + $this->campaign_source = $campaign_source; + } + + /** + * @return string $campaign_source + */ + public function getCampaignSource() { + return $this->campaign_source; + } + + /** + * @param string $campaign_name + */ + public function setCampaignName($campaign_name) { + $this->campaign_name = $campaign_name; + } + + /** + * @return string $campaign_name + */ + public function getCampaignName() { + return $this->campaign_name; + } + + /** + * @param string $campaign_content + */ + public function setCampaignContent($campaign_content) { + $this->campaign_content = $campaign_content; + } + + /** + * @return string $campaign_content + */ + public function getCampaignContent() { + return $this->campaign_content; + } + + /** + * @param string $campaign_medium + */ + public function setCampaignMedium($campaign_medium) { + $this->campaign_medium = $campaign_medium; + } + + /** + * @return string $campaign_medium + */ + public function getCampaignMedium() { + return $this->campaign_medium; + } + + /** + * String demited list of keywords. + * + * @param string $campaign_term + */ + public function setCampaignTerm($campaign_term) { + $this->campaign_term = $campaign_term; + } + + /** + * Space delimited list of keywords. + * + * @return string $campaign_term + */ + public function getCampaignTerm() { + return $this->campaign_term; + } + + /** + * Array of strings of keywords derived from $campaign_term. + * + * @param array $keywords + */ + public function setKeywords(array $keywords = null) { + $this->keywords = $keywords; + } + + /** + * Array of strings of keywords derived from $campaign_term. + * + * @return array $keywords + */ + public function getKeywords() { + return $this->keywords; + } +} diff --git a/tests/GoogleAnalytics/CampaignTracking/TrackerTest.php b/tests/GoogleAnalytics/CampaignTracking/TrackerTest.php new file mode 100644 index 0000000..21cc9e0 --- /dev/null +++ b/tests/GoogleAnalytics/CampaignTracking/TrackerTest.php @@ -0,0 +1,54 @@ +assertEquals('123456789', $tracker->getDomainHash()); + + $this->assertEquals('1891678824', $tracker->getVisitorId()); + + $this->assertEquals(1357877198, $tracker->getInitialVisit()->getTimestamp()); + + $this->assertEquals(1357877198, $tracker->getPreviousVisit()->getTimestamp()); + + $this->assertEquals(1357877198, $tracker->getCurrentVisit()->getTimestamp()); + + $this->assertEquals(1, $tracker->getPagesViewed()); + + $this->assertEquals(1357877198, $tracker->getCurrentSessionStart()->getTimestamp()); + + $this->assertEquals(1345235917, $tracker->getDateTime()->getTimestamp()); + + $this->assertEquals(2, $tracker->getSessionNumber()); + + $this->assertEquals(4, $tracker->getCampaignNumber()); + + $this->assertEquals('google', $tracker->getCampaignSource()); + + $this->assertEquals('organic', $tracker->getCampaignMedium()); + + $this->assertEquals('keyword1 keyword2', $tracker->getCampaignTerm()); + + $this->assertEquals(2, count($tracker->getKeywords())); + } + + public function provider() { + return array( + array( + array( + '__utma' => '123456789.1891678824.1357877198.1357877198.1357877198.1', + '__utmb' => '123456789.1.10.1357877198', + '__utmc' => '123456789', + '__utmv' => '123456789.|2=AnotherVar=AnotherValue=1', + '__utmz' => '123456789.1345235917.2.4.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=keyword1 keyword2' + ) + ) + ); + } + +} diff --git a/tests/GoogleAnalytics/CampaignTracking/Utm/ATest.php b/tests/GoogleAnalytics/CampaignTracking/Utm/ATest.php new file mode 100644 index 0000000..315341c --- /dev/null +++ b/tests/GoogleAnalytics/CampaignTracking/Utm/ATest.php @@ -0,0 +1,30 @@ +assertEquals('123456789', $utma->getDomainHash()); + + $this->assertEquals(1891678824, $utma->getVisitorId()); + + $this->assertEquals(1357877198, $utma->getInitialVisit()->getTimestamp()); + + $this->assertEquals(1357877198, $utma->getPreviousVisit()->getTimestamp()); + + $this->assertEquals(1357877198, $utma->getCurrentVisit()->getTimestamp()); + + $this->assertEquals(1, $utma->getSessionCount()); + } + + public function provider() { + return array( + array('123456789.1891678824.1357877198.1357877198.1357877198.1') + ); + } + +} diff --git a/tests/GoogleAnalytics/CampaignTracking/Utm/BTest.php b/tests/GoogleAnalytics/CampaignTracking/Utm/BTest.php new file mode 100644 index 0000000..b6bf96b --- /dev/null +++ b/tests/GoogleAnalytics/CampaignTracking/Utm/BTest.php @@ -0,0 +1,24 @@ +assertEquals('123456789', $utmb->getDomainHash()); + + $this->assertEquals(1, $utmb->getPagesViewed()); + + $this->assertEquals(1357877198, $utmb->getCurrentSessionStart()->getTimestamp()); + } + + public function provider() { + return array( + array('123456789.1.10.1357877198') + ); + } + +} diff --git a/tests/GoogleAnalytics/CampaignTracking/Utm/CTest.php b/tests/GoogleAnalytics/CampaignTracking/Utm/CTest.php new file mode 100644 index 0000000..4a3bf02 --- /dev/null +++ b/tests/GoogleAnalytics/CampaignTracking/Utm/CTest.php @@ -0,0 +1,20 @@ +assertEquals('123456789', $utmc->getDomainHash()); + } + + public function provider() { + return array( + array('123456789') + ); + } + +} diff --git a/tests/GoogleAnalytics/CampaignTracking/Utm/VTest.php b/tests/GoogleAnalytics/CampaignTracking/Utm/VTest.php new file mode 100644 index 0000000..1f99c24 --- /dev/null +++ b/tests/GoogleAnalytics/CampaignTracking/Utm/VTest.php @@ -0,0 +1,29 @@ +assertEquals('123456789', $utmv->getDomainHash()); + + $this->assertEquals(2, count($utmv->getCustomVars())); + + $custom_vars = $utmv->getCustomVars(); + + $this->assertEquals(1, $custom_vars[0]->getSlotNumber()); + $this->assertEquals('MyVar', $custom_vars[0]->getName()); + $this->assertEquals('TheValue', $custom_vars[0]->getValue()); + $this->assertEquals(1, $custom_vars[0]->getScope()); + } + + public function provider() { + return array( + array('123456789.|1=MyVar=TheValue=1^2=AnotherVar=AnotherValue=1'), + ); + } + +} diff --git a/tests/GoogleAnalytics/CampaignTracking/Utm/ZTest.php b/tests/GoogleAnalytics/CampaignTracking/Utm/ZTest.php new file mode 100644 index 0000000..7329ea6 --- /dev/null +++ b/tests/GoogleAnalytics/CampaignTracking/Utm/ZTest.php @@ -0,0 +1,59 @@ +assertEquals('123456789', $utmz->getDomainHash()); + + $this->assertEquals(1357877198, $utmz->getDateTime()->getTimestamp()); + + $this->assertEquals(1, $utmz->getSessionNumber()); + + $this->assertEquals(2, $utmz->getCampaignNumber()); + + $this->assertEquals('(direct)', $utmz->getCampaignSource()); + + $this->assertEquals('(none)', $utmz->getCampaignMedium()); + } + + public function directProvider() { + return array( + array('123456789.1357877198.1.2.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)') + ); + } + + /** + * @dataProvider organicProvider + */ + public function testParseOrganic($__utmz) { + $utmz = new GoogleAnalytics\CampaignTracking\Utm\Z($__utmz); + + $this->assertEquals('123456789', $utmz->getDomainHash()); + + $this->assertEquals(1345235917, $utmz->getDateTime()->getTimestamp()); + + $this->assertEquals(2, $utmz->getSessionNumber()); + + $this->assertEquals(4, $utmz->getCampaignNumber()); + + $this->assertEquals('google', $utmz->getCampaignSource()); + + $this->assertEquals('organic', $utmz->getCampaignMedium()); + + $this->assertEquals('(organic)', $utmz->getCampaignName()); + + $this->assertEquals(2, count($utmz->getKeywords())); + } + + public function organicProvider() { + return array( + array('123456789.1345235917.2.4.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=keyword1 keyword2') + ); + } + +} From a48244f7bf5ed072f586befc5b021e18fb570be8 Mon Sep 17 00:00:00 2001 From: Kiosk Date: Thu, 28 Aug 2014 16:46:50 -0500 Subject: [PATCH 3/4] Fix typo in example. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b3d560f..010d168 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ __utmz - Expires 6 months from set/update. Stores campaign attribution informat ```php getCampaignMedium() . "\n"; echo $tracker->getCampaignSource() . "\n"; From 51c546c4e99202721719f98bdf3bb42b542b25fd Mon Sep 17 00:00:00 2001 From: Roger Webb Date: Thu, 11 Dec 2014 01:21:35 +0000 Subject: [PATCH 4/4] Added composer.json --- composer.json | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..54f3e8d --- /dev/null +++ b/composer.json @@ -0,0 +1,3 @@ +{ + "name": "RogerWebb/Google-Analytics-PHP-cookie-parser" +}