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: * Google Analytics module observer
30: *
31: * @category Mage
32: * @package Mage_GoogleAnalytics
33: */
34: class Mage_GoogleAnalytics_Model_Observer
35: {
36: /**
37: * Whether the google checkout inclusion link was rendered by this observer instance
38: * @var bool
39: */
40: protected $_isGoogleCheckoutLinkAdded = false;
41:
42: /**
43: * Create Google Analytics block for success page view
44: *
45: * @deprecated after 1.3.2.3 Use setGoogleAnalyticsOnOrderSuccessPageView() method instead
46: * @param Varien_Event_Observer $observer
47: */
48: public function order_success_page_view($observer)
49: {
50: $this->setGoogleAnalyticsOnOrderSuccessPageView($observer);
51: }
52:
53: /**
54: * Add order information into GA block to render on checkout success pages
55: *
56: * @param Varien_Event_Observer $observer
57: */
58: public function setGoogleAnalyticsOnOrderSuccessPageView(Varien_Event_Observer $observer)
59: {
60: $orderIds = $observer->getEvent()->getOrderIds();
61: if (empty($orderIds) || !is_array($orderIds)) {
62: return;
63: }
64: $block = Mage::app()->getFrontController()->getAction()->getLayout()->getBlock('google_analytics');
65: if ($block) {
66: $block->setOrderIds($orderIds);
67: }
68: }
69:
70: /**
71: * Add google analytics tracking to google checkout shortcuts
72: *
73: * If there is at least one GC button on the page, there should be the script for GA/GC integration included
74: * a each shortcut should track submits to GA
75: * There should be no tracking if there is no GA available
76: * This method assumes that the observer instance is run as a "singleton" (through Mage::getSingleton())
77: *
78: * @param Varien_Event_Observer $observer
79: */
80: public function injectAnalyticsInGoogleCheckoutLink(Varien_Event_Observer $observer)
81: {
82: $block = $observer->getEvent()->getBlock();
83: if (!$block || !Mage::helper('googleanalytics')->isGoogleAnalyticsAvailable()) {
84: return;
85: }
86:
87: // make sure to track google checkout "onsubmit"
88: $onsubmitJs = $block->getOnsubmitJs();
89: $block->setOnsubmitJs($onsubmitJs . ($onsubmitJs ? '; ' : '') . '_gaq.push(function() {var pageTracker = _gaq._getAsyncTracker(); setUrchinInputCode(pageTracker);});');
90:
91: // add a link that includes google checkout/analytics script, to the first instance of the link block
92: if ($this->_isGoogleCheckoutLinkAdded) {
93: return;
94: }
95: $beforeHtml = $block->getBeforeHtml();
96: $protocol = Mage::app()->getStore()->isCurrentlySecure() ? 'https' : 'http';
97: $block->setBeforeHtml($beforeHtml . '<script src="' . $protocol
98: . '://checkout.google.com/files/digital/ga_post.js" type="text/javascript"></script>'
99: );
100: $this->_isGoogleCheckoutLinkAdded = true;
101: }
102: }
103: