Permalink
Browse files

Added support for 1.4.1.1+ and optimized asynchronous GA tracking sni…

…ppet.
  • Loading branch information...
ptz0n committed May 25, 2012
1 parent 1d6eafb commit 3e3ee6af0b0d8a20402e5e13462355dd5d56b81e
Showing with 90 additions and 0 deletions.
  1. +90 −0 app/code/community/Improove/GoogleAnalytics/Block/Ga.php
@@ -12,6 +12,8 @@ class Improove_GoogleAnalytics_Block_Ga extends Mage_GoogleAnalytics_Block_Ga
/**
* Retrieve Order Data HTML with currency conversion
*
+ * @deprecated after 1.4.1.1
+ * self::_getOrdersTrackingCode()
* @return string
*/
public function getOrderHtml()
@@ -68,4 +70,92 @@ public function getOrderHtml()
return $html;
}
+
+ /**
+ * 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 "var _gaq = [['_setAccount', '{$this->jsQuoteEscape($accountId)}'], ['_trackPageview'{$optPageURL}]];";
+ }
+
+ /**
+ * Render information about specified orders and their items with currency conversion
+ *
+ * @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();
+ }
+
+ $baseToGlobalRate = $order->getBaseToGlobalRate();
+
+ $result[] = sprintf("_gaq.push(['_addTrans', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s']);",
+ $order->getIncrementId(), Mage::app()->getStore()->getFrontendName(), $order->getBaseGrandTotal()*$baseToGlobalRate,
+ $order->getBaseTaxAmount()*$baseToGlobalRate, $order->getBaseShippingAmount()*$baseToGlobalRate,
+ $this->jsQuoteEscape($address->getCity()),
+ $this->jsQuoteEscape($address->getRegion()),
+ $this->jsQuoteEscape($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()*$baseToGlobalRate, $item->getQtyOrdered());
+ }
+ $result[] = "_gaq.push(['_trackTrans']);";
+ }
+ return implode("\n", $result);
+ }
+
+ /**
+ * Render optimized asynchronous GA tracking snippet
+ *
+ * @link http://mathiasbynens.be/notes/async-analytics-snippet
+ * @return string
+ */
+ protected function _toHtml()
+ {
+ if (!Mage::helper('googleanalytics')->isGoogleAnalyticsAvailable()) {
+ return '';
+ }
+ $accountId = Mage::getStoreConfig(Mage_GoogleAnalytics_Helper_Data::XML_PATH_ACCOUNT);
+ return '
+<script>
+ ' . $this->_getPageTrackingCode($accountId) . '
+ (function(d, t) {
+ var g = d.createElement(t),
+ s = d.getElementsByTagName(t)[0];
+ g.src = \'//www.google-analytics.com/ga.js\';
+ s.parentNode.insertBefore(g, s);
+ }(document, \'script\'));
+ ' . $this->_getOrdersTrackingCode() . '
+</script>
+';
+ }
}

0 comments on commit 3e3ee6a

Please sign in to comment.