I use Facebook JSSDK (i.e client-side) to verify who the current user to my web-app is.
Code looks something like this:
... inited google analytics ...
...
//events *before* authentication
_gaq.push(['_trackEvent', 'TEST', 'EVENT BEFORE AUTH', 1]);
...
//events *after* attempt at client-side authentication
// callback might return after, say, 7 seconds
authenticateUser(function(userInfo) {
// setting visitor-level custom var
_gaq.push(['_setCustomVar', 1, 'AUTHED-USER', userInfo, 1]);
_gaq.push(['_trackEvent', 'TEST', 'EVENT AFTER AUTH', 1]);
});
(If you try simulating this, please make sure you run "setCustomVar" quite a while after the first event fires - as you want to give GA "enough time" to already send the first event)
This creates the following "Issues" with tracking events under custom variables:
The response from Facebook, with the user's info might return after other events were already sent to Google Analytics - A visitor-level custom variable seems to "solve this" because it seems like even events that were tracked previously in this session (before the custom var was set) are listed "under" the custom var value (that was set after them).
A different user might use the same browser to use the app. The behaviour of "over-writing" the visitor-level custom variable is problematic - it seems that ALL pre-auth events that were issued from this browser are listed/grouped under the LATEST custom-var value. I tried issuing a _deleteCustomVar call before overwriting - but this did not solve the issue.
To differently "solve" the first issue - I thought of not using a visitor-level custom var (but rather a page/session one) - but this would mean all the pre-auth events would not be listed under the custom var - To solve this I thought to somehow defer all the event-tracking to after the auth has been done - but this seems.. umm.. weird.
Another solution would be to use my own "visitor-cookie". But GA already does that so I am reluctant to re-invent the wheel.. (and also after a user-switch, the visitor's cookie will be "lying" for pre-auth events)
Any suggestions to how to tackle this scenario?