Better to use the code in a custom module class. Probably in the event handler code for the ApplicationEvents.SessionStart or ApplicationEvents.SessionEnd events. I can't say for sure when Kentico overwrites the cookie itself. You'd have to experiment and see what works.