2015-02-04 16:54:35 -05:00
|
|
|
log = require 'winston'
|
2014-12-15 14:45:12 -05:00
|
|
|
mongoose = require 'mongoose'
|
|
|
|
plugins = require '../plugins/plugins'
|
2015-02-04 16:54:35 -05:00
|
|
|
utils = require '../lib/utils'
|
2015-03-02 19:02:26 -05:00
|
|
|
http = require 'http'
|
|
|
|
config = require '../../server_config'
|
2014-12-15 14:45:12 -05:00
|
|
|
|
2015-01-14 20:51:04 -05:00
|
|
|
AnalyticsLogEventSchema = new mongoose.Schema({
|
|
|
|
u: mongoose.Schema.Types.ObjectId
|
|
|
|
e: Number # event analytics.string ID
|
|
|
|
p: mongoose.Schema.Types.Mixed
|
|
|
|
|
|
|
|
# TODO: Remove these legacy properties after we stop querying for them (probably 30 days, ~2/16/15)
|
|
|
|
user: mongoose.Schema.Types.ObjectId
|
|
|
|
event: String
|
|
|
|
properties: mongoose.Schema.Types.Mixed
|
|
|
|
}, {strict: false})
|
2015-01-27 13:02:47 -05:00
|
|
|
|
2015-05-01 12:37:43 -04:00
|
|
|
AnalyticsLogEventSchema.index({event: 1, _id: -1})
|
|
|
|
AnalyticsLogEventSchema.index({event: 1, 'properties.level': 1})
|
|
|
|
AnalyticsLogEventSchema.index({user: 1})
|
2014-12-15 14:45:12 -05:00
|
|
|
|
2015-02-09 18:02:45 -05:00
|
|
|
AnalyticsLogEventSchema.statics.logEvent = (user, event, properties={}) ->
|
2015-02-04 16:54:35 -05:00
|
|
|
unless user?
|
|
|
|
log.warn 'No user given to analytics logEvent.'
|
|
|
|
return
|
|
|
|
|
2015-03-02 19:02:26 -05:00
|
|
|
doc = new AnalyticsLogEvent
|
|
|
|
user: user
|
|
|
|
event: event
|
|
|
|
properties: properties
|
|
|
|
if config.isProduction and not config.unittest
|
|
|
|
docString = JSON.stringify doc
|
|
|
|
headers =
|
|
|
|
"Content-Type":'application/json'
|
|
|
|
"Content-Length": docString.length
|
|
|
|
|
|
|
|
options =
|
|
|
|
host: 'analytics.codecombat.com'
|
|
|
|
port: 80
|
|
|
|
path: '/analytics'
|
|
|
|
method: 'POST'
|
|
|
|
headers: headers
|
|
|
|
req = http.request options, (res) ->
|
|
|
|
req.on 'error', (e) -> log.warn e
|
|
|
|
req.write(docString)
|
|
|
|
req.end()
|
|
|
|
else
|
|
|
|
doc.save()
|
2015-02-04 16:54:35 -05:00
|
|
|
|
2014-12-15 14:45:12 -05:00
|
|
|
module.exports = AnalyticsLogEvent = mongoose.model('analytics.log.event', AnalyticsLogEventSchema)
|