I think the best command to do this would be xdotool
however I am not familiar with the Spotify app so you'll have to test if the xwindowname is updated by the app. Below is a list of commands to test this from there a simple script would be easy enough to write but first we need proof of concept.
pgreg <name of spotify app>
xdotool search --pid <pid returned by pgrep>
xdotool getwindowname <window id returned by xdotool search>
The last command will tell you the window name and then you can test if it is changed when the ads come on.
If everything works and the window name is changed when the ads play a simple script like.
NOTE: This is very rough to show the concepts not give a copy and paste answer.
#!/bin/bash
while true;
do
pid=$(pgrep spotify)
win=$(xdotool search --pid $pid)
state=$(xdotool getwindowname $win | <filter for ads>)
if [[ $state = 'ads' ]]; then
mute
else
unmute
fi
done
Something like that very rough but get across the concept and idea.