diff --git a/app/core/Tracker.coffee b/app/core/Tracker.coffee index 59a5aec7c..dda408a47 100644 --- a/app/core/Tracker.coffee +++ b/app/core/Tracker.coffee @@ -1,4 +1,5 @@ {me} = require 'core/auth' +AnalyticsLogEvent = require 'models/AnalyticsLogEvent' debugAnalytics = false @@ -68,6 +69,10 @@ module.exports = class Tracker for integration in includeIntegrations context.integrations[integration] = true analytics?.track action, properties, context + + # Log internally too. Will turn off external event logging when internal logging is sufficient. + event = new AnalyticsLogEvent event: action, properties: properties + event.save() trackTiming: (duration, category, variable, label, samplePercentage=5) -> # https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingTiming diff --git a/app/models/AnalyticsLogEvent.coffee b/app/models/AnalyticsLogEvent.coffee new file mode 100644 index 000000000..7b631ef3d --- /dev/null +++ b/app/models/AnalyticsLogEvent.coffee @@ -0,0 +1,6 @@ +CocoModel = require './CocoModel' + +module.exports = class AnalyticsLogEvent extends CocoModel + @className: 'AnalyticsLogEvent' + @schema: require 'schemas/models/analytics_log_event' + urlRoot: '/db/analytics.log.event' diff --git a/app/schemas/models/analytics_log_event.coffee b/app/schemas/models/analytics_log_event.coffee new file mode 100644 index 000000000..60705cd50 --- /dev/null +++ b/app/schemas/models/analytics_log_event.coffee @@ -0,0 +1,16 @@ +c = require './../schemas' + +AnalyticsLogEventSchema = c.object { + title: 'Analytics Log Event' + description: 'Analytics event logs.' +} + +_.extend AnalyticsLogEventSchema.properties, + created: c.date({title: 'Created', readOnly: true}) + user: c.objectId(links: [{rel: 'extra', href: '/db/user/{($)}'}]) + event: {type: 'string'} + properties: {type: 'object'} + +c.extendBasicProperties AnalyticsLogEventSchema, 'analytics.log.event' + +module.exports = AnalyticsLogEventSchema diff --git a/server/analytics/AnalyticsLogEvent.coffee b/server/analytics/AnalyticsLogEvent.coffee new file mode 100644 index 000000000..a81b70c4c --- /dev/null +++ b/server/analytics/AnalyticsLogEvent.coffee @@ -0,0 +1,10 @@ +mongoose = require 'mongoose' +plugins = require '../plugins/plugins' + +AnalyticsLogEventSchema = new mongoose.Schema({ + created: + type: Date + 'default': Date.now +}, {strict: false}) + +module.exports = AnalyticsLogEvent = mongoose.model('analytics.log.event', AnalyticsLogEventSchema) diff --git a/server/analytics/analytics_log_event_handler.coffee b/server/analytics/analytics_log_event_handler.coffee new file mode 100644 index 000000000..0c512ab74 --- /dev/null +++ b/server/analytics/analytics_log_event_handler.coffee @@ -0,0 +1,20 @@ +AnalyticsLogEvent = require './AnalyticsLogEvent' +Handler = require '../commons/Handler' + +class AnalyticsLogEventHandler extends Handler + modelClass: AnalyticsLogEvent + jsonSchema: require '../../app/schemas/models/analytics_log_event' + editableProperties: [ + 'event' + 'properties' + ] + + hasAccess: (req) -> + req.method in ['PUT', 'POST'] or req.user?.isAdmin() + + makeNewInstance: (req) -> + instance = super(req) + instance.set('user', req.user._id) + instance + +module.exports = new AnalyticsLogEventHandler() diff --git a/server/commons/mapping.coffee b/server/commons/mapping.coffee index dc3b728f8..8bd7e4f7a 100644 --- a/server/commons/mapping.coffee +++ b/server/commons/mapping.coffee @@ -1,4 +1,5 @@ module.exports.handlers = + 'analytics_log_event': 'analytics/analytics_log_event_handler' 'analytics_users_active': 'analytics/analytics_users_active_handler' 'article': 'articles/article_handler' 'level': 'levels/level_handler'