Spotify uses a combination of event tracking, background processes, and real-time triggers to detect if you've been listening for 30 minutes and then push an ad. Here's exactly how it works:


βœ… 1. Session Tracking (Listening Time)

πŸ‘‰ This data is sent to the backend server in real-time.


βœ… 2. Heartbeat Pings to Backend

{
  "userId": "12345",
  "songId": "abc123",
  "timestamp": "2025-03-10T14:00:00Z",
  "isPlaying": true,
  "device": "Android",
  "sessionDuration": 1730
}

πŸ‘‰ Session Duration keeps increasing as long as the user is listening.


βœ… 3. Cumulative Listening Time Tracking


βœ… 4. Background Timer (Even When App is Minimized)