This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Piwik.php
190 lines (162 loc) · 6.27 KB
/
Piwik.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
*
* Piwik Extension for Magento created by Adrian Speyer
* Get Piwik at http://www.piwik.org - Open source web analytics
*
* PiwikAnalytics Page Block
*
* @category PiwikMage
* @package PiwikMage_PiwikAnalytics
* @copyright Copyright (c) 2012 Adrian Speyer. (http://www.adrianspeyer.com)
* @license @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
class PiwikMage_PiwikAnalytics_Block_Piwik extends Mage_Core_Block_Template
{
/**
* Get a specific page name (may be customized via layout)
*
* @return string|null
*/
public function getPageName()
{
return $this->_getData('page_name');
}
/**
* Render information about specified orders and their items
* http://piwik.org/docs/ecommerce-analytics/
*/
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) {
foreach ($order->getAllVisibleItems() as $item) {
//get category name
$productId = $item->product_id;
$product = Mage::getModel('catalog/product')->load($productId);
$categoryName = '';
$categoryIds = $product->getCategoryIds();
if (!empty($categoryIds)) {
$categoryId = $categoryIds[0];
$category = Mage::getModel('catalog/category')->load($categoryId);
$categoryName = $category->getName();
}
if ($item->getQtyOrdered()) {
$qty = number_format($item->getQtyOrdered(), 0, '.', '');
} else {
$qty = '0';
}
$result[] = sprintf("_paq.push(['addEcommerceItem', '%s', '%s', '%s', %s, %s]);",
$this->jsQuoteEscape($item->getSku()),
$this->jsQuoteEscape($item->getName()),
$categoryName,
$item->getBasePrice(),
$qty
);
}
foreach ($collection as $order) {
if ($order->getGrandTotal()) {
$subtotal = $order->getGrandTotal() - $order->getShippingAmount() - $order->getShippingTaxAmount();
} else {
$subtotal = '0.00';
}
$result[] = sprintf("_paq.push(['trackEcommerceOrder' , '%s', %s, %s, %s, %s]);",
$order->getIncrementId(),
$order->getBaseGrandTotal(),
$subtotal,
$order->getBaseTaxAmount(),
$order->getBaseShippingAmount()
);
}
}
return implode("\n", $result);
}
/**
* Render information when cart updated
* http://piwik.org/docs/ecommerce-analytics/
*/
protected function _getEcommerceCartUpdate()
{
$cart = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
foreach ($cart as $cartItem) {
//get category name
$productId = $cartItem->product_id;
$product = Mage::getModel('catalog/product')->load($productId);
$categoryName = '';
$categoryIds = $product->getCategoryIds();
if (!empty($categoryIds)) {
$categoryId = $categoryIds[0];
$category = Mage::getModel('catalog/category')->load($categoryId);
$categoryName = $category->getName();
}
$productName = $cartItem->getName();
$productName = str_replace('"', "", $productName);
if ($cartItem->getPrice() == 0 || $cartItem->getPrice() < 0.00001):
continue;
endif;
echo "_paq.push(['addEcommerceItem', " . json_encode($cartItem->getSku()) . ", " . json_encode($productName) . ", " . json_encode($categoryName) . ", " . $cartItem->getPrice() . ", " . $cartItem->getQty() . "]);";
echo "\n";
}
//total in cart
$grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
if ($grandTotal != 0) {
echo "_paq.push(['trackEcommerceCartUpdate', " . $grandTotal . "]);";
echo "\n";
}
}
/**
* Render information when product page view
* http://piwik.org/docs/ecommerce-analytics/
*/
protected function _getProductPageview()
{
$currentProduct = Mage::registry('current_product');
if (!($currentProduct instanceof Mage_Catalog_Model_Product)) {
return;
}
$productId = $currentProduct->getId();
$product = Mage::getModel('catalog/product')->load($productId);
$categoryName = '';
$categoryIds = $product->getCategoryIds();
if (!empty($categoryIds)) {
$categoryId = $categoryIds[0];
$category = Mage::getModel('catalog/category')->load($categoryId);
$categoryName = $category->getName();
}
$productName = $currentProduct->getName();
echo "_paq.push(['setEcommerceView', " . json_encode($currentProduct->getSku()) . ", " . json_encode($productName) . ", " . json_encode($categoryName) . ", " . $currentProduct->getPrice() . " ]);";
Mage::unregister('current_category');
}
/**
* Render information of category view
* http://piwik.org/docs/ecommerce-analytics/
*/
protected function _getCategoryPageview()
{
$currentCategory = Mage::registry('current_category');
if (!($currentCategory instanceof Mage_Catalog_Model_Category)) {
return;
}
echo "_paq.push(['setEcommerceView', false, false, " . json_encode($currentCategory->getName()) . "]);";
Mage::unregister('current_product');
}
/**
* Render Piwik tracking scripts
*
* @return string
*/
protected function _toHtml()
{
if (!Mage::helper('piwikanalytics')->isPiwikAnalyticsAvailable()) {
return '';
}
return parent::_toHtml();
}
}