Skip to content

Commit ec6bcde

Browse files
committed
added typehints, minor refactorings, updated tracking code from piwik to matomo
1 parent 2950015 commit ec6bcde

File tree

7 files changed

+109
-112
lines changed

7 files changed

+109
-112
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"type": "magento-module",
44
"license": "GPL-3.0-or-later",
55
"description": "A Magento 1 extension that allows you to get ecommerce transaction data into Matomo analytics.",
6+
"require": {
7+
"php": "^7.4 || ^8.0"
8+
},
69
"require-dev": {
710
"roave/security-advisories": "dev-master",
811
"magento/marketplace-eqp": ">=4.0"

src/app/code/community/Matomo/Analytics/Block/Script.php

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
*
46
* Based on Piwik Extension for Magento created by Adrian Speyer
@@ -10,27 +12,25 @@
1012
*
1113
*/
1214

13-
class Matomo_Analytics_Block_Script extends Mage_Core_Block_Template
15+
final class Matomo_Analytics_Block_Script extends Mage_Core_Block_Template
1416
{
1517
/**
16-
* Get a specific page name (may be customized via layout)
17-
*
18-
* @return string|null
18+
* Get a specific page name (maybe customized via layout)
1919
*/
20-
public function getPageName()
20+
public function getPageName(): ?string
2121
{
2222
return $this->_getData('page_name');
2323
}
2424

2525
/**
2626
* Render information about specified orders and their items
27-
* http://piwik.org/docs/ecommerce-analytics/
27+
* @see https://matomo.org/guide/reports/ecommerce/
2828
*/
29-
public function getOrdersTrackingCode()
29+
public function getOrdersTrackingCode(): string
3030
{
3131
$orderIds = $this->getOrderIds();
3232
if (empty($orderIds) || !is_array($orderIds)) {
33-
return;
33+
return '';
3434
}
3535

3636
/** @var Mage_Sales_Model_Resource_Order_Collection $collection */
@@ -91,9 +91,9 @@ public function getOrdersTrackingCode()
9191

9292
/**
9393
* Render information when cart updated
94-
* http://piwik.org/docs/ecommerce-analytics/
94+
* @see https://matomo.org/guide/reports/ecommerce/
9595
*/
96-
public function getEcommerceCartUpdate()
96+
public function getEcommerceCartUpdate(): string
9797
{
9898
$result = array();
9999

@@ -146,15 +146,14 @@ public function getEcommerceCartUpdate()
146146

147147
/**
148148
* Render information when product page view
149-
* http://piwik.org/docs/ecommerce-analytics/
149+
* @see https://matomo.org/guide/reports/ecommerce/
150150
*/
151-
public function getProductPageview()
151+
public function getProductPageview(): string
152152
{
153153
/** @var Mage_Catalog_Model_Product $product */
154154
$product = Mage::registry('current_product');
155155

156156
if ($product instanceof Mage_Catalog_Model_Product) {
157-
158157
$categoryName = '';
159158
$categoryIds = $product->getCategoryIds();
160159
if (!empty($categoryIds)) {
@@ -173,14 +172,15 @@ public function getProductPageview()
173172
$product->getFinalPrice()
174173
);
175174
}
175+
176176
return '';
177177
}
178178

179179
/**
180180
* Render information of category view
181-
* http://piwik.org/docs/ecommerce-analytics/
181+
* @see https://matomo.org/guide/reports/ecommerce/
182182
*/
183-
public function getCategoryPageview()
183+
public function getCategoryPageview(): string
184184
{
185185
$currentCategory = Mage::registry('current_category');
186186

@@ -191,16 +191,45 @@ public function getCategoryPageview()
191191
return '';
192192
}
193193

194-
/**
195-
* Render Piwik tracking scripts
196-
*
197-
* @return string
198-
*/
199-
public function _toHtml()
194+
public function getSiteId(): string
195+
{
196+
return Mage::getStoreConfig(Matomo_Analytics_Helper_Data::XML_PATH_SITE);
197+
}
198+
199+
public function getInstallPath(): string
200+
{
201+
$installPath = Mage::getStoreConfig(Matomo_Analytics_Helper_Data::XML_PATH_INSTALL);
202+
203+
// remove https or https
204+
return preg_replace('/^https?:/', '', $installPath);
205+
}
206+
207+
public function getSearchResultCount(): int
200208
{
201-
if (!Mage::helper('matomoanalytics')->isEnabled()) {
209+
$count = 0;
210+
211+
if ($this->getRequest()->getControllerName() === 'result') {
212+
$queryText = $this->helper('catalogsearch')->getQuery()->getQueryText();
213+
$count = (int)$this->helper('catalogsearch')->getEngine()
214+
->getResultCollection()
215+
->addSearchFilter($queryText)
216+
->getSize();
217+
}
218+
219+
return $count;
220+
}
221+
222+
public function is404(): bool
223+
{
224+
return $this->getAction() === 'noRoute';
225+
}
226+
227+
public function _toHtml(): string
228+
{
229+
if (! $this->helper('matomoanalytics')->isEnabled()) {
202230
return '';
203231
}
232+
204233
return parent::_toHtml();
205234
}
206235
}
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
*
46
* Based on Piwik Extension for Magento created by Adrian Speyer
@@ -10,25 +12,19 @@
1012
*
1113
*/
1214

13-
14-
class Matomo_Analytics_Helper_Data extends Mage_Core_Helper_Abstract
15+
final class Matomo_Analytics_Helper_Data extends Mage_Core_Helper_Abstract
1516
{
16-
/**
17-
* Config paths for using throughout the code
18-
*/
1917
const XML_PATH_ACTIVE = 'matomo/analytics/active';
2018
const XML_PATH_SITE = 'matomo/analytics/site';
2119
const XML_PATH_INSTALL = 'matomo/analytics/install';
2220
const XML_PATH_TOKEN = 'matomo/analytics/token';
2321

2422
/**
25-
*
2623
* @param mixed $store
27-
* @return bool
2824
*/
29-
public function isEnabled($store = null)
25+
public function isEnabled($store = null): bool
3026
{
31-
$siteId = Mage::getStoreConfig(self::XML_PATH_SITE, $store);
32-
return $siteId && Mage::getStoreConfigFlag(self::XML_PATH_ACTIVE, $store);
27+
return Mage::getStoreConfig(self::XML_PATH_SITE, $store)
28+
&& Mage::getStoreConfigFlag(self::XML_PATH_ACTIVE, $store);
3329
}
3430
}

src/app/code/community/Matomo/Analytics/Model/Observer.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
*
46
* Based on Piwik Extension for Magento created by Adrian Speyer
@@ -10,26 +12,25 @@
1012
*
1113
*/
1214

13-
class Matomo_Analytics_Model_Observer
15+
final class Matomo_Analytics_Model_Observer
1416
{
1517
/**
1618
* Add order information into Piwik block to render on checkout success pages
17-
*
18-
* @param Varien_Event_Observer $observer
1919
*/
2020
public function setMatomoAnalyticsOnOrderSuccessPageView(Varien_Event_Observer $observer)
2121
{
22-
if (Mage::helper('matomoanalytics')->isEnabled()) {
23-
$orderIds = $observer->getEvent()->getOrderIds();
22+
if (! Mage::helper('matomoanalytics')->isEnabled()) {
23+
return;
24+
}
2425

25-
if (empty($orderIds) || !is_array($orderIds)) {
26-
return;
27-
}
26+
$orderIds = $observer->getEvent()->getOrderIds();
27+
if (empty($orderIds) || ! is_array($orderIds)) {
28+
return;
29+
}
2830

29-
$block = Mage::app()->getFrontController()->getAction()->getLayout()->getBlock('matomo_script');
30-
if ($block) {
31-
$block->setOrderIds($orderIds);
32-
}
31+
$block = Mage::app()->getFrontController()->getAction()->getLayout()->getBlock('matomo_script');
32+
if ($block) {
33+
$block->setOrderIds($orderIds);
3334
}
3435
}
3536
}

src/app/code/community/Matomo/Analytics/etc/config.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<version>1.1.1</version>
1818
</Matomo_Analytics>
1919
</modules>
20+
2021
<global>
2122
<models>
2223
<matomoanalytics>
@@ -34,6 +35,7 @@
3435
</matomoanalytics>
3536
</helpers>
3637
</global>
38+
3739
<frontend>
3840
<translate>
3941
<modules>
@@ -70,6 +72,7 @@
7072
</updates>
7173
</layout>
7274
</frontend>
75+
7376
<adminhtml>
7477
<translate>
7578
<modules>
@@ -81,6 +84,7 @@
8184
</modules>
8285
</translate>
8386
</adminhtml>
87+
8488
<admin>
8589
<routers>
8690
<Matomo_Analytics>

src/app/design/frontend/base/default/layout/matomoanalytics.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
<layout version="0.1.0">
1515
<default>
1616
<reference name="before_body_end">
17-
<block type="matomoanalytics/script"
18-
name="matomo_script"
19-
as="matomo.script"
20-
template="matomoanalytics/script.phtml"/>
17+
<block type="matomoanalytics/script" name="matomo_script" as="matomo.script" template="matomoanalytics/script.phtml"/>
2118
</reference>
2219
</default>
2320
</layout>

src/app/design/frontend/base/default/template/matomoanalytics/script.phtml

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,34 @@
1111
*/
1212
/** @var Matomo_Analytics_Block_Script $this */
1313
?>
14-
<?php if (!Mage::helper('core/cookie')->isUserNotAllowSaveCookie()): ?>
15-
16-
<?php
17-
$siteId = Mage::getStoreConfig(Matomo_Analytics_Helper_Data::XML_PATH_SITE);
18-
$installPath = Mage::getStoreConfig(Matomo_Analytics_Helper_Data::XML_PATH_INSTALL);
19-
$action = Mage::app()->getRequest()->getActionName();
20-
21-
if ($siteId): ?>
22-
23-
<?php
24-
/*remove https or https*/
25-
$installPath = preg_replace('/^https?:/', '', $installPath);
26-
27-
// 0 Search Results
28-
if ($this->getRequest()->getControllerName() == 'result') {
29-
$queryText = Mage::helper('catalogsearch')->getQuery()->getQueryText();
30-
$noRes = Mage::helper('catalogsearch')->getEngine()
31-
->getResultCollection()
32-
->addSearchFilter($queryText)
33-
->getSize();
34-
}
35-
?>
36-
37-
<!-- START PIWIK TRACKING CODE -->
38-
<script type="text/javascript">
39-
//<![CDATA[
40-
var _paq = _paq || [];
41-
(function () {
42-
var u = "<?php echo $installPath ?>";
43-
_paq.push(['setSiteId', <?php echo $siteId ?>]);
44-
_paq.push(['setTrackerUrl', u + 'piwik.php']);
45-
46-
<?php echo $this->getEcommerceCartUpdate()?>
47-
<?php echo $this->getOrdersTrackingCode()?>
48-
<?php echo $this->getProductPageview()?>
49-
<?php echo $this->getCategoryPageview()?>
50-
51-
<?php if (isset($noRes)): ?>
52-
_paq.push(['setCustomUrl', '' + document.URL + '&search_count=<?php echo $noRes ?>']);
53-
<?php endif ?>
54-
55-
<?php
56-
//404
57-
if ($action == 'noRoute'): ?>
58-
_paq.push(['setDocumentTitle', '404/URL = ' + String(document.location.pathname + document.location.search).replace(/\//g, "%2f") + '/From = ' + String(document.referrer).replace(/\//g, "%2f")]);
59-
<?php endif; ?>
60-
61-
_paq.push(['trackPageView']);
62-
_paq.push(['enableLinkTracking']);
63-
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
64-
g.type = 'text/javascript';
65-
g.defer = true;
66-
g.async = true;
67-
g.src = u + 'piwik.js';
68-
s.parentNode.insertBefore(g, s);
69-
})();
70-
</script>
71-
<noscript>
72-
<p><img src="<?php echo $installPath ?>piwik.php?idsite=<?php echo $siteId ?>" style="border:0" /></p>
73-
</noscript>
74-
<!-- END PIWIK TRACKING CODE -->
75-
76-
<?php endif; ?>
77-
<?php endif; ?>
14+
<?php if (! $this->helper('core/cookie')->isUserNotAllowSaveCookie() && $this->getSiteId()): ?>
15+
<!-- Matomo -->
16+
<script>
17+
var _paq = window._paq = window._paq || [];
18+
(function () {
19+
var u = "<?php echo $this->getInstallPath() ?>";
20+
_paq.push(['setSiteId', <?php echo $this->getSiteId() ?>]);
21+
_paq.push(['setTrackerUrl', u + 'matomo.php']);
22+
23+
<?php echo $this->getEcommerceCartUpdate() ?>
24+
<?php echo $this->getOrdersTrackingCode() ?>
25+
<?php echo $this->getProductPageview() ?>
26+
<?php echo $this->getCategoryPageview() ?>
27+
28+
<?php if ($this->getSearchResultCount() > 0): ?>
29+
_paq.push(['setCustomUrl', '' + document.URL + '&search_count=<?php echo $this->getSearchResultCount() ?>']);
30+
<?php endif ?>
31+
32+
<?php if ($this->is404()): ?>
33+
_paq.push(['setDocumentTitle', '404/URL = ' + String(document.location.pathname + document.location.search).replace(/\//g, "%2f") + '/From = ' + String(document.referrer).replace(/\//g, "%2f")]);
34+
<?php endif ?>
35+
36+
_paq.push(['trackPageView']);
37+
_paq.push(['enableLinkTracking']);
38+
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
39+
g.async = true; g.src = u + 'matomo.js'; s.parentNode.insertBefore(g, s);
40+
})();
41+
</script>
42+
<noscript><p><img src="<?php echo $this->getInstallPath() ?>matomo.php?idsite=<?php echo $this->getSiteId() ?>&rec=1" style="border:0" alt="" /></p></noscript>
43+
<!-- End Matomo Code -->
44+
<?php endif ?>

0 commit comments

Comments
 (0)