You will need to add a script to the following folder:
/userdata/system/configs/emulationstation/scripts/<event-type>/yourscript.sh
event-type can be one of:
game-start, game-end, game-selected (new in v33), system-selected (new in v33), config-changed, settings-changed, controls-changed, sleep, wake, theme-changed
You can create a simple script to see what args are passed in:
#!/bin/bash
echo "$@" >> /userdata/system/logs/event-test.log
Don’t forget to chmod +x yourscript.sh