UI Extensions can contain a hooks/ subdirectory. This directory can contain especially named scripts that would hook up to the Revit event system and will get executed whenever that event occurs. For example, a doc-changed.py hook script, gets executed automatically every time DocumentChanged event occurs in Revit.

Read the guide below for information on how to write hook scripts

Anatomy of Hook Scripts

Hook Script Types