This sounds like a classic case of build vs. buy. If analytics are not your core product, inventing a new solution is going to cost you more than buying an existing analytics solution. There are dozens, a few of which have even been in the news the last few days due to acquisitions.
I'm not going to endorse any of them over the others, but I will say you'll be better off using a 3rd party than coding this yourself.