Socket
Socket
Sign inDemoInstall

universal-analytics

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

universal-analytics - npm Package Compare versions

Comparing version 0.2.3 to 0.3.0

test/middleware.js

44

lib/index.js

@@ -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 "" +

2

package.json
{
"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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc