Heartbeat for Image / Link Tracking possible?

Hi,
is it possible to wrap the Image-tracker which JS that on initial Page load the IMG gets requestet and after then, every 5-10 Seconds, same Link? If yes, do i have to add something else for that heartbeat request after initial?

<img referrerpolicy="no-referrer-when-downgrade" src="https://xxxx/matomo.php?idsite=XXX&amp;rec=1" style="border:0; position: absolute;" alt="" />

i have following but seems not working, request are all fine, every 10 Secs:

<script>
            function sendHeartbeat() {
            // URL Matomo
            const imageUrl = "https://xxxxxxx/matomo.php?idsite=xxx&rec=1&ping=1";
          
            // FEtch request
            fetch(imageUrl)
              .then(response => {
                if (response.ok) {
                  
                } else {
                  console.error("Error sending heartbeat.");
                }
              })
              .catch(error => {
                console.error("Error heartbeat: ", error);
              });
          }
          
          // every 10 secs heartbeat
          setInterval(sendHeartbeat, 10000);
          
          </script>

But i wont count seconds in Matomo Backend. Only while zapping through homepage…

Hi lenu, what is your goal by this tracking?

You mixed image tracking with JavaScript tracking. Users with disabled JavaScript are tracket by the first, but not by the second. Please consider that. Only users with enabled JavaScript are tracked by both.

Maybe its a minimal failure by character &?
https://xxxxxxx/matomo.php?idsite=xxx&amp;rec=1&amp;ping=1
Further, i think (without knowledge) the rec=1 is not possible by a ping=1.
https://xxxxxxx/matomo.php?idsite=xxx&amp;ping=1

1 Like

thx! Do it has to be &? As i corrected it to & in my second link.
I also had red=1 in mind (to trim / delete it) but i didn’t test.

my goal was to have a heartbeat to track how long visitors are on page and not have 0 seconds everywhere. But i cam to conclusion that, if i use JS for that, i can also just use tracking code JS from Matomo (with Cookie turned off) and i am at same goal, maybe more better ^.- …

I have not enough knowledge for fetch(), but i think by using this with a query string it must be used with URLSearchParams to create a URL query string.

See here:

For a solution like your goal see the JS tracking code here: Erfahrungsbericht matomo Tracking Rauschen - #27 by melbao

Addition: the Pro of your method is, that by this it is not neccessary to load the matmo.js in the webpages. When the image tracking is enough info for you and you will only expand it to a Time on Page, than use your (fetch) method.