From 6c8490f0d02c8433255315733cbd501a7cb39178 Mon Sep 17 00:00:00 2001 From: snapshotpl Date: Sun, 13 Oct 2013 12:06:26 +0200 Subject: [PATCH 01/10] Revrite view helper, fixes, tweaks, autoformat... --- Module.php | 8 +- .../Analytics/CustomVariable.php | 33 ++- .../Analytics/Ecommerce/Item.php | 26 +- .../Analytics/Ecommerce/Transaction.php | 62 ++--- src/SlmGoogleAnalytics/Analytics/Event.php | 35 +-- src/SlmGoogleAnalytics/Analytics/Tracker.php | 58 ++--- .../View/Helper/GoogleAnalytics.php | 245 ++++++++++++------ .../Analytics/CustomeVariableTest.php | 17 +- .../Analytics/Ecommerce/ItemTest.php | 15 +- .../Analytics/Ecommerce/TransactionTest.php | 13 +- .../Analytics/EventTest.php | 17 +- .../Analytics/TrackerTest.php | 14 +- .../View/Helper/GoogleAnalyticsTest.php | 119 ++++----- 13 files changed, 358 insertions(+), 304 deletions(-) diff --git a/Module.php b/Module.php index 2da1c4a..fad0bca 100644 --- a/Module.php +++ b/Module.php @@ -1,4 +1,5 @@ array( + 'aliases' => array( 'google-analytics' => 'SlmGoogleAnalytics\Analytics\Tracker', ), 'factories' => array( @@ -107,7 +109,7 @@ public function getServiceConfig() if (isset($config['allow_linker'])) { $tracker->setAllowLinker($config['allow_linker']); } - + if (true === $config['anonymize_ip']) { $tracker->setAnonymizeIp(true); } diff --git a/src/SlmGoogleAnalytics/Analytics/CustomVariable.php b/src/SlmGoogleAnalytics/Analytics/CustomVariable.php index 0fd4609..0f68756 100644 --- a/src/SlmGoogleAnalytics/Analytics/CustomVariable.php +++ b/src/SlmGoogleAnalytics/Analytics/CustomVariable.php @@ -40,16 +40,16 @@ class CustomVariable { - const SCOPE_VISITOR = 1; - const SCOPE_SESSION = 2; + const SCOPE_VISITOR = 1; + const SCOPE_SESSION = 2; const SCOPE_PAGE_LEVEL = 3; - + protected $index; protected $name; protected $value; protected $scope; - public function __construct ($index, $name, $value, $scope = self::SCOPE_PAGE_LEVEL) + public function __construct($index, $name, $value, $scope = self::SCOPE_PAGE_LEVEL) { $this->setIndex($index); $this->setName($name); @@ -61,16 +61,15 @@ public function setIndex($index) { if (!is_int($index)) { throw new InvalidArgumentException( - sprintf( - 'Index must be of type integer, %s given', - gettype($index) - ) + sprintf( + 'Index must be of type integer, %s given', gettype($index) + ) ); - } - + } + $this->index = $index; } - + public function getIndex() { return $this->index; @@ -103,16 +102,15 @@ public function setScope($scope) self::SCOPE_SESSION, self::SCOPE_PAGE_LEVEL ); - + if (!in_array($scope, $allowed, true)) { throw new InvalidArgumentException( - sprintf( - 'Invalid value given for scope. Acceptable values are: %s.', - implode(', ', $allowed) - ) + sprintf( + 'Invalid value given for scope. Acceptable values are: %s.', implode(', ', $allowed) + ) ); } - + $this->scope = $scope; } @@ -120,5 +118,4 @@ public function getScope() { return $this->scope; } - } \ No newline at end of file diff --git a/src/SlmGoogleAnalytics/Analytics/Ecommerce/Item.php b/src/SlmGoogleAnalytics/Analytics/Ecommerce/Item.php index 5485879..5f71494 100644 --- a/src/SlmGoogleAnalytics/Analytics/Ecommerce/Item.php +++ b/src/SlmGoogleAnalytics/Analytics/Ecommerce/Item.php @@ -44,10 +44,10 @@ class Item protected $sku; protected $price; protected $quantity; - protected $product; - protected $category; + protected $product = ''; + protected $category = ''; - public function __construct ($sku, $price, $quantity, $product = null, $category = null) + public function __construct($sku, $price, $quantity, $product = null, $category = null) { $this->setSku($sku); $this->setPrice($price); @@ -62,52 +62,52 @@ public function __construct ($sku, $price, $quantity, $product = null, $category } } - public function getSku () + public function getSku() { return $this->sku; } - public function setSku ($sku) + public function setSku($sku) { $this->sku = $sku; } - public function getProduct () + public function getProduct() { return $this->product; } - public function setProduct ($product) + public function setProduct($product) { $this->product = $product; } - public function getCategory () + public function getCategory() { return $this->category; } - public function setCategory ($category) + public function setCategory($category) { $this->category = $category; } - public function getPrice () + public function getPrice() { return $this->price; } - public function setPrice ($price) + public function setPrice($price) { $this->price = $price; } - public function getQuantity () + public function getQuantity() { return $this->quantity; } - public function setQuantity ($quantity) + public function setQuantity($quantity) { $this->quantity = $quantity; } diff --git a/src/SlmGoogleAnalytics/Analytics/Ecommerce/Transaction.php b/src/SlmGoogleAnalytics/Analytics/Ecommerce/Transaction.php index 99f595d..830d766 100644 --- a/src/SlmGoogleAnalytics/Analytics/Ecommerce/Transaction.php +++ b/src/SlmGoogleAnalytics/Analytics/Ecommerce/Transaction.php @@ -42,117 +42,111 @@ class Transaction { protected $id; - protected $affiliation; - protected $total; - protected $tax; - protected $shipping; - protected $city; - protected $state; - protected $country; + protected $affiliation = ''; + protected $total = ''; + protected $tax = ''; + protected $shipping = ''; + protected $city = ''; + protected $state = ''; + protected $country = ''; + protected $items = array(); - protected $items; - - public function __construct ($id, $total) + public function __construct($id, $total) { $this->setId($id); $this->setTotal($total); } - public function getId () + public function getId() { return $this->id; } - public function setId ($id) + public function setId($id) { $this->id = $id; } - public function getAffiliation () + public function getAffiliation() { return $this->affiliation; } - public function setAffiliation ($affiliation) + public function setAffiliation($affiliation) { $this->affiliation = $affiliation; } - public function getTotal () + public function getTotal() { return $this->total; } - public function setTotal ($total) + public function setTotal($total) { $this->total = $total; } - public function getTax () + public function getTax() { return $this->tax; } - public function setTax ($tax) + public function setTax($tax) { $this->tax = $tax; } - public function getShipping () + public function getShipping() { return $this->shipping; } - public function setShipping ($shipping) + public function setShipping($shipping) { $this->shipping = $shipping; } - public function getCity () + public function getCity() { return $this->city; } - public function setCity ($city) + public function setCity($city) { $this->city = $city; } - public function getState () + public function getState() { return $this->state; } - public function setState ($state) + public function setState($state) { $this->state = $state; } - public function getCountry () + public function getCountry() { return $this->country; } - public function setCountry ($country) + public function setCountry($country) { $this->country = $country; } - public function items () + public function getItems() { return $this->items; } - public function addItem (Item $item) + public function addItem(Item $item) { - if (null === $this->items) { - $this->items = array(); - } - $sku = $item->getSku(); if (array_key_exists($sku, $this->items)) { - $quantity = $this->items[$sku]->getQuantity() - + $item->getQuantity(); + $quantity = $this->items[$sku]->getQuantity() + $item->getQuantity(); $this->items[$sku]->setQuantity($quantity); } else { diff --git a/src/SlmGoogleAnalytics/Analytics/Event.php b/src/SlmGoogleAnalytics/Analytics/Event.php index 9ed24f6..6abcf5c 100644 --- a/src/SlmGoogleAnalytics/Analytics/Event.php +++ b/src/SlmGoogleAnalytics/Analytics/Event.php @@ -43,53 +43,58 @@ class Event { protected $category; protected $action; - protected $label; - protected $value; + protected $label = ''; + protected $value = ''; - public function __construct ($category, $action, $label = null, $value = null) + public function __construct($category, $action, $label = '', $value = '') { - $this->category = $category; - $this->action = $action; - $this->label = $label; - $this->value = $value; + $this->setCategory($category); + $this->setAction($action); + + if ($label !== null) { + $this->setLabel($label); + } + if ($value !== null) { + $this->setValue($value); + } } - public function getCategory () + public function getCategory() { return $this->category; } - public function setCategory ($category) + public function setCategory($category) { $this->category = $category; } - public function getAction () + public function getAction() { return $this->action; } - public function setAction ($action) + public function setAction($action) { $this->action = $action; } - public function getLabel () + public function getLabel() { return $this->label; } - public function setLabel ($label) + public function setLabel($label) { $this->label = $label; } - public function getValue () + public function getValue() { return $this->value; } - public function setValue ($value) + public function setValue($value) { $this->value = $value; } diff --git a/src/SlmGoogleAnalytics/Analytics/Tracker.php b/src/SlmGoogleAnalytics/Analytics/Tracker.php index 22e4566..e78c6e3 100644 --- a/src/SlmGoogleAnalytics/Analytics/Tracker.php +++ b/src/SlmGoogleAnalytics/Analytics/Tracker.php @@ -59,49 +59,45 @@ class Tracker * @var bool */ protected $enableTracking = true; - protected $enablePageTracking = true; - protected $allowLinker = false; protected $domainName; - protected $anonymizeIp = false; + protected $customVariables = array(); + protected $events = array(); + protected $transactions = array(); - protected $customVariables; - protected $events; - protected $transactions; - - public function __construct ($id) + public function __construct($id) { $this->setId($id); } - public function getId () + public function getId() { return $this->id; } - public function setId ($id) + public function setId($id) { $this->id = $id; } - public function enabled () + public function enabled() { return $this->enableTracking; } - public function setEnableTracking ($enable_tracking = true) + public function setEnableTracking($enable_tracking = true) { $this->enableTracking = (bool) $enable_tracking; } - public function enabledPageTracking () + public function enabledPageTracking() { return $this->enablePageTracking; } - public function setEnablePageTracking ($enable_page_tracking = true) + public function setEnablePageTracking($enable_page_tracking = true) { $this->enablePageTracking = (bool) $enable_page_tracking; } @@ -144,58 +140,44 @@ public function setAnonymizeIp($flag) $this->anonymizeIp = (bool) $flag; } - public function customVariables() + public function getCustomVariables() { return $this->customVariables; } - + public function addCustomVariable(CustomVariable $variable) { - if (null === $this->customVariables) { - $this->customVariables = array(); - } - $index = $variable->getIndex(); if (array_key_exists($index, $this->customVariables)) { throw new InvalidArgumentException( - 'Cannot add custom variable with index %d, it already exists', - $index + 'Cannot add custom variable with index %d, it already exists', $index ); } - + $this->customVariables[$index] = $variable; } - - public function events () + + public function getEvents() { return $this->events; } - public function addEvent (Event $event) + public function addEvent(Event $event) { - if (null === $this->events) { - $this->events = array(); - } - $this->events[] = $event; } - public function transactions () + public function getTransactions() { return $this->transactions; } - public function addTransaction (Transaction $transaction) + public function addTransaction(Transaction $transaction) { - if (null === $this->transactions) { - $this->transactions = array(); - } - $id = $transaction->getId(); if (array_key_exists($id, $this->transactions)) { throw new InvalidArgumentException(sprintf( - 'Cannot add transaction with id %s, it already exists', - $id + 'Cannot add transaction with id %s, it already exists', $id )); } diff --git a/src/SlmGoogleAnalytics/View/Helper/GoogleAnalytics.php b/src/SlmGoogleAnalytics/View/Helper/GoogleAnalytics.php index 59009f3..0a823b7 100644 --- a/src/SlmGoogleAnalytics/View/Helper/GoogleAnalytics.php +++ b/src/SlmGoogleAnalytics/View/Helper/GoogleAnalytics.php @@ -41,8 +41,9 @@ use Zend\View\Helper\AbstractHelper; use Zend\View\Helper\HeadScript; +use Zend\Json\Encoder; use SlmGoogleAnalytics\Analytics\Tracker; - +use SlmGoogleAnalytics\Analytics\Ecommerce\Transaction; use SlmGoogleAnalytics\Exception\RuntimeException; class GoogleAnalytics extends AbstractHelper @@ -62,124 +63,200 @@ class GoogleAnalytics extends AbstractHelper */ protected $rendered = false; - public function __construct (Tracker $tracker) + public function __construct(Tracker $tracker) { $this->tracker = $tracker; } - public function getContainer () + public function getContainerName() { return $this->container; } - public function setContainer ($container) + public function setContainer($container) { $this->container = $container; } - public function __invoke () + public function __invoke() { - // Do not render the GA twice - if ($this->rendered) { - return; - } - - // Do not render when tracker is disabled - $tracker = $this->tracker; - if (!$tracker->enabled()) { - return; - } - // We need to be sure $container->appendScript() can be called - $container = $this->view->plugin($this->getContainer()); + $container = $this->view->plugin($this->getContainerName()); if (!$container instanceof HeadScript) { throw new RuntimeException(sprintf( - 'Container %s does not extend HeadScript view helper', - $this->getContainer() + 'Container %s does not extend HeadScript view helper', $this->getContainerName() )); } - $script = "var _gaq = _gaq || [];\n"; - $script .= sprintf("_gaq.push(['_setAccount', '%s']);\n", - $tracker->getId()); + $script = $this->getScript(); - if ($tracker->getDomainName()) { - $script .= sprintf("_gaq.push(['_setDomainName', '%s']);\n", - $tracker->getDomainName());; + if (empty($script)) { + return; } - if ($tracker->getAllowLinker()) { - $script .= "_gaq.push(['_setAllowLinker', true]);\n"; - } + $container->appendScript($script); - if ($tracker->getAnonymizeIp()) { - $script .= "_gaq.push(['_gat._anonymizeIp']);\n"; - } + // Mark this GA as rendered + $this->rendered = true; + } - if (null !== ($customVariables = $tracker->customVariables())) { - foreach ($customVariables as $variable) { - $script .= sprintf("_gaq.push(['_setCustomVar', %d, '%s', '%s', %d]);\n", - $variable->getIndex(), - $variable->getName(), - $variable->getValue(), - $variable->getScope()); - } - } - - if ($tracker->enabledPageTracking()) { - $script .= "_gaq.push(['_trackPageview']);\n"; - } - - - if (null !== ($events = $tracker->events())) { - foreach ($events as $event) { - $script .= sprintf("_gaq.push(['_trackEvent', '%s', '%s', '%s', '%s']);\n", - $event->getCategory(), - $event->getAction(), - $event->getLabel() ?: '', - $event->getValue() ?: ''); - } + public function getScript() + { + // Do not render the GA twice + if ($this->rendered) { + return ''; } - if (null !== ($transactions = $tracker->transactions())) { - foreach ($transactions as $transaction) { - $script .= sprintf("_gaq.push(['_addTrans', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s']);\n", - $transaction->getId(), - $transaction->getAffiliation() ?: '', - $transaction->getTotal(), - $transaction->getTax() ?: '', - $transaction->getShipping() ?: '', - $transaction->getCity() ?: '', - $transaction->getState() ?: '', - $transaction->getCountry() ?: ''); - - if (null !== ($items = $transaction->items())) { - foreach ($items as $item) { - $script .= sprintf("_gaq.push(['_addItem', '%s', '%s', '%s', '%s', '%s', '%s']);\n", - $transaction->getId(), - $item->getSku() ?: '', - $item->getProduct() ?: '', - $item->getCategory() ?: '', - $item->getPrice(), - $item->getQuantity()); - } - } - } - - $script .= "_gaq.push(['_trackTrans']);"; + // Do not render when tracker is disabled + if (!$this->tracker->enabled()) { + return ''; } - $script .= <<