Hey guys this might help. Tyler told us about this in the Einstein track. Here is a link to the page where he posted this code after last year's InfusionCon. It has a bit more information, so maybe it will help answer some of your questions.
http://www.infusionblog.com/company-info/infusion-crm-and-analytics/