Hello, i use on my websites a JavaScript confirm()
included a Cancel → event.preventDefault()
-Rule with links <a href="https://external.com/xyz">
. It seems, that matomo tracks the click on a link and not the opening of the link. That’s in my case not so good. My interest is further not to produce more data, like with Event-Tracking. My interest is in manage the link tracking including the use of a confirm()
. The further thing is, that not all links have included a confirm()
.
It is realizable with
_paq.push(['trackLink', url, 'link']); // 'link' or 'download'
The url
must not be a textstring, but a URL-Object. That is realizable with new URL()
.
First, disable the link tracking, if it is enabled:
// _paq.push(['enableLinkTracking']);
Second, put the link tracking in the confirm function:
function link_confirm(link_element) {
var link_element_url = new URL(link_element.href);
var confirm_result = confirm('confirm text');
if (confirm_result === true) {
_paq.push(['trackLink', link_element_url, 'link']);
} else {
event.preventDefault();
}
}
Yet, not the link click will be tracked, but the link after click the OK in the confirm.
Third, put to all links a onclick
event:
<a href="https://example.com/" onclick="link_confirm(this);">example.com</a>
When in the website also are links included, that should not have a confirm, for this you can write a second function:
function link_tracking(link_element) {
var link_element_url = new URL(link_element.href);
_paq.push(['trackLink', link_element_url, 'link']);
}
<a href="https://example.com/" onclick="link_tracking(this);">example.com</a>
When you make all right, it works fine.
Reference: https://developer.matomo.org/api-reference/tracking-javascript