diff --git a/Block/Matomo.php b/Block/Matomo.php index b632930..d33ee47 100644 --- a/Block/Matomo.php +++ b/Block/Matomo.php @@ -112,12 +112,37 @@ protected function _prepareTracker() */ public function getJsOptions() { - return [ - 'scriptUrl' => $this->getScriptUrl(), - 'trackerUrl' => $this->getTrackerUrl(), - 'siteId' => $this->getSiteId(), - 'actions' => $this->getTracker()->toArray() - ]; + if ($this->isContainerEnabled()) { + $result = []; + } else { + $result = [ + 'scriptUrl' => $this->getScriptUrl(), + 'trackerUrl' => $this->getTrackerUrl(), + 'siteId' => $this->getSiteId(), + ]; + } + $result['isContainerEnabled'] = $this->_dataHelper->isContainerEnabled(); + $result['actions'] = $this->getTracker()->toArray(); + + return $result; + } + + /** + * Check if Matomo Tag Manager Container is enabled + * @return string + */ + public function isContainerEnabled() + { + return $this->_dataHelper->isContainerEnabled(); + } + + /** + * Get Matomo Tag Manager Container URL + * @return string + */ + public function getContainerUrl() + { + return $this->_dataHelper->getContainerUrl(); } /** diff --git a/Helper/Data.php b/Helper/Data.php index c747377..877859f 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -39,6 +39,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper const XML_PATH_CDN_HOSTNAME = 'piwik/tracking/cdn_hostname'; const XML_PATH_JS_SCRIPT_PATH = 'piwik/tracking/js_script_path'; const XML_PATH_PHP_SCRIPT_PATH = 'piwik/tracking/php_script_path'; + const XML_PATH_CONTAINER_ENABLED = 'piwik/tracking/container_enabled'; + const XML_PATH_CONTAINER_SCRIPT_PATH = 'piwik/tracking/container_script_path'; const XML_PATH_SITE_ID = 'piwik/tracking/site_id'; const XML_PATH_LINK_ENABLED = 'piwik/tracking/link_enabled'; const XML_PATH_LINK_DELAY = 'piwik/tracking/link_delay'; @@ -151,6 +153,48 @@ public function getJsScriptPath($store = null) )), '/') ?: 'matomo.js'; } + /** + * Retrieve Matomo Tag Manager JS container Url + * + * @param null|string|bool|int|Store $store + * @return string + */ + public function getContainerUrl($store = null, $secure = null) + { + return $this->getBaseUrl($store, $secure) + . $this->getContainerPath($store); + } + + /** + * Retrieve Matomo Tag Manager JS container path + * + * @param null|string|bool|int|Store $store + * @return ?string + */ + public function getContainerPath($store = null) + { + return ltrim(trim($this->scopeConfig->getValue( + self::XML_PATH_CONTAINER_SCRIPT_PATH, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + $store + )), '/') ?: null; + } + + /** + * Retrieve Matomo Tag Manager JS container path + * + * @param null|string|bool|int|Store $store + * @return bool + */ + public function isContainerEnabled($store = null) + { + return boolval($this->scopeConfig->getValue( + self::XML_PATH_CONTAINER_ENABLED, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + $store + ) ?: false); + } + /** * Retrieve Matomo tracker JS script URL * diff --git a/Observer/BeforeTrackPageViewObserver.php b/Observer/BeforeTrackPageViewObserver.php index 7812278..7221953 100644 --- a/Observer/BeforeTrackPageViewObserver.php +++ b/Observer/BeforeTrackPageViewObserver.php @@ -71,6 +71,10 @@ public function execute(\Magento\Framework\Event\Observer $observer) */ protected function _pushLinkTracking(\Chessio\Matomo\Model\Tracker $tracker) { + if ($this->_dataHelper->isContainerEnabled()) { + return $this; + } + if ($this->_dataHelper->isLinkTrackingEnabled()) { $tracker->enableLinkTracking(true); $delay = $this->_dataHelper->getLinkTrackingDelay(); diff --git a/README.md b/README.md index 0de8a10..972138e 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,11 @@ php bin/magento module:enable Chessio_Matomo --clear-static-content Once installed, configuration options can be found in the Magento 2 administration panel under *Stores/Configuration/Sales/Matomo API*. To start tracking, set *Enable Tracking* to *Yes*, enter the *Hostname* of your Matomo installation and click *Save Config*. If you have multiple websites in the same Matomo installation, make sure the *Site ID* configured in Magento is correct. +### Using Matomo Tag Manager + +You can use the Matomo Tag Manager instead of Matomo directly. Set the configuration *Enable Matomo Tag Manager Container* to yes and set the *Container Script Path*. +For details on how to configure the Matomo Tag Manager, to track ecommerce events, see doc/tag-manager.md + ## Customization If you need to send some custom information to your Matomo server, Chessio_Matomo lets you do so using event observers. diff --git a/doc/tag-manager.md b/doc/tag-manager.md new file mode 100644 index 0000000..d776dad --- /dev/null +++ b/doc/tag-manager.md @@ -0,0 +1,44 @@ +# Using Matomo Tag Manager with the Matomo Magento 2 Module + +This describes how you can set up the tag manager to track events through Matomo Ecommerce tracking. +Essentially, Ecommerce events generated by the Magento 2 module are forwarded to the Matomo events. + +First make sure to set the configuration *Enable Matomo Tag Manager Container* to yes and set the *Container Script Path*. + +## Configuring triggers + +In the Matomo Tag manager container, configure all the necessary triggers. + +Repeat the steps below for the four events + +* `setEcommerceView` +* `addEcommerceItem` +* `trackEcommerceCartUpdate` +* `trackEcommerceOrder` + +### Steps to configure each trigger event + +1. Go to *Trigger* +2. Click create new trigger +3. Click custom event +4. Name and Event name should be the same + +### Steps to configure the ecommerce tag + +1. Go to *Tags* +2. Click *Create New Tag* +3. Choose *Custom HTML* +4. Name "Universal Ecommerce Tag for Matomo" (or choose your own) +5. HTML + ``` + + ``` + +6. *Configure when the tag should do this*, choose all the events from above, one by one +7. Save tag diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 6c0b734..0255f2b 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -65,6 +65,30 @@ required-entry + + + Magento\Config\Model\Config\Source\Yesno + + + + Example: js/container_abcDEFgh.js + + 1 + 1 + + Listed under Settings/Website in your Matomo administration panel 1 + 0 required-entry validate-digits validate-zero-or-greater @@ -91,6 +116,7 @@ Enable tracking of outlinks and downloads 1 + 0 1 1 + 0 validate-digits validate-zero-or-greater diff --git a/etc/config.xml b/etc/config.xml index 9cc5a69..b226c83 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -33,6 +33,8 @@ matomo.php matomo.js + 0 + diff --git a/view/frontend/layout/default.xml b/view/frontend/layout/default.xml index c6f8902..e13631b 100644 --- a/view/frontend/layout/default.xml +++ b/view/frontend/layout/default.xml @@ -29,5 +29,11 @@ as="matomo.tracker" template="matomo.phtml" /> + + + diff --git a/view/frontend/templates/matomo.phtml b/view/frontend/templates/matomo.phtml index fa39888..4d1564a 100644 --- a/view/frontend/templates/matomo.phtml +++ b/view/frontend/templates/matomo.phtml @@ -18,9 +18,12 @@ * You should have received a copy of the GNU Affero General Public License * along with Chessio_Matomo. If not, see . */ - ?> - +isContainerEnabled()) { + return; +} +?> + + +