As with lots of little bits of code, if they are useful, you may write and re-write them a several times in several projects before you take the time to package them up into a reusable chunk. That finally happened to me around Google Analytics Event tracking.
Almost every project that I’ve been on in the past year has added Google Analytics (simple page tracking) right up front, and then near the end of the work, realized that they also want event/click tracking. On this last project, I decided to make it right, and package it up.
Assuming you add the class ‘trackit’ to every element you want tracked, you can have something like this:
And clicking all those elements will get tracked using the values found in the element attributes “data-category”, “data-action”, and “data-label”.
The source is available on Github with a few more examples. A test suite is included, in case you want to add your own extras and submit a pull request.
I hope this saves you a little time down the road, as I’m sure it will for me.