Dialplan customization - How/Where to insert?

See my comment in this thread from earlier today - you would use essentially the same method to insert your code:

Script to monitor call failures NOT TRUNK

The inbound route would go to the custom destination, and the custom destination would go to your extension after returning from your custom code.