Run an AGI Script (or something else) when agent joins/leaves a queue?

Be sure to put this code in your extensions_override.conf file, or it will either be ignored (if in extensions_custom) or overwritten (if in extensions.conf or extensions_additional.conf).

Hooking for fun and income might also be a good resource for you to help you understand how to modify the dial plan.