Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getFunnelData(startDay, eventFunnel, testGroupFn, levelSlugs, logDB) {
if (!startDay || !eventFunnel || eventFunnel.length === 0 || !testGroupFn) return {};
// log('getFunnelData:');
// log(startDay);
// log(eventFunnel);
var startObj = objectIdWithTimestamp(ISODate(startDay + "T00:00:00.000Z"));
var queryParams = {$and: [{_id: {$gte: startObj}},{"event": {$in: eventFunnel}}]};
var cursor = (logDB.log || db['analytics.log.events']).find(queryParams);
log("Fetching events..");
// Map ordering: level, user, event, day
var levelUserEventMap = {};
var levelSessions = [];
var users = [];
var eventsCounted = 0;
while (cursor.hasNext()) {
var doc = cursor.next();
var created = doc._id.getTimestamp().toISOString();
var day = created.substring(0, 10);
var event = doc.event;
var properties = doc.properties;
var user = doc.user.valueOf();
var level = 'n/a';
var ls = null;
function getLevelFunnelData(startDay, endDay, eventFunnel) {
// Copied from insertPerDayAnalytics.js
if (!startDay || !eventFunnel || eventFunnel.length === 0) return {};
// var startObj = objectIdWithTimestamp(ISODate(startDay + "T00:00:00.000Z"));
var startObj = objectIdWithTimestamp(ISODate(dataStartDay + "T00:00:00.000Z"));
var endObj = objectIdWithTimestamp(ISODate(endDay + "T00:00:00.000Z"));
var queryParams = {$and: [{_id: {$gte: startObj}},{_id: {$lt: endObj}},{"event": {$in: eventFunnel}}]};
// var queryParams = {$and: [{user: ObjectId("539c630f30a67c3b05d98d95")},{_id: {$gte: startObj}},{_id: {$lt: endObj}},{"event": {$in: eventFunnel}}]};
var cursor = db['analytics.log.events'].find(queryParams);
// Map ordering: level, user, event, day
var recordCount = 0;
var duplicates = {};
var levelEventUserDayMap = {};
var levelUserEventDayMap = {};
while (cursor.hasNext()) {
recordCount++;
var doc = cursor.next();
var created = doc._id.getTimestamp().toISOString();
var day = created.substring(0, 10);
var event = doc.event;
var properties = doc.properties;
var user = doc.user;
var level;
print("Start date is " + startDate)
// var endDate = "2015-01-06T00:00:00.000Z";
// print("End date is " + endDate)
function objectIdWithTimestamp(timestamp)
{
// Convert string date to Date object (otherwise assume timestamp is a date)
if (typeof(timestamp) == 'string') timestamp = new Date(timestamp);
// Convert date object to hex seconds since Unix epoch
var hexSeconds = Math.floor(timestamp/1000).toString(16);
// Create an ObjectId with that hex timestamp
var constructedObjectId = ObjectId(hexSeconds + "0000000000000000");
return constructedObjectId
}
var cursor = db['analytics.log.events'].find({
$and: [
{_id: {$gte: objectIdWithTimestamp(ISODate(startDate))}},
{$or: [ {"event" : 'Started Level'}, {"event" : 'Saw Victory'}]}
]
});
var longestLevelName = -1;
// Copied from WorldMapView
var dungeonLevels = [
'dungeons-of-kithgard',
'gems-in-the-deep',
'shadow-guard',
'kounter-kithwise',
'crawlways-of-kithgard',
}
}
};
var group={"$group" : {
"_id" : {
"m" : "$event",
"d" : "$created"
},
"count" : {
"$sum" : 1
}
}
};
var conversionsPerDay = {};
var sort = {$sort: { "_id.d" : -1}};
var cursor = db['analytics.log.events'].aggregate(match, proj1, proj2, group, sort);
while (cursor.hasNext()) {
var myDoc = cursor.next();
var key = myDoc._id.d.toDateString()
if (!conversionsPerDay[key]) conversionsPerDay[key] = {}
conversionsPerDay[key][myDoc._id.m] = myDoc.count;
}
for (key in conversionsPerDay) {
print(key + "\t" + conversionsPerDay[key]['Started Signup'] + "\t" + conversionsPerDay[key]['Finished Signup'] + "\t" + (conversionsPerDay[key]['Finished Signup'] / conversionsPerDay[key]['Started Signup'] * 100).toFixed(2) + "%");
}