refactor analytics code