Move analytics code into the analytics handler