This guide applies to Matomo core developers. If you develop a plugin and want to share it as a contributor please check out the guide for Enriching the Matomo JS Tracker.
See JavaScript Tracking Client integration guide.
It means we need to support older browsers. If you're not sure if a certain method is supported by all the required browsers, then you can use Can I Use website to find out.
Generally we try to keep the JS file smallish for fast performance.
To make changes to the JS tracker, you need to edit the raw/unminfied JS tracker itself which you can find in the js/piwik.js directory. When you create a pull request, you can then minify the JS tracker see below.
Public tracker methods like trackPageView
can be called eg using _paq.push(['trackPageView', '...parameters'])
. Such methods are defined similar to this example.
Any newly added method or changed parameters will be documented in the developer changelog and on the JS tracking client page.
A test also needs to be added unless it isn't possible. We also add a test to the "API methods"
test group to ensure the function is exposed.
In most cases we'll also create a new FAQ on our matomo.org website.
See adding tests guide.
You can execute the JavaScript tests by opening https://yourlocal.matomo/tests/javascript/ .
The file containing all tests is located in tests/javascript/index.php
.
To execute tests for a specific module use the module
URL parameter, for example &module=core
.
If you are developing multiple tracker plugins and want to only include tests for a specific tracker plugin (like our CI would do) use the URL parameter plugin
as in &plugin=MyPluginName
.
Sometimes you may want to test private methods that aren't exposed in the JS tracker API. This can be done by following these steps:
tests/javascript/matomotest.js
where you expose a new internal method, for example '_replaceHrefForCrossDomainLink: replaceHrefForCrossDomainLink,'
tracker.hook.test._replaceHrefForCrossDomainLink
To access private tracker variables that only exist within a tracker instance, you can place extra tracker methods between /*<DEBUG>*/
and /*</DEBUG>*/
. Any code between these comments will be removed in the final minified JS tracker version. You can see this done for example here: https://github.com/matomo-org/matomo/blob/4.2.0/js/piwik.js#L4820-L4840
You can then access these methods on a tracker instance as usual. For example tracker.isUsingAlwaysUseSendBeacon
.
You may see a message like Test failed in module externals: 'JSLint'. Error: JSLint validation: please check the browser console for the list of jslint errors.
In that case you need to run the tests in the browser and then open the browser developer tools. It will show you all the JSLint errors in the console. There might be a lot of log output in the console. You can find the relevant entry by searching for JSLint
. Explanations for the different errors you can find for example on linterrors.com/js.
If the error is for example Unexpected /*property*/ '{a}'.
then you may need to add a new member or global in the comments to resolve the issue.
When you create a pull request for a JS tracker change you will notice that a test will fail because the minified version wasn't updated. To generate the minified version for a JS tracker change simply create comment in the PR with the words build js
.
If you are reviewing a pull request from a community contributor and you are happy with the change, then follow these steps:
edit
PR in the topbase: 5.x-dev
.build js
If you need to debug the tracking code for a plugin that is not behaving correctly on a website you do not control, for example, a client or customer's website, this strategy can help:
in the developer tools window, execute the following javascript snippet manually:
delete window.Piwik;
delete window.Matomo;
var _paq = [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
// ... add the other methods you need to call here, examples below ...
//_paq.push(['FormAnalytics::enableDebugMode']);
//_paq.push(['HeatmapSessionRecording::disable']);
(function() {
var u="//apache.matomo/"; // replace apache.matomo with the domain your local matomo is on, eg, localhost
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', '1']);
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);
})();
Note: depending on the plugin you are testing, you may want or need to adjust what tracker methods are called.
Once you're receiving tracking requests from the website, you may want to modify the tracking code that is used in order to
debug an issue or just get better insight into what's happening. You can do this by modifying the plugin's tracker.js
file
(or the core js/piwik.js
) and regenerating the JavaScript. Use the following method so the unminified code will be used:
./console custom-matomo-js:update --ignore-minified
Then reload the webpage and rerun the JavaScript snippet above to use the new tracking code.
This strategy can help increase the speed of debugging complex issues that you do not know how to reproduce locally