@matomoto opened this Issue on January 15th 2018

Hello, I just want to count clicks on an internal link. This works with "trackContentInteractionNode". But there are problems.
If the Matomo script with "trackContentInteractionNode" is in the <head>, Matomo will not work because there is an "is not defined" message.

document.getElementById("aid").addEventListener('click', function () {
_paq.push(['trackContentInteractionNode', this]);
});

The element "document.getElementById("aid")" does not exist when the Matomo script is loaded with "trackContentInteractionNode" in the <head> in the browser (Firefox 57.0.4).

For "trackContentInteractionNode" the Matomo script must be at the bottom of the

. But then the Matomo script does not work either, because at least one " in head check" in the Matomo Script. If no "" is in the , then a document.getElementsByTagName("head")[0] .appendChild must be used.
@Findus23 commented on January 15th 2018 Member

Hi,

This isn't really related to Matomo. If you wan't to add an eventListener to an element you need to wait until it exists.

document.addEventListener("DOMContentLoaded", function(event) { 
  document.getElementById("aid").addEventListener('click', function () {
    _paq.push(['trackContentInteractionNode', this]);
  });
});

Then it doesn't matter where you add the code, it will only be loaded after the all elements exist.

That snipped also doesn't need to be at the same place as your piwik tracking code. You can add it to the bottom or add it to the rest of your website Javascript.

PS: For general questions that aren't specific bugs in Matomo, please ask on the forum: https://forum.matomo.org/

@matomoto commented on January 15th 2018

That was not my idea, i have stolen and customized it from the developer site.
Please visit at the bottom of this page:
https://developer.matomo.org/guides/content-tracking

formElement.addEventListener('submit', function () {
    _paq.push(['trackContentInteractionNode', this, 'submittedForm']);
});

Thanks for the hint to "DOMContentLoaded".

@Findus23 commented on January 15th 2018 Member

The code on the developer page is correct. How it needs to be integrated into the website and what exactly it should do, depends on the website where you want to use it.

@matomoto commented on January 15th 2018

You're the first one to mention that, on a page that nobody will read on who reads the developer pages.

This Issue was closed on February 8th 2022
Powered by GitHub Issue Mirror