universal-analytics
Advanced tools
Comparing version 0.2.3 to 0.3.0
@@ -39,2 +39,44 @@ | ||
module.exports.middleware = function (tid, options) { | ||
this.tid = tid; | ||
this.options = options; | ||
var cookieName = (this.options || {}).cookieName || "_ga"; | ||
return function (req, res, next) { | ||
req.visitor = module.exports.createFromSession(req.session); | ||
if (req.visitor) return next(); | ||
var cid; | ||
if (req.cookies && req.cookies[cookieName]) { | ||
var gaSplit = req.cookies[cookieName].split('.'); | ||
cid = gaSplit[2] + "." + gaSplit[3]; | ||
} | ||
req.visitor = init(tid, cid, options); | ||
if (req.session) { | ||
req.session.cid = req.visitor.cid; | ||
} | ||
next(); | ||
} | ||
} | ||
module.exports.createFromSession = function (session) { | ||
if (session && session.cid) { | ||
return init(this.tid, session.cid, this.options); | ||
} | ||
} | ||
Visitor.prototype = { | ||
@@ -352,3 +394,3 @@ | ||
for (var i = 0; i < args.length; i++) { | ||
id = utils.ensureUuid(args[i]); | ||
id = utils.ensureValidCid(args[i]); | ||
if (id !== false) return id; | ||
@@ -355,0 +397,0 @@ if (id != null) this._log("Warning! Invalid UUID format '" + args[i] + "'"); |
@@ -8,4 +8,16 @@ | ||
module.exports.ensureUuid = function (uuid) { | ||
if (!this.isUuid(uuid)) return false; | ||
module.exports.isCookieCid = function (cid) { | ||
return /^[0-9]+\.[0-9]+$/.test(cid) | ||
} | ||
module.exports.ensureValidCid = function (uuid) { | ||
if (!this.isUuid(uuid)) { | ||
if (!this.isCookieCid(uuid)) { | ||
return false; | ||
} | ||
return uuid; | ||
} | ||
uuid = uuid.replace(/\-/g, ""); | ||
@@ -12,0 +24,0 @@ return "" + |
{ | ||
"name": "universal-analytics", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "A node module for Google's Universal Analytics tracking", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -357,2 +357,25 @@ universal-analytics | ||
## Session-based identification | ||
In order to make session-based apps easier to work with, `universal-analytics` also provides a middleware that works in an Expressjs-style fashion. It will try to detect a client ID based on the `_ga` cookie used by the analytics.js client-side tracking. Additionally it will store the detected client ID in the current session to recognize the visitor later. | ||
```javascript | ||
var ua = require("universal-analytics"); | ||
var express = require("express"); | ||
var app = express() | ||
express.use(ua.middleware("UA-XXXX-Y", {cookieName: '_ga'})); | ||
``` | ||
The middleware will attach the `universal analytics` visitor instance to every request (`req.visitor`). | ||
Additionally, the module also exposes a `createFromSession` method to create a visitor instance simply based on a session, which is helpful when working with Socket.io, etc. where the middleware is not used. | ||
```javascript | ||
var visitor = ua.createFromSession(socket.handshake.session); | ||
``` | ||
## Debug mode | ||
@@ -368,2 +391,3 @@ | ||
## Shortcuts | ||
@@ -370,0 +394,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
118898
20
2214
459