Refactor analytics code to be centralized