As a little follow-up post on this weeks analytics posts I thought I present to you a little javascript thingy that eases the setting of Custom Variables on a page where you have no control over and no idea if Google Analytics was using the async or traditional code or whether it is located at the top or the bottom of the page.
This script will save a visitor- or visit-level Custom Variable with the key CV Key and the value CV Value in slot 1.
The variables will be set using event tracking. Initially I build a script that relied on event tracking only as a fallback (page track pixel already fired) whereas the script below relies entirely on event tracking as this is the only way to maintain a constant behaviour throughout different situations.
However Event Tracking does not work with page-level variables as the events themselves are not attached to pages but sessions (visits).
Apart from that limitation it should work right away. If it helps you let me know. If you found a way to make it better please tell me.