Move analytics code to separate include

DRY up the code a bit.