Created
September 7, 2013 01:23
-
-
Save JavascriptMick/6471995 to your computer and use it in GitHub Desktop.
universal-analytics wrapper to assist with putting user info on session. Note that this method assumes you are going to scrape the cid out of the _ga cookie i.e. let analytics.js derive the cid and do the cookie persistance for you. If you want to use your own cid determined server side, then this is not for you.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Useage | |
**require** | |
uaw = require('.[path]/universal-analytics-wrapper'), | |
** express setup ** | |
app.use(uaw.cookieConfigurer('UAXXXXX')); | |
** get visitor ** | |
var visitor = uaw.getVisitor(req.session); | |
** get visitor socket.io with session integration** | |
var visitor = uaw.getVisitor(socket.handshake.session); | |
** use visitor ** | |
visitor.debug().event("activation", "Joined Hosted Room").send(); | |
*/ | |
var ua = require('universal-analytics'); | |
var gaTrackingIdlocal; | |
module.exports.cookieConfigurer = function(gaTrackingId) { | |
gaTrackingIdlocal = gaTrackingId; | |
return function cookieConfigurer(req, res, next) { | |
if(req.session && (!req.session.cid) && req.cookies._ga){ | |
//Use the cid already embedded in the _ga cookie and save to session | |
var gaSplit = req.cookies._ga.split('.'); | |
req.session.cid = gaSplit[2] + "." + gaSplit[3]; | |
}; | |
next(); | |
} | |
}; | |
module.exports.getVisitor = function(session){ | |
console.log('getVisitor'); | |
if(session && session.cid){ | |
console.log('gaTrackingIdlocal:' + gaTrackingIdlocal); | |
console.log('session.cid:' + session.cid); | |
return ua(gaTrackingIdlocal, session.cid); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment