Matomo Heartbeat !new!

public function getEngagementMetrics($dateFrom, $dateTo) $stmt = $this->db->prepare(" SELECT COUNT(DISTINCT session_id) as total_sessions, SUM(total_engaged_time) as total_engagement_seconds, AVG(total_engaged_time) as avg_engagement_seconds, AVG(heartbeat_count) as avg_heartbeats_per_session, COUNT(CASE WHEN total_engaged_time > 60 THEN 1 END) as long_sessions, COUNT(CASE WHEN total_engaged_time < 10 THEN 1 END) as bounce_sessions FROM matomo_heartbeat_sessions WHERE start_time BETWEEN ? AND ? AND is_active = 0 ");

This implementation provides accurate user engagement tracking beyond Matomo's default time tracking, which only records page load and unload events.

$now = new DateTime();

bindEvents() // Track user activity events const activityEvents = ['mousedown', 'mousemove', 'keydown', 'scroll', 'touchstart', 'click']; matomo heartbeat

, 1000);

This is where the mechanism changes the game. By sending small periodic signals back to the server, Matomo ensures that your engagement data reflects reality, not just clicks. What is the Matomo Heartbeat?

$params = [ 'module' => 'API', 'method' => 'CustomEvents.getCustomEvent', 'idSite' => $this->siteId, 'period' => 'range', 'date' => $dateRange, 'format' => 'json', 'filter_limit' => -1 ]; $now = new DateTime(); bindEvents() // Track user

In a traditional setup, Matomo records a timestamp when a page loads. It only calculates "Time on Page" by subtracting that timestamp from the timestamp of the next page view. If there is no next page view (a "bounce"), the duration is unknown. The Matomo Heartbeat mechanism solves this by sending signals at regular intervals, allowing the server to know exactly how long the user stayed, even if they never clicked another link. Why the Heartbeat is Essential for Modern Websites 1. Accurate "Time on Page" for Single-Page Visits

Look for the option in the configuration settings.

if (window._paq) window._paq.push(['trackEvent', finalData.e_c, finalData.e_a, finalData.e_n, finalData.e_v]); $params = [ 'module' =&gt; 'API', 'method' =&gt;

Set your desired interval (e.g., 30 seconds) and save/publish the container. Server Load

A "bounce" isn't always bad. A bounce where a user stayed for six minutes suggests high-quality content. Heartbeat allows you to differentiate between a "fast bounce" (the user left immediately) and a "satisfied bounce" (the user read the whole page). How to Enable the Matomo Heartbeat