Add conversion info to admin dashboard

This commit is contained in:
Matt Lott 2015-05-01 09:37:43 -07:00
parent 8b9c12e685
commit 5305d80887
5 changed files with 48 additions and 13 deletions

View file

@ -50,14 +50,10 @@ block content
th Cancelled
else
th
if subscriberSponsored
th Sponsored
else
th
th Conversion
th Email
th Hero
th Level
th Last Level
th Age
th Spoken
th Clans
@ -73,11 +69,12 @@ block content
span= subscriber.cancel.substring(0, 10)
td
if subscriber.user.stripe.sponsorID
span Yes
span *sponsored*
else if subscriber.user.conversion
span= subscriber.user.conversion
td= subscriber.user.emailLower
td= subscriber.hero
td= subscriber.level
td= subscriber.user.lastLevel
td= subscriber.user.ageRange
td= subscriber.user.preferredLanguage
if subscriber.user.clans

View file

@ -22,6 +22,10 @@
{
"name": "George Saines",
"email": "george@codecombat.com"
},
{
"name": "Matt Lott",
"email": "matt@codecombat.com"
}
],
"scripts": {
@ -55,6 +59,7 @@
"lodash": "~2.4.1",
"mailchimp-api": "2.0.x",
"moment": "~2.5.0",
"mongodb": "^2.0.28",
"mongoose": "3.8.x",
"mongoose-cache": "https://github.com/nwinter/mongoose-cache/tarball/master",
"node-force-domain": "~0.1.0",

View file

@ -16,7 +16,9 @@ AnalyticsLogEventSchema = new mongoose.Schema({
properties: mongoose.Schema.Types.Mixed
}, {strict: false})
AnalyticsLogEventSchema.index({event: 1, _id: 1})
AnalyticsLogEventSchema.index({event: 1, _id: -1})
AnalyticsLogEventSchema.index({event: 1, 'properties.level': 1})
AnalyticsLogEventSchema.index({user: 1})
AnalyticsLogEventSchema.statics.logEvent = (user, event, properties={}) ->
unless user?

View file

@ -1,6 +1,7 @@
# Not paired with a document in the DB, just handles coordinating between
# the stripe property in the user with what's being stored in Stripe.
MongoClient = require('mongodb').MongoClient
mongoose = require 'mongoose'
async = require 'async'
config = require '../../server_config'
@ -25,7 +26,7 @@ class SubscriptionHandler extends Handler
getByRelationship: (req, res, args...) ->
return @getCancellations(req, res) if args[1] is 'cancellations'
return @getSubscribers(req, res) if args[1] is 'subscribers'
return @getRecentSubscribers(req, res) if args[1] is 'subscribers'
return @getSubscriptions(req, res) if args[1] is 'subscriptions'
super(arguments...)
@ -79,15 +80,42 @@ class SubscriptionHandler extends Handler
return @sendDatabaseError(res, err) if err
@sendSuccess(res, cancellations)
getSubscribers: (req, res) ->
# console.log 'subscription_handler getSubscribers'
getRecentSubscribers: (req, res) ->
# console.log 'subscription_handler getRecentSubscribers'
return @sendForbiddenError(res) unless req.user?.isAdmin()
subscriberUserIDs = req.body.ids or []
User.find {_id: {$in: subscriberUserIDs}}, (err, users) =>
return @sendDatabaseError(res, err) if err
userMap = {}
userMap[user.id] = user for user in users
@sendSuccess(res, userMap)
userMap[user.id] = user.toObject() for user in users
# Get conversion data directly from analytics database and add it to results
url = "mongodb://#{config.mongo.analytics_host}:#{config.mongo.analytics_port}/#{config.mongo.analytics_db}"
MongoClient.connect url, (err, db) =>
return @sendDatabaseError(res, err) if err
userEventMap = {}
db.collection('log').find({user: {$in: subscriberUserIDs}}).sort({_id: -1}).each (err, doc) =>
if err
db.close()
return @sendDatabaseError(res, err)
if (doc)
userEventMap[doc.user] ?= []
userEventMap[doc.user].push doc
else
db.close()
for userID, eventList of userEventMap
finishedPurchase = false
for event in eventList
finishedPurchase = true if event.event is 'Finished subscription purchase'
if finishedPurchase
if event.event is 'Show subscription modal' and event.properties?.level?
userMap[userID].conversion = event.properties.level
break
else if event.event is 'Show subscription modal' and event.properties?.label is 'buy gems modal'
userMap[userID].conversion = event.properties.label
break
@sendSuccess(res, userMap)
getSubscriptions: (req, res) ->
# console.log 'subscription_handler getSubscriptions'

View file

@ -17,6 +17,9 @@ config.mongo =
port: process.env.COCO_MONGO_PORT or 27017
host: process.env.COCO_MONGO_HOST or 'localhost'
db: process.env.COCO_MONGO_DATABASE_NAME or 'coco'
analytics_port: process.env.COCO_MONGO_ANALYTICS_PORT or 27017
analytics_host: process.env.COCO_MONGO_ANALYTICS_HOST or 'localhost'
analytics_db: process.env.COCO_MONGO_ANALYTICS_DATABASE_NAME or 'analytics'
mongoose_replica_string: process.env.COCO_MONGO_MONGOOSE_REPLICA_STRING or ''
mongoose_tokyo_replica_string: process.env.COCO_MONGO_MONGOOSE_TOKYO_REPLICA_STRING or ''