Matomo Heartbeat | |best|

// 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 );

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(); matomo heartbeat

startHeartbeat() if (this.intervalId

startIdleMonitoring() this.idleCheckId = setInterval(() => const idleTime = (Date.now() - this.lastActivity) / 1000; if (idleTime >= this.options.idleTimeout && this.isActive) this.stopHeartbeat(); this.log(`User idle for $idleTime seconds, stopping heartbeat`); , 1000); // Example usage in your tracking endpoint if

// Auto-initialize if Matomo is present if (typeof window._paq !== 'undefined') window.matomoHeartbeat = new MatomoHeartbeat( heartbeatInterval: 15, minVisitLength: 5, idleTimeout: 30, debug: window.location.hostname === 'localhost' ); JavaScript Heartbeat Tracker // matomo-heartbeat

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');

public function processHeartbeat($data) $heartbeatData = [ 'idsite' => $this->siteId, 'rec' => 1, 'action_name' => $data['action_name'] ?? 'Heartbeat', 'url' => $data['url'] ?? ($_SERVER['HTTP_REFERER'] ?? ''), 'rand' => mt_rand(), 'e_c' => $data['e_c'] ?? 'Engagement', 'e_a' => $data['e_a'] ?? 'heartbeat', 'e_n' => $data['e_n'] ?? 'User Activity', 'e_v' => $data['e_v'] ?? 0, 'cdt' => date('Y-m-d H:i:s') ]; // Add custom variables if (!empty($data['_cvar'])) $heartbeatData['_cvar'] = $data['_cvar']; // Send to Matomo return $this->sendToMatomo($heartbeatData);