WIP: New analytics code

Do NOT merge yet.