Matomo Heartbeat < Top 100 RECOMMENDED >

// Example usage in your tracking endpoint if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['action']) && $_GET['action'] === 'heartbeat') $handler = new MatomoHeartbeatHandler( 'https://your-matomo-domain.com', 1, // Your site ID 'your-token-auth' // Optional for API access );

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 "); $stmt->execute([$dateFrom, $dateTo]); $metrics = $stmt->fetch(PDO::FETCH_ASSOC); if ($metrics['total_sessions'] > 0) $metrics['bounce_rate'] = ($metrics['bounce_sessions'] / $metrics['total_sessions']) * 100; $metrics['engagement_rate'] = ($metrics['long_sessions'] / $metrics['total_sessions']) * 100; return $metrics;

stopHeartbeat() if (this.intervalId) clearInterval(this.intervalId); this.intervalId = null; // Calculate engaged time for this session if (this.lastHeartbeatTime) const engagedTime = Math.floor((Date.now() - this.lastHeartbeatTime) / 1000); if (engagedTime >= this.options.minVisitLength) this.totalEngagedTime += engagedTime; this.isActive = false; this.log('Heartbeat stopped'); matomo heartbeat

I'll help you develop a Matomo Heartbeat feature. This feature tracks user engagement time more accurately by sending periodic heartbeats to Matomo analytics. 1. JavaScript Heartbeat Tracker // matomo-heartbeat.js class MatomoHeartbeat { constructor(options = {}) this.options = heartbeatInterval: 15, // seconds minVisitLength: 5, // minimum seconds for a valid visit idleTimeout: 30, // seconds of inactivity to stop heartbeat debug: false, ...options ; this.intervalId = null; this.idleCheckId = null; this.lastActivity = Date.now(); this.visitStartTime = null; this.totalEngagedTime = 0; this.isActive = false; this.lastHeartbeatTime = null; this.init();

private function createHeartbeatTable() $sql = " CREATE TABLE IF NOT EXISTS matomo_heartbeat_sessions ( id INT AUTO_INCREMENT PRIMARY KEY, session_id VARCHAR(64) NOT NULL, visitor_id VARCHAR(64) NOT NULL, page_url TEXT, start_time DATETIME NOT NULL, last_heartbeat DATETIME NOT NULL, total_engaged_time INT DEFAULT 0, heartbeat_count INT DEFAULT 0, is_active BOOLEAN DEFAULT TRUE, INDEX idx_session (session_id), INDEX idx_visitor (visitor_id), INDEX idx_active (is_active) )"; $this->db->exec($sql); // Example usage in your tracking endpoint if

header('Content-Type: application/json'); echo json_encode($result); <!DOCTYPE html> <html> <head> <!-- Matomo Tracking Code --> <script> var _paq = window._paq = window._paq || []; _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() var u="https://your-matomo-domain.com/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); )(); </script>

public function getEngagementReport($dateRange = 'last30') $apiUrl = $this->matomoUrl . '/index.php'; $params = [ 'module' => 'API', 'method' => 'CustomEvents.getCustomEvent', 'idSite' => $this->siteId, 'period' => 'range', 'date' => $dateRange, 'format' => 'json', 'filter_limit' => -1 ]; if ($this->tokenAuth) $params['token_auth'] = $this->tokenAuth; $url = $apiUrl . '?' . http_build_query($params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); return json_decode($response, true); This feature tracks user engagement time more accurately

private function endSession($sessionId) $stmt = $this->db->prepare(" UPDATE matomo_heartbeat_sessions SET is_active = 0 WHERE id = ? "); $stmt->execute([$sessionId]);