Not track links containing a specific word

Hello,

I have been using matomo for a short time, and I would like to know if there is a method (by Javascript) to exclude specific urls from tracking, for example:
exclude URLs that contain the word “test”.

thank you,

Any news regarding your question above?

I am interested as well with URL containing word “preview=”

It’s possible with JavaScript and a workflow.

First: remove _paq.push(['enableLinkTracking']); from your JavaScript tracking code. Yet, no link will be track by default.

Second: Put a onclick="matomolinktrack(this);" to all your links.

Create a function (put it in the HTML head above the matomo script):

<script>
// ↓↓↓ for Bertrand Marchand. case sensitive
function matomolinktrack(element_a) {

  var a_href = element_a.href;

  var url = new URL(a_href);

  var url_href = url.href;

  var url_params = url.searchParams;
  if (url_href.includes("test") === false) {
    _paq.push(['trackLink', url, 'link']);
  }
}
</script>
<script>
// ↓↓↓ for ehquionest38. case sensitive
function matomolinktrack(element_a) {

  var a_href = element_a.href;

  var url = new URL(a_href);

  var url_params = url.searchParams;
  if (url_params["preview"] === undefined) {
    _paq.push(['trackLink', url, 'link']);
  }
}
</script>

untested.
I use this in a similar way.

Edit:
Whit this code append the onclick function to all links.
Put it on the end of the HTML body.

<script>
var element_a_array = document.getElementsByTagName('a');
for (var i = 0; i < element_a_array.length; i++) {
	element_a_array[i].addEventListener('click', function(){matomolinktrack(this);});
}
</script>
1 Like

Thank you for your help, but I don’t know where/how to set your script…

Another example, today I edit a new post on Wordpress and I can see that the following URL (https://www.xxxxxx.com/?p=30858 has been counted many times in the stats. How to prevent that?

Thanks

The following help page provides an example of how you can include a small ‘if’ statement in your tracking code to exclude a certain directory or page from being tracked - https://matomo.org/faq/how-to/faq_89/

This is added just before the ‘trackPageView’:

_paq.push(['trackPageView']);

You can slightly modify this to suit your needs, for example to exclude pages that contain p= in the URL:

if (!window.location.search.includes('p=')) {
    _paq.push(['trackPageView']);
}

To exclude URLs containing p= and a URL with the phrase ‘test’:

if (!window.location.search.includes('p=') && window.location.pathname.indexOf('test') === -1) {
    _paq.push(['trackPageView']);
}
2 Likes

OK, sorry, a misunderstanding (Link / URL). Follow @goochj03 solution.

I am using “Matomo Connect” plugin for Wordpress. So I modified manualy the code for this one:

<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="https://xxxxxxxx.com/";
if (!window.location.search.includes('p=')) {
    _paq.push(['trackPageView']);
}
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '1']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

It should be correct right?
Thank you again !

Using the Matomo Connect plugin is fine but you would only want to amend your existing trackPageView rather than having this added twice. For example:

<!-- Matomo -->
<script>
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
if (!window.location.search.includes('p=')) {
    _paq.push(['trackPageView']);
}
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="https://xxxxxxxx.com/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '1']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->
1 Like

Professional (for URL query excluding).
A good base also for other solutions.

<!-- Matomo -->
<script>
// Start matomo custom
function set_trackPageView() {
  var dontTrack = "p";
  var url = new URL(window.location.href);
  var url_params = url.searchParams;
  if (url_params[dontTrack] === undefined) {
    _paq.push(['trackPageView']);
  }
}
// End matomo custom
  var _paq = window._paq = window._paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  set_trackPageView(); // custom function above
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="https://xxxxxxxx.com/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '1']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->
1 Like