Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trackContentInteractionNode "is not defined" #12444

Closed
matomoto opened this issue Jan 15, 2018 · 4 comments
Closed

trackContentInteractionNode "is not defined" #12444

matomoto opened this issue Jan 15, 2018 · 4 comments
Labels
answered For when a question was asked and we referred to forum or answered it.

Comments

@matomoto
Copy link

matomoto commented Jan 15, 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 <script> element must already exist in the head because of the "insertbefore". There is no "if </script>" in head check" in the Matomo Script. If no "</script>" is in the , then a document.getElementsByTagName("head")[0] .appendChild must be used.

@Findus23
Copy link
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/

@Findus23 Findus23 added the answered For when a question was asked and we referred to forum or answered it. label Jan 15, 2018
@matomoto
Copy link
Author

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
Copy link
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
Copy link
Author

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

@Findus23 Findus23 closed this as completed Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered For when a question was asked and we referred to forum or answered it.
Projects
None yet
Development

No branches or pull requests

2 participants