diff --git a/app/react/App/App.js b/app/react/App/App.js index bdc795f74c..74a91f86af 100644 --- a/app/react/App/App.js +++ b/app/react/App/App.js @@ -15,7 +15,7 @@ import { Menu } from './Menu'; import { AppMainContext } from './AppMainContext'; import SiteName from './SiteName'; import GoogleAnalytics from './GoogleAnalytics'; -import Matomo from './Matomo'; +import { Matomo } from './Matomo'; import 'bootstrap/dist/css/bootstrap.css'; import 'react-widgets/lib/scss/react-widgets.scss'; import 'nprogress/nprogress.css'; diff --git a/app/react/App/Matomo.js b/app/react/App/Matomo.js index 35445fb367..b12895cbf5 100644 --- a/app/react/App/Matomo.js +++ b/app/react/App/Matomo.js @@ -1,48 +1,44 @@ +import { useEffect } from 'react'; import { connect } from 'react-redux'; import PropTypes from 'prop-types'; -import React, { Component } from 'react'; -export class Matomo extends Component { - render() { - if (!this.props.id && !this.props.url) { - return false; +const MatomoComponent = ({ id, url }) => { + useEffect(() => { + if (id && url) { + const matomoUrl = url.replace(/\/?$/, '/'); + + const script = document.createElement('script'); + + script.innerHTML = ` + var _paq = _paq || []; + _paq.push(['trackPageView']); + _paq.push(['enableLinkTracking']); + (function() { + var u="${matomoUrl}"; + _paq.push(['setTrackerUrl', u+'piwik.php']); + _paq.push(['setSiteId', '${id}']); + var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; + g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); + })();`; + + document.body.appendChild(script); } + }, [id, url]); - const url = this.props.url.replace(/\/?$/, '/'); - - return ( -