-
Notifications
You must be signed in to change notification settings - Fork 72
feat(analytics): add content performance metrics #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zeemscript
merged 2 commits into
Deen-Bridge:main
from
Fury03:feat/issue-244-content-performance
Sep 2, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // controllers/analytics/contentPerformanceController.js | ||
| import mongoose from "mongoose"; | ||
| import logger from "../../config/logger.js"; | ||
| import contentMetricsService from "../../services/analytics/contentMetricsService.js"; | ||
|
|
||
| /** | ||
| * GET /api/analytics/content-performance | ||
| * Comparative analytics across all courses and books: views, engagement, | ||
| * completion rates, and a platform-level roll-up. | ||
| */ | ||
| export const getContentPerformance = async (req, res) => { | ||
| try { | ||
| const performance = await contentMetricsService.getContentPerformance(); | ||
| res.status(200).json({ success: true, ...performance }); | ||
| } catch (error) { | ||
| logger.error("Failed to compute content performance:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Failed to compute content performance", | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * GET /api/analytics/content-performance/:type/:id | ||
| * Metrics for a single course or book. | ||
| */ | ||
| export const getContentMetrics = async (req, res) => { | ||
| try { | ||
| const { type, id } = req.params; | ||
|
|
||
| if (!["course", "book"].includes(type)) { | ||
| return res.status(400).json({ | ||
| success: false, | ||
| message: "type must be either 'course' or 'book'", | ||
| }); | ||
| } | ||
|
|
||
| if (!mongoose.Types.ObjectId.isValid(id)) { | ||
| return res.status(400).json({ | ||
| success: false, | ||
| message: "A valid content id is required", | ||
| }); | ||
| } | ||
|
|
||
| const metrics = await contentMetricsService.getContentMetrics({ type, id }); | ||
| if (!metrics) { | ||
| return res.status(404).json({ | ||
| success: false, | ||
| message: "Content not found", | ||
| }); | ||
| } | ||
|
|
||
| res.status(200).json({ success: true, metrics }); | ||
| } catch (error) { | ||
| logger.error("Failed to compute content metrics:", error); | ||
| res.status(500).json({ | ||
| success: false, | ||
| message: "Failed to compute content metrics", | ||
| }); | ||
| } | ||
| }; | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // routes/analytics/contentPerformanceRoutes.js | ||
| // | ||
| // Content performance analytics endpoints. Mounted at /api/analytics in | ||
| // app.js. All endpoints require authentication (protect). | ||
| import express from "express"; | ||
| import { protect } from "../../middlewares/authMiddleware.js"; | ||
| import { | ||
| getContentPerformance, | ||
| getContentMetrics, | ||
| } from "../../controllers/analytics/contentPerformanceController.js"; | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| // Comparative analytics across all courses and books. | ||
| router.get("/content-performance", protect, getContentPerformance); | ||
|
|
||
| // Metrics for a single item: /content-performance/course/:id | /book/:id | ||
| router.get("/content-performance/:type/:id", protect, getContentMetrics); | ||
|
|
||
| export default router; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| // services/analytics/contentMetricsService.js | ||
| // | ||
| // Content performance analytics (issue #244): tracks view counts for courses | ||
| // and books, and aggregates engagement, completion and interaction metrics | ||
| // across all content so creators can see how their work is performing and | ||
| // compare items against each other. | ||
|
|
||
| import Course from "../../models/Course.js"; | ||
| import Book from "../../models/Book.js"; | ||
| import CourseProgress from "../../models/CourseProgress.js"; | ||
| import ReadingProgress from "../../models/ReadingProgress.js"; | ||
| import { | ||
| interactionRate, | ||
| completionRate, | ||
| avgTimeSpentSeconds, | ||
| avgPercentComplete, | ||
| engagementScore, | ||
| } from "../../utils/analytics/engagementCalculator.js"; | ||
|
|
||
| export class ContentMetricsService { | ||
| /** | ||
| * Record a course view (fire-and-forget so a failed metric write never | ||
| * blocks or fails the detail response). | ||
| * | ||
| * @param {string} courseId - Course ObjectId. | ||
| */ | ||
| async recordCourseView(courseId) { | ||
| try { | ||
| await Course.updateOne({ _id: courseId }, { $inc: { views: 1 } }); | ||
| } catch { | ||
| // View tracking is best-effort; ignore write failures. | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Record a book view (read) — same best-effort semantics as course views. | ||
| * | ||
| * @param {string} bookId - Book ObjectId. | ||
| */ | ||
| async recordBookView(bookId) { | ||
| try { | ||
| await Book.updateOne({ _id: bookId }, { $inc: { readCount: 1 } }); | ||
| } catch { | ||
| // View tracking is best-effort; ignore write failures. | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Build the metrics row for a single course. | ||
| * | ||
| * @param {object} course - Lean Course document. | ||
| * @param {Array<object>} progressDocs - CourseProgress records for the course. | ||
| * @returns {object} The metrics row. | ||
| */ | ||
| _courseRow(course, progressDocs) { | ||
| const views = course.views || 0; | ||
| const reviews = course.numReviews || 0; | ||
| const enrollments = Array.isArray(course.enrolledUsers) | ||
| ? course.enrolledUsers.length | ||
| : 0; | ||
| const completions = progressDocs.filter( | ||
| (p) => p.completedAt || Number(p.percentComplete || 0) >= 100 | ||
| ).length; | ||
| const cr = completionRate(completions, enrollments); | ||
|
|
||
| return { | ||
| id: String(course._id), | ||
| type: "course", | ||
| title: course.title, | ||
| category: course.category || "", | ||
| views, | ||
| reviews, | ||
| enrollments, | ||
| completions, | ||
| completionRate: cr, | ||
| interactionRate: interactionRate(reviews, views), | ||
| avgTimeSpentSeconds: avgTimeSpentSeconds(progressDocs), | ||
| avgPercentComplete: avgPercentComplete(progressDocs), | ||
| engagementScore: engagementScore({ | ||
| completionRate: cr, | ||
| interactionRate: interactionRate(reviews, views), | ||
| avgPercentComplete: avgPercentComplete(progressDocs), | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Build the metrics row for a single book. | ||
| * | ||
| * @param {object} book - Lean Book document. | ||
| * @param {Array<object>} progressDocs - ReadingProgress records for the book. | ||
| * @returns {object} The metrics row. | ||
| */ | ||
| _bookRow(book, progressDocs) { | ||
| const views = book.readCount || 0; | ||
| const reviews = book.numReviews || 0; | ||
| // Books have no enrollments; learner depth is the average reading progress | ||
| // (ReadingProgress stores the field as `percentage`, not `percentComplete`). | ||
| const apc = avgPercentComplete( | ||
| progressDocs.map((p) => ({ percentComplete: p.percentage })) | ||
| ); | ||
|
|
||
| return { | ||
| id: String(book._id), | ||
| type: "book", | ||
| title: book.title, | ||
| category: book.category || "", | ||
| views, | ||
| reviews, | ||
| enrollments: 0, | ||
| completions: 0, | ||
| completionRate: apc, // proxy: avg reading progress percentage | ||
| interactionRate: interactionRate(reviews, views), | ||
| avgTimeSpentSeconds: 0, | ||
| avgPercentComplete: apc, | ||
| engagementScore: engagementScore({ | ||
| completionRate: apc, | ||
| interactionRate: interactionRate(reviews, views), | ||
| avgPercentComplete: apc, | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Comparative analytics across ALL content (courses + books), sorted by | ||
| * views, with a platform-level roll-up. | ||
| * | ||
| * @returns {Promise<{summary: object, content: object[]}>} | ||
| */ | ||
| async getContentPerformance() { | ||
| const [courses, books] = await Promise.all([ | ||
| Course.find().lean(), | ||
| Book.find().lean(), | ||
| ]); | ||
|
|
||
| const [courseProgress, bookProgress] = await Promise.all([ | ||
| CourseProgress.find({ course: { $in: courses.map((c) => c._id) } }).lean(), | ||
| ReadingProgress.find({ book: { $in: books.map((b) => b._id) } }).lean(), | ||
| ]); | ||
|
|
||
| const progressByCourse = this._groupBy(courseProgress, "course"); | ||
| const progressByBook = this._groupBy(bookProgress, "book"); | ||
|
|
||
| const content = [ | ||
| ...courses.map((course) => | ||
| this._courseRow(course, progressByCourse.get(String(course._id)) || []) | ||
| ), | ||
| ...books.map((book) => | ||
| this._bookRow(book, progressByBook.get(String(book._id)) || []) | ||
| ), | ||
| ].sort((a, b) => b.views - a.views); | ||
|
|
||
| // Course-only completion average for the roll-up (books have no enrollments). | ||
| const courseRows = content.filter((row) => row.type === "course"); | ||
| const avgCourseCompletion = courseRows.length | ||
| ? Math.round( | ||
| (courseRows.reduce((sum, r) => sum + r.completionRate, 0) / | ||
| courseRows.length) * | ||
| 100 | ||
| ) / 100 | ||
| : 0; | ||
|
|
||
| return { | ||
| summary: { | ||
| totalContent: content.length, | ||
| totalCourses: courseRows.length, | ||
| totalBooks: content.length - courseRows.length, | ||
| totalViews: content.reduce((sum, r) => sum + r.views, 0), | ||
| totalReviews: content.reduce((sum, r) => sum + r.reviews, 0), | ||
| avgCompletionRate: avgCourseCompletion, | ||
| topByViews: [...content].sort((a, b) => b.views - a.views).slice(0, 3), | ||
| topByEngagement: [...content] | ||
| .sort((a, b) => b.engagementScore - a.engagementScore) | ||
| .slice(0, 3), | ||
| }, | ||
| content, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Metrics for a single course or book. | ||
| * | ||
| * @param {object} params | ||
| * @param {"course"|"book"} params.type - Content type. | ||
| * @param {string} params.id - Content ObjectId. | ||
| * @returns {Promise<object|null>} The metrics row, or null when not found. | ||
| */ | ||
| async getContentMetrics({ type, id }) { | ||
| if (type === "course") { | ||
| const course = await Course.findById(id).lean(); | ||
| if (!course) return null; | ||
| const progressDocs = await CourseProgress.find({ course: id }).lean(); | ||
| return this._courseRow(course, progressDocs); | ||
| } | ||
| if (type === "book") { | ||
| const book = await Book.findById(id).lean(); | ||
| if (!book) return null; | ||
| const progressDocs = await ReadingProgress.find({ book: id }).lean(); | ||
| return this._bookRow(book, progressDocs); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Group a list of documents by a field, keyed by its string value. | ||
| * | ||
| * @param {Array<object>} docs - Documents to group. | ||
| * @param {string} field - Field name to group by. | ||
| * @returns {Map<string, object[]>} | ||
| */ | ||
| _groupBy(docs, field) { | ||
| const groups = new Map(); | ||
| for (const doc of docs) { | ||
| const key = String(doc[field]); | ||
| if (!groups.has(key)) groups.set(key, []); | ||
| groups.get(key).push(doc); | ||
| } | ||
| return groups; | ||
| } | ||
| } | ||
|
|
||
| export const contentMetricsService = new ContentMetricsService(); | ||
| export default contentMetricsService; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Use the standard response envelope on every analytics success and error path:
{ success, message, data }. Keep aggregate results underdata, item metrics underdata, and includedataconsistently in error responses so clients can parse both endpoints uniformly.📍 Affects 2 files
src/controllers/analytics/contentPerformanceController.js#L14-L14(this comment)src/routes/analytics/contentPerformanceRoutes.js#L15-L15🤖 Prompt for AI Agents
Source: Path instructions
🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
Restrict analytics access beyond authentication. The aggregate endpoint exposes platform-wide metrics and the item endpoint accepts arbitrary content IDs without checking
req.user; enforce an analytics-role or administrator check for aggregate metrics and ownership or equivalent authorization for item metrics.📍 Affects 2 files
src/controllers/analytics/contentPerformanceController.js#L13-L13(this comment)src/routes/analytics/contentPerformanceRoutes.js#L15-L15🤖 Prompt for AI Agents
Source: Path instructions