Skip to content

Commit b6c9c8d

Browse files
committed
Matomo Magento, V1.0.0
1 parent 2102707 commit b6c9c8d

File tree

29 files changed

+655
-760
lines changed

29 files changed

+655
-760
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Matomo.
2+
3+
Matomo Module based on adrianspeyer/Piwik-for-Magento
4+
5+
## Installation
6+
7+
### Modgit: [https://github.com/jreinke/modgit](https://github.com/jreinke/modgit)
8+
9+
$ cd /path/to/magento
10+
$ modgit init
11+
$ modgit add installments https://github.com/contardi/installments.git
12+
13+
### Manual:
14+
15+
- Download [latest version](https://github.com/contardi/matomo-magento/archive/master.zip) .
16+
- Unzip copy and paste the files.
17+
- Clear all caches.
18+
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
/**
3+
*
4+
* Based on Piwik Extension for Magento created by Adrian Speyer
5+
*
6+
* @category Matomo
7+
* @package Matomo_Analytics
8+
* @copyright Copyright (c) 2018 Thiago Contardi.
9+
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
10+
*
11+
*/
12+
13+
class Matomo_Analytics_Block_Script extends Mage_Core_Block_Template
14+
{
15+
/**
16+
* Get a specific page name (may be customized via layout)
17+
*
18+
* @return string|null
19+
*/
20+
public function getPageName()
21+
{
22+
return $this->_getData('page_name');
23+
}
24+
25+
/**
26+
* Render information about specified orders and their items
27+
* http://piwik.org/docs/ecommerce-analytics/
28+
*/
29+
public function getOrdersTrackingCode()
30+
{
31+
$orderIds = $this->getOrderIds();
32+
if (empty($orderIds) || !is_array($orderIds)) {
33+
return;
34+
}
35+
36+
/** @var Mage_Sales_Model_Resource_Order_Collection $collection */
37+
$collection = Mage::getResourceModel('sales/order_collection')
38+
->addFieldToFilter('entity_id', array('in' => $orderIds));
39+
$result = array();
40+
41+
/** @var Mage_Sales_Model_Order $order */
42+
foreach ($collection as $order) {
43+
/** @var Mage_Sales_Model_Order_Item $item */
44+
foreach ($order->getAllVisibleItems() as $item) {
45+
46+
//get category name
47+
$productId = $item->product_id;
48+
49+
/** @var Mage_Catalog_Model_Product $product */
50+
$product = Mage::getModel('catalog/product')->load($productId);
51+
$categoryName = '';
52+
$categoryIds = $product->getCategoryIds();
53+
54+
if (!empty($categoryIds)) {
55+
$categoryId = $categoryIds[0];
56+
/** @var Mage_Catalog_Model_Category $category */
57+
$category = Mage::getModel('catalog/category')->load($categoryId);
58+
$categoryName = $category->getName();
59+
}
60+
61+
if ($item->getQtyOrdered()) {
62+
$qty = number_format($item->getQtyOrdered(), 0, '.', '');
63+
} else {
64+
$qty = '0';
65+
}
66+
$result[] = sprintf('_paq.push([\'addEcommerceItem\', \'%s\', \'%s\', \'%s\', %s, %s]);',
67+
$this->jsQuoteEscape($item->getSku()),
68+
$this->jsQuoteEscape($item->getName()),
69+
$categoryName,
70+
$item->getBasePrice(),
71+
$qty
72+
);
73+
74+
}
75+
76+
if ($order->getGrandTotal()) {
77+
$subtotal = $order->getGrandTotal() - $order->getShippingAmount() - $order->getShippingTaxAmount();
78+
} else {
79+
$subtotal = '0.00';
80+
}
81+
$result[] = sprintf("_paq.push(['trackEcommerceOrder' , '%s', %s, %s, %s, %s]);",
82+
$order->getIncrementId(),
83+
$order->getBaseGrandTotal(),
84+
$subtotal,
85+
$order->getBaseTaxAmount(),
86+
$order->getBaseShippingAmount()
87+
);
88+
}
89+
return implode("\n", $result);
90+
}
91+
92+
/**
93+
* Render information when cart updated
94+
* http://piwik.org/docs/ecommerce-analytics/
95+
*/
96+
public function getEcommerceCartUpdate()
97+
{
98+
$result = array();
99+
100+
/** @var Mage_Checkout_Model_Cart $cart */
101+
$cart = Mage::getModel('checkout/cart');
102+
/** @var Mage_Sales_Model_Quote $quote */
103+
$quote = $cart->getQuote();
104+
105+
/** @var Mage_Sales_Model_Quote_Item $cartItem */
106+
foreach ($quote->getAllVisibleItems() as $cartItem) {
107+
108+
//get category name
109+
$productId = $cartItem->product_id;
110+
111+
/** @var Mage_Catalog_Model_Product $product */
112+
$product = Mage::getModel('catalog/product')->load($productId);
113+
$categoryName = '';
114+
$categoryIds = $product->getCategoryIds();
115+
if (!empty($categoryIds)) {
116+
$categoryId = $categoryIds[0];
117+
/** @var Mage_Catalog_Model_Category $category */
118+
$category = Mage::getModel('catalog/category')->load($categoryId);
119+
$categoryName = $category->getName();
120+
}
121+
$productName = $cartItem->getName();
122+
$productName = str_replace('"', "", $productName);
123+
124+
if ($cartItem->getPrice() == 0 || $cartItem->getPrice() < 0.00001):
125+
continue;
126+
endif;
127+
128+
$result[] = sprintf(
129+
"_paq.push(['addEcommerceItem', '%s', '%s', '%s', %s, %s]);",
130+
$this->jsQuoteEscape($cartItem->getSku()),
131+
$this->jsQuoteEscape($productName),
132+
$categoryName,
133+
$cartItem->getPrice(),
134+
$cartItem->getQty()
135+
);
136+
}
137+
138+
//total in cart
139+
$grandTotal = $quote->getGrandTotal();
140+
if ($grandTotal != 0) {
141+
$result[] = sprintf("_paq.push(['trackEcommerceCartUpdate', %s]);", $grandTotal);
142+
}
143+
144+
return implode("\n", $result);
145+
}
146+
147+
/**
148+
* Render information when product page view
149+
* http://piwik.org/docs/ecommerce-analytics/
150+
*/
151+
public function getProductPageview()
152+
{
153+
/** @var Mage_Catalog_Model_Product $product */
154+
$product = Mage::registry('current_product');
155+
156+
if ($product instanceof Mage_Catalog_Model_Product) {
157+
158+
$categoryName = '';
159+
$categoryIds = $product->getCategoryIds();
160+
if (!empty($categoryIds)) {
161+
$categoryId = $categoryIds[0];
162+
/** @var Mage_Catalog_Model_Category $category */
163+
$category = Mage::getModel('catalog/category')->load($categoryId);
164+
$categoryName = $category->getName();
165+
}
166+
$productName = $product->getName();
167+
168+
return sprintf(
169+
"_paq.push(['setEcommerceView', '%s', '%s', '%s', %s]);",
170+
$this->jsQuoteEscape($product->getSku()),
171+
$this->jsQuoteEscape($productName),
172+
$categoryName,
173+
$product->getFinalPrice()
174+
);
175+
}
176+
return '';
177+
}
178+
179+
/**
180+
* Render information of category view
181+
* http://piwik.org/docs/ecommerce-analytics/
182+
*/
183+
public function getCategoryPageview()
184+
{
185+
$currentCategory = Mage::registry('current_category');
186+
187+
if ($currentCategory instanceof Mage_Catalog_Model_Category) {
188+
return sprintf("_paq.push(['setEcommerceView', false, false, '%s']);", $currentCategory->getName());
189+
}
190+
191+
return '';
192+
}
193+
194+
/**
195+
* Render Piwik tracking scripts
196+
*
197+
* @return string
198+
*/
199+
public function _toHtml()
200+
{
201+
if (!Mage::helper('matomoanalytics')->isEnabled()) {
202+
return '';
203+
}
204+
return parent::_toHtml();
205+
}
206+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
*
4+
* Based on Piwik Extension for Magento created by Adrian Speyer
5+
*
6+
* @category Matomo
7+
* @package Matomo_Analytics
8+
* @copyright Copyright (c) 2018 Thiago Contardi.
9+
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
10+
*
11+
*/
12+
13+
14+
class Matomo_Analytics_Helper_Data extends Mage_Core_Helper_Abstract
15+
{
16+
/**
17+
* Config paths for using throughout the code
18+
*/
19+
const XML_PATH_ACTIVE = 'matomo/analytics/active';
20+
const XML_PATH_SITE = 'matomo/analytics/site';
21+
const XML_PATH_INSTALL = 'matomo/analytics/install';
22+
const XML_PATH_TOKEN = 'matomo/analytics/token';
23+
24+
/**
25+
*
26+
* @param mixed $store
27+
* @return bool
28+
*/
29+
public function isEnabled($store = null)
30+
{
31+
$siteId = Mage::getStoreConfig(self::XML_PATH_SITE, $store);
32+
return $siteId && Mage::getStoreConfigFlag(self::XML_PATH_ACTIVE, $store);
33+
}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
*
4+
* Based on Piwik Extension for Magento created by Adrian Speyer
5+
*
6+
* @category Matomo
7+
* @package Matomo_Analytics
8+
* @copyright Copyright (c) 2018 Thiago Contardi.
9+
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
10+
*
11+
*/
12+
13+
14+
class Matomo_Analytics_Model_Observer
15+
{
16+
/**
17+
* Add order information into Piwik block to render on checkout success pages
18+
*
19+
* @param Varien_Event_Observer $observer
20+
*/
21+
public function setMatomoAnalyticsOnOrderSuccessPageView(Varien_Event_Observer $observer)
22+
{
23+
if (Mage::helper('matomoanalytics')->isEnabled()) {
24+
$orderIds = $observer->getEvent()->getOrderIds();
25+
26+
if (empty($orderIds) || !is_array($orderIds)) {
27+
return;
28+
}
29+
30+
$block = Mage::app()->getFrontController()->getAction()->getLayout()->getBlock('matomo.scripts');
31+
if ($block) {
32+
$block->setOrderIds($orderIds);
33+
}
34+
}
35+
}
36+
}
37+
38+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
*
4+
* Based on Piwik Extension for Magento created by Adrian Speyer
5+
*
6+
* @category Matomo
7+
* @package Matomo_Analytics
8+
* @copyright Copyright (c) 2018 Thiago Contardi.
9+
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
10+
*
11+
*/
12+
13+
class Matomo_Analytics_Adminhtml_IndexController
14+
extends Mage_Adminhtml_Controller_Action
15+
{
16+
public function indexAction()
17+
{
18+
$this->loadLayout();
19+
20+
$siteId = Mage::getStoreConfig(Matomo_Analytics_Helper_Data::XML_PATH_SITE);
21+
$installPath = Mage::getStoreConfig(Matomo_Analytics_Helper_Data::XML_PATH_INSTALL);
22+
$token = Mage::getStoreConfig(Matomo_Analytics_Helper_Data::XML_PATH_TOKEN);
23+
24+
if ($token && $siteId) {
25+
26+
$params = '?module=Widgetize' .
27+
'&action=iframe' .
28+
'&moduleToWidgetize=Dashboard' .
29+
'&actionToWidgetize=index' .
30+
'&idSite=' . $siteId .
31+
'&period=week' .
32+
'&date=yesterday' .
33+
'&token_auth=' . $token;
34+
35+
$block = $this->getLayout()->createBlock('core/text', 'matomo-block')->setText(
36+
'<iframe src="' . $installPath . '/index.php' . $params . '"
37+
frameborder="0"
38+
marginheight="0"
39+
marginwidth="0"
40+
width="100%"
41+
height="1000px"></iframe>'
42+
);
43+
44+
} else {
45+
$block = $this->getLayout()->createBlock('core/text', 'matomo-block')->setText(
46+
$this->__('You are missing your Matomo Token Auth Key.')
47+
);
48+
}
49+
50+
$this->_addContent($block);
51+
$this->_setActiveMenu('matomo_menu')->renderLayout();
52+
53+
}
54+
}

0 commit comments

Comments
 (0)