Last active
September 4, 2016 12:29
-
-
Save mtavkhelidze/ce597dc70acd91439e53591e705c7539 to your computer and use it in GitHub Desktop.
Poor man's Google Analytics with webtask.io
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
/*eslint no-console: 0*/ | |
/** | |
Poor man's Google Analytics webtask.io Task. | |
Written by Misha Tavkhelidze | |
*/ | |
'use strict'; | |
var mongo = require('mongodb').MongoClient; | |
module.exports = (ctx, cb) => { | |
let getId = (qp) => { | |
let id = null; | |
try { | |
id = parseInt(qp); | |
} catch(e) { | |
// noop | |
} | |
return id; | |
}; | |
let id = getId(ctx.query.id); | |
if(!id) return cb(new Error('არ გიცნობ საერთოდ!')); | |
if(!ctx.data.data) return cb(new Error('მითხარი რამე, რომ ჩავიწერო!')); | |
let data = JSON.parse(ctx.data.data); | |
data.ts = new Date(); | |
mongo.connect(ctx.data.MONGO_URL, (err, db) => { | |
if(err) return cb(err); | |
if(db === null) return cb(new Error('ბაზა ვერ მოვძებნე!')); | |
let cname = 'user_' + id; | |
db.collection(cname) | |
.insert(data, (err, res) => { | |
if(err) return cb(err); | |
db.collection(cname).count((err, res) => { | |
return cb(err, res); | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First create task
Than use it like below
PS Strange characters in webtask's error messages are Georgian