Skip to content

Commit 9fc71b4

Browse files
committed
improved matomo tracking script integration by deferring the script as much as possible
1 parent f07d938 commit 9fc71b4

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ Matomo Extension for Magento 1
33

44
Facts
55
-----
6-
- extension key: `Matomo_Analytics`
6+
- Extension key: `Matomo_Analytics`
77

88
Description
99
-----------
10-
This extension for Magento 1 adds the Matomo analytics tracking to your shop.
10+
This Extension for Magento 1 and OpenMage adds the Matomo analytics tracking to your shop.
1111
It is based on the original implementation by `adrianspeyer/Piwik-for-Magento`.
1212

1313
Requirements

src/app/code/community/Matomo/Analytics/controllers/Adminhtml/IndexController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,5 @@ public function indexAction()
4949

5050
$this->_addContent($block);
5151
$this->_setActiveMenu('matomo_menu')->renderLayout();
52-
5352
}
5453
}
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
<?php
22
/**
3-
*
43
* Based on Piwik Extension for Magento created by Adrian Speyer
54
*
65
* @category Matomo
76
* @package Matomo_Analytics
87
* @copyright Copyright (c) 2018 Thiago Contardi.
98
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
109
*
10+
* @see https://matomo.org/faq/reports/advanced-manually-tracking-ecommerce-actions-in-matomo/
11+
* @see https://matomo.org/blog/2017/04/different-ways-embedding-piwik-tracking-code-faster-website-performance/
1112
*/
1213
/** @var Matomo_Analytics_Block_Script $this */
1314
?>
1415
<?php if (! $this->helper('core/cookie')->isUserNotAllowSaveCookie() && $this->getSiteId()): ?>
15-
<!-- Matomo -->
16-
<script>
16+
<script nonce="<?= $this->getNonce() ?>">
1717
var _paq = window._paq = window._paq || [];
1818
(function () {
19-
var u = "<?php echo $this->getInstallPath() ?>";
20-
_paq.push(['setSiteId', <?php echo $this->getSiteId() ?>]);
19+
var u = "<?= $this->getInstallPath() ?>";
20+
_paq.push(['setSiteId', <?= $this->getSiteId() ?>]);
2121
_paq.push(['setTrackerUrl', u + 'matomo.php']);
2222

23-
<?php echo $this->getEcommerceCartUpdate() ?>
24-
<?php echo $this->getOrdersTrackingCode() ?>
25-
<?php echo $this->getProductPageview() ?>
26-
<?php echo $this->getCategoryPageview() ?>
23+
<?= $this->getEcommerceCartUpdate() ?>
24+
<?= $this->getOrdersTrackingCode() ?>
25+
<?= $this->getProductPageview() ?>
26+
<?= $this->getCategoryPageview() ?>
2727

2828
<?php if ($this->getSearchResultCount() > 0): ?>
29-
_paq.push(['setCustomUrl', '' + document.URL + '&search_count=<?php echo $this->getSearchResultCount() ?>']);
29+
_paq.push(['setCustomUrl', '' + document.URL + '&search_count=<?= $this->getSearchResultCount() ?>']);
3030
<?php endif ?>
3131

3232
<?php if ($this->is404()): ?>
@@ -35,10 +35,19 @@
3535

3636
_paq.push(['trackPageView']);
3737
_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);
38+
function embedTrackingCode() {
39+
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
40+
g.defer=true; g.async=true; g.src=u+"matomo.js"; s.parentNode.insertBefore(g,s);
41+
}
42+
43+
if (window.addEventListener) {
44+
window.addEventListener("load", embedTrackingCode, false);
45+
} else if (window.attachEvent) {
46+
window.attachEvent("onload", embedTrackingCode);
47+
} else {
48+
embedTrackingCode();
49+
}
4050
})();
4151
</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 -->
52+
<noscript><p><img src="<?= $this->getInstallPath() ?>matomo.php?idsite=<?= $this->getSiteId() ?>&rec=1" style="border:0" alt="" /></p></noscript>
4453
<?php endif ?>

0 commit comments

Comments
 (0)