00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_GoogleAnalytics_Block_Ga extends Mage_Core_Block_Text
00036 {
00037
00038
00039
00040
00041
00042 public function getQuoteOrdersHtml()
00043 {
00044 $quote = $this->getQuote();
00045 if (!$quote) {
00046 return '';
00047 }
00048
00049 if ($quote instanceof Mage_Sales_Model_Quote) {
00050 $quoteId = $quote->getId();
00051 } else {
00052 $quoteId = $quote;
00053 }
00054
00055 if (!$quoteId) {
00056 return '';
00057 }
00058
00059 $orders = Mage::getResourceModel('sales/order_collection')
00060 ->addAttributeToFilter('quote_id', $quoteId)
00061 ->load();
00062
00063 $html = '';
00064 foreach ($orders as $order) {
00065 $html .= $this->setOrder($order)->getOrderHtml();
00066 }
00067
00068 return $html;
00069 }
00070
00071
00072
00073
00074
00075
00076 public function getOrderHtml()
00077 {
00078
00079 $order = $this->getOrder();
00080 if (!$order) {
00081 return '';
00082 }
00083
00084 if (!$order instanceof Mage_Sales_Model_Order) {
00085 $order = Mage::getModel('sales/order')->load($order);
00086 }
00087
00088 if (!$order) {
00089 return '';
00090 }
00091
00092 $address = $order->getBillingAddress();
00093
00094 $html = '<script type="text/javascript">' . "\n";
00095 $html .= "//<![CDATA[\n";
00096 $html .= 'pageTracker._addTrans(';
00097 $html .= '"' . $order->getIncrementId() . '",';
00098 $html .= '"' . $order->getAffiliation() . '",';
00099 $html .= '"' . $order->getBaseGrandTotal() . '",';
00100 $html .= '"' . $order->getBaseTaxAmount() . '",';
00101 $html .= '"' . $order->getBaseShippingAmount() . '",';
00102 $html .= '"' . $this->jsQuoteEscape($address->getCity(), '"') . '",';
00103 $html .= '"' . $this->jsQuoteEscape($address->getRegion(), '"') . '",';
00104 $html .= '"' . $this->jsQuoteEscape($address->getCountry(), '"') . '"';
00105 $html .= ');' . "\n";
00106
00107 foreach ($order->getAllItems() as $item) {
00108 if ($item->getParentItemId()) {
00109 continue;
00110 }
00111
00112 $html .= 'pageTracker._addItem(';
00113 $html .= '"' . $order->getIncrementId() . '",';
00114 $html .= '"' . $this->jsQuoteEscape($item->getSku(), '"') . '",';
00115 $html .= '"' . $this->jsQuoteEscape($item->getName(), '"') . '",';
00116 $html .= '"' . $item->getCategory() . '",';
00117 $html .= '"' . $item->getBasePrice() . '",';
00118 $html .= '"' . $item->getQtyOrdered() . '"';
00119 $html .= ');' . "\n";
00120 }
00121
00122 $html .= 'pageTracker._trackTrans();' . "\n";
00123 $html .= '//]]>';
00124 $html .= '</script>';
00125
00126 return $html;
00127 }
00128
00129
00130
00131
00132
00133
00134 public function getAccount()
00135 {
00136 if (!$this->hasData('account')) {
00137 $this->setAccount(Mage::getStoreConfig('google/analytics/account'));
00138 }
00139 return $this->getData('account');
00140 }
00141
00142
00143
00144
00145
00146
00147 public function getPageName()
00148 {
00149 if (!$this->hasData('page_name')) {
00150
00151
00152
00153
00154 $this->setPageName(Mage::getSingleton('core/url')->escape($_SERVER['REQUEST_URI']));
00155 }
00156 return $this->getData('page_name');
00157 }
00158
00159
00160
00161
00162
00163
00164 protected function _toHtml()
00165 {
00166 if (!Mage::getStoreConfigFlag('google/analytics/active')) {
00167 return '';
00168 }
00169
00170 $this->addText('
00171 <!-- BEGIN GOOGLE ANALYTICS CODE -->
00172 <script type="text/javascript">
00173 //<![CDATA[
00174 var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
00175 document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
00176 //]]>
00177 </script>
00178 <script type="text/javascript">
00179 //<![CDATA[
00180 var pageTracker = _gat._getTracker("' . $this->getAccount() . '");
00181 pageTracker._trackPageview("'.$this->getPageName().'");
00182 //]]>
00183 </script>
00184 <!-- END GOOGLE ANALYTICS CODE -->
00185 ');
00186
00187 $this->addText($this->getQuoteOrdersHtml());
00188
00189 if ($this->getGoogleCheckout()) {
00190 $protocol = Mage::app()->getStore()->isCurrentlySecure() ? 'https' : 'http';
00191 $this->addText('<script src="'.$protocol.'://checkout.google.com/files/digital/ga_post.js" type="text/javascript"></script>');
00192 }
00193
00194 return parent::_toHtml();
00195 }
00196 }