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)
- As soon as you press play on any song, Spotify starts a listening session in the backend.
- The session keeps track of:
- When you started the song.
- When you paused or skipped the song.
- Cumulative listening time.
π This data is sent to the backend server in real-time.
β
2. Heartbeat Pings to Backend
- Spotify continuously sends "heartbeat" pings to the server every few seconds to track:
- Is the user still listening?
- Is the app in the background/foreground?
- Has the user paused the song?
- These pings look like:
{
"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
- Spotify doesnβt count paused time or skipped time.
- It only counts the time when audio was actively playing.
- For example:
- If you listen for 15 minutes, then pause for 10 minutes, and resume β it only counts 15 minutes of active listening.
- Once the cumulative active listening time hits 30 minutes, it triggers an ad.
β
4. Background Timer (Even When App is Minimized)