From 4febcb654e53bde74e1a38d5628906c126cf36dc Mon Sep 17 00:00:00 2001 From: Zainan Victor Zhou Date: Sun, 10 Mar 2019 20:45:33 -0700 Subject: [PATCH 1/3] Apply @otothea 's commit 797d678a69517c624d1390f6c736596e980335d1 --- lib/index.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/index.js b/lib/index.js index 3cc4484..38b5a3e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -100,6 +100,34 @@ module.exports.createFromSession = function (session) { } } +module.exports.koa_middlewhere = function (tid, options) { + + this.tid = tid; + this.options = options; + + var cookieName = (this.options || {}).cookieName || "_ga"; + + return function *(next) { + + this.req.visitor = module.exports.createFromSession(this.session); + + if (this.req.visitor) return yield next; + + var cid; + if (this.cookies && this.cookies.get(cookieName)) { + var gaSplit = this.cookies.get(cookieName).split('.'); + cid = gaSplit[2] + "." + gaSplit[3]; + } + + this.req.visitor = init(tid, cid, options); + + if (this.session) { + this.session.cid = this.req.visitor.cid; + } + + yield next; + } +}; Visitor.prototype = { From 31b3ebb0539fd8a931ff53adf6d030160716a470 Mon Sep 17 00:00:00 2001 From: Zainan Victor Zhou Date: Mon, 11 Mar 2019 10:50:04 -0700 Subject: [PATCH 2/3] Fix naming --- lib/index.js | 2 +- package-lock.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 38b5a3e..cde43c2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -100,7 +100,7 @@ module.exports.createFromSession = function (session) { } } -module.exports.koa_middlewhere = function (tid, options) { +module.exports.koaMiddlewhere = function (tid, options) { this.tid = tid; this.options = options; diff --git a/package-lock.json b/package-lock.json index 3bf1fa2..ace6ff6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "universal-analytics", - "version": "0.4.20", + "version": "0.4.21", "lockfileVersion": 1, "requires": true, "dependencies": { From 427c6460719e5daefba9f0207c2ee8ee3912ee37 Mon Sep 17 00:00:00 2001 From: Zainan Victor Zhou Date: Tue, 12 Mar 2019 09:25:20 -0700 Subject: [PATCH 3/3] Apply @jeffijoe's suggestion at https://github.com/peaksandpies/universal-analytics/pull/129#issuecomment-472059798 --- lib/index.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index cde43c2..ef26de1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -101,31 +101,30 @@ module.exports.createFromSession = function (session) { } module.exports.koaMiddlewhere = function (tid, options) { - this.tid = tid; this.options = options; var cookieName = (this.options || {}).cookieName || "_ga"; - return function *(next) { - - this.req.visitor = module.exports.createFromSession(this.session); - - if (this.req.visitor) return yield next; + return async function (ctx, next) { + ctx.request.visitor = module.exports.createFromSession(ctx.session); + if (ctx.request.visitor) { + return await next(); + } var cid; - if (this.cookies && this.cookies.get(cookieName)) { - var gaSplit = this.cookies.get(cookieName).split('.'); + if (ctx.cookies && ctx.cookies.get(cookieName)) { + var gaSplit = ctx.cookies.get(cookieName).split('.'); cid = gaSplit[2] + "." + gaSplit[3]; } - this.req.visitor = init(tid, cid, options); + ctx.request.visitor = init(tid, cid, options); - if (this.session) { - this.session.cid = this.req.visitor.cid; + if (ctx.session) { + ctx.session.cid = ctx.request.visitor.cid; } - yield next; + await next(); } };