1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_GoogleAnalytics
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * GoogleAnalitics Page Block
30: *
31: * @category Mage
32: * @package Mage_GoogleAnalytics
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_GoogleAnalytics_Block_Ga extends Mage_Core_Block_Template
36: {
37: /**
38: * @deprecated after 1.4.1.1
39: * @see self::_getOrdersTrackingCode()
40: * @return string
41: */
42: public function getQuoteOrdersHtml()
43: {
44: return '';
45: }
46:
47: /**
48: * @deprecated after 1.4.1.1
49: * self::_getOrdersTrackingCode()
50: * @return string
51: */
52: public function getOrderHtml()
53: {
54: return '';
55: }
56:
57: /**
58: * @deprecated after 1.4.1.1
59: * @see _toHtml()
60: * @return string
61: */
62: public function getAccount()
63: {
64: return '';
65: }
66:
67: /**
68: * Get a specific page name (may be customized via layout)
69: *
70: * @return string|null
71: */
72: public function getPageName()
73: {
74: return $this->_getData('page_name');
75: }
76:
77: /**
78: * Render regular page tracking javascript code
79: * The custom "page name" may be set from layout or somewhere else. It must start from slash.
80: *
81: * @link http://code.google.com/apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._trackPageview
82: * @link http://code.google.com/apis/analytics/docs/gaJS/gaJSApi_gaq.html
83: * @param string $accountId
84: * @return string
85: */
86: protected function _getPageTrackingCode($accountId)
87: {
88: $pageName = trim($this->getPageName());
89: $optPageURL = '';
90: if ($pageName && preg_match('/^\/.*/i', $pageName)) {
91: $optPageURL = ", '{$this->jsQuoteEscape($pageName)}'";
92: }
93: return "
94: _gaq.push(['_setAccount', '{$this->jsQuoteEscape($accountId)}']);
95: _gaq.push(['_trackPageview'{$optPageURL}]);
96: ";
97: }
98:
99: /**
100: * Render information about specified orders and their items
101: *
102: * @link http://code.google.com/apis/analytics/docs/gaJS/gaJSApiEcommerce.html#_gat.GA_Tracker_._addTrans
103: * @return string
104: */
105: protected function _getOrdersTrackingCode()
106: {
107: $orderIds = $this->getOrderIds();
108: if (empty($orderIds) || !is_array($orderIds)) {
109: return;
110: }
111: $collection = Mage::getResourceModel('sales/order_collection')
112: ->addFieldToFilter('entity_id', array('in' => $orderIds))
113: ;
114: $result = array();
115: foreach ($collection as $order) {
116: if ($order->getIsVirtual()) {
117: $address = $order->getBillingAddress();
118: } else {
119: $address = $order->getShippingAddress();
120: }
121: $result[] = sprintf("_gaq.push(['_addTrans', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s']);",
122: $order->getIncrementId(),
123: $this->jsQuoteEscape(Mage::app()->getStore()->getFrontendName()),
124: $order->getBaseGrandTotal(),
125: $order->getBaseTaxAmount(),
126: $order->getBaseShippingAmount(),
127: $this->jsQuoteEscape(Mage::helper('core')->escapeHtml($address->getCity())),
128: $this->jsQuoteEscape(Mage::helper('core')->escapeHtml($address->getRegion())),
129: $this->jsQuoteEscape(Mage::helper('core')->escapeHtml($address->getCountry()))
130: );
131: foreach ($order->getAllVisibleItems() as $item) {
132: $result[] = sprintf("_gaq.push(['_addItem', '%s', '%s', '%s', '%s', '%s', '%s']);",
133: $order->getIncrementId(),
134: $this->jsQuoteEscape($item->getSku()), $this->jsQuoteEscape($item->getName()),
135: null, // there is no "category" defined for the order item
136: $item->getBasePrice(), $item->getQtyOrdered()
137: );
138: }
139: $result[] = "_gaq.push(['_trackTrans']);";
140: }
141: return implode("\n", $result);
142: }
143:
144: /**
145: * Render GA tracking scripts
146: *
147: * @return string
148: */
149: protected function _toHtml()
150: {
151: if (!Mage::helper('googleanalytics')->isGoogleAnalyticsAvailable()) {
152: return '';
153: }
154:
155: return parent::_toHtml();
156: }
157: }
158: