Add custom action on code that sets user id for piwik

I have a special requirement for which I need to execute my own code whenever a user id is set. I have gone through piwik code but still not clear on which file do I need to make changes. What is the code flow that handles visit related HTTP requests in piwik?