Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting KoaJS middleware, (originally by @otothea) #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ module.exports.createFromSession = function (session) {
}
}

module.exports.koaMiddlewhere = function (tid, options) {
this.tid = tid;
this.options = options;

var cookieName = (this.options || {}).cookieName || "_ga";

return async function (ctx, next) {
ctx.request.visitor = module.exports.createFromSession(ctx.session);
if (ctx.request.visitor) {
return await next();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to await promise before return when function is async.

Suggested change
return await next();
return next();

}

var cid;
if (ctx.cookies && ctx.cookies.get(cookieName)) {
var gaSplit = ctx.cookies.get(cookieName).split('.');
cid = gaSplit[2] + "." + gaSplit[3];
}

ctx.request.visitor = init(tid, cid, options);

if (ctx.session) {
ctx.session.cid = ctx.request.visitor.cid;
}

await next();
}
};


Visitor.prototype = {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.