*/ class Mage_GoogleAnalytics_Block_Ga extends Mage_Core_Block_Template { /** * @deprecated after 1.4.1.1 * @see self::_getOrdersTrackingCode() * @return string */ public function getQuoteOrdersHtml() { return ''; } /** * @deprecated after 1.4.1.1 * self::_getOrdersTrackingCode() * @return string */ public function getOrderHtml() { return ''; } /** * @deprecated after 1.4.1.1 * @see _toHtml() * @return string */ public function getAccount() { return ''; } /** * Get a specific page name (may be customized via layout) * * @return string|null */ public function getPageName() { return $this->_getData('page_name'); } /** * Render regular page tracking javascript code * The custom "page name" may be set from layout or somewhere else. It must start from slash. * * @link http://code.google.com/apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._trackPageview * @link http://code.google.com/apis/analytics/docs/gaJS/gaJSApi_gaq.html * @param string $accountId * @return string */ protected function _getPageTrackingCode($accountId) { $pageName = trim($this->getPageName()); $optPageURL = ''; if ($pageName && preg_match('/^\/.*/i', $pageName)) { $optPageURL = ", '{$this->jsQuoteEscape($pageName)}'"; } return " _gaq.push(['_setAccount', '{$this->jsQuoteEscape($accountId)}']); _gaq.push(['_trackPageview'{$optPageURL}]); "; } /** * Render information about specified orders and their items * * @link http://code.google.com/apis/analytics/docs/gaJS/gaJSApiEcommerce.html#_gat.GA_Tracker_._addTrans * @return string */ protected function _getOrdersTrackingCode() { $orderIds = $this->getOrderIds(); if (empty($orderIds) || !is_array($orderIds)) { return; } $collection = Mage::getResourceModel('sales/order_collection') ->addFieldToFilter('entity_id', array('in' => $orderIds)) ; $result = array(); foreach ($collection as $order) { if ($order->getIsVirtual()) { $address = $order->getBillingAddress(); } else { $address = $order->getShippingAddress(); } $result[] = sprintf("_gaq.push(['_addTrans', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s']);", $order->getIncrementId(), $this->jsQuoteEscape(Mage::app()->getStore()->getFrontendName()), $order->getBaseGrandTotal(), $order->getBaseTaxAmount(), $order->getBaseShippingAmount(), $this->jsQuoteEscape(Mage::helper('core')->escapeHtml($address->getCity())), $this->jsQuoteEscape(Mage::helper('core')->escapeHtml($address->getRegion())), $this->jsQuoteEscape(Mage::helper('core')->escapeHtml($address->getCountry())) ); foreach ($order->getAllVisibleItems() as $item) { $result[] = sprintf("_gaq.push(['_addItem', '%s', '%s', '%s', '%s', '%s', '%s']);", $order->getIncrementId(), $this->jsQuoteEscape($item->getSku()), $this->jsQuoteEscape($item->getName()), null, // there is no "category" defined for the order item $item->getBasePrice(), $item->getQtyOrdered() ); } $result[] = "_gaq.push(['_trackTrans']);"; } return implode("\n", $result); } /** * Render GA tracking scripts * * @return string */ protected function _toHtml() { if (!Mage::helper('googleanalytics')->isGoogleAnalyticsAvailable()) { return ''; } return parent::_toHtml(); } }