mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-07-13 21:21:58 +02:00
feat: add ap pageviews analytics
This commit is contained in:
@@ -21,6 +21,7 @@ let local = {
|
||||
pageViewsRegistered: 0,
|
||||
pageViewsGuest: 0,
|
||||
pageViewsBot: 0,
|
||||
apPageViews: 0,
|
||||
uniquevisitors: 0,
|
||||
};
|
||||
const empty = _.cloneDeep(local);
|
||||
@@ -117,6 +118,10 @@ Analytics.pageView = async function (payload) {
|
||||
}
|
||||
};
|
||||
|
||||
Analytics.apPageView = function () {
|
||||
local.apPageViews += 1;
|
||||
};
|
||||
|
||||
Analytics.writeData = async function () {
|
||||
const today = new Date();
|
||||
const month = new Date();
|
||||
@@ -162,6 +167,12 @@ Analytics.writeData = async function () {
|
||||
total.pageViewsBot = 0;
|
||||
}
|
||||
|
||||
if (total.apPageViews > 0) {
|
||||
incrByBulk.push(['analytics:pageviews:ap', total.apPageViews, today.getTime()]);
|
||||
incrByBulk.push(['analytics:pageviews:ap:month', total.apPageViews, month.getTime()]);
|
||||
total.apPageViews = 0;
|
||||
}
|
||||
|
||||
if (total.uniquevisitors > 0) {
|
||||
incrByBulk.push(['analytics:uniquevisitors', total.uniquevisitors, today.getTime()]);
|
||||
total.uniquevisitors = 0;
|
||||
|
||||
@@ -91,7 +91,7 @@ async function getLatestVersion() {
|
||||
dashboardController.getAnalytics = async (req, res, next) => {
|
||||
// Basic validation
|
||||
const validUnits = ['days', 'hours'];
|
||||
const validSets = ['uniquevisitors', 'pageviews', 'pageviews:registered', 'pageviews:bot', 'pageviews:guest'];
|
||||
const validSets = ['uniquevisitors', 'pageviews', 'pageviews:registered', 'pageviews:bot', 'pageviews:guest', 'pageviews:ap'];
|
||||
const until = req.query.until ? new Date(parseInt(req.query.until, 10)) : Date.now();
|
||||
const count = req.query.count || (req.query.units === 'hours' ? 24 : 30);
|
||||
if (isNaN(until) || !validUnits.includes(req.query.units)) {
|
||||
|
||||
@@ -3,11 +3,17 @@
|
||||
const db = require('../database');
|
||||
const meta = require('../meta');
|
||||
const activitypub = require('../activitypub');
|
||||
const analytics = require('../analytics');
|
||||
|
||||
const middleware = module.exports;
|
||||
|
||||
middleware.enabled = async (req, res, next) => next(!meta.config.activitypubEnabled ? 'route' : undefined);
|
||||
|
||||
middleware.pageview = async (req, res, next) => {
|
||||
analytics.apPageView();
|
||||
next();
|
||||
};
|
||||
|
||||
middleware.assertS2S = async function (req, res, next) {
|
||||
// For whatever reason, express accepts does not recognize "profile" as a valid differentiator
|
||||
// Therefore, manual header parsing is used here.
|
||||
|
||||
@@ -14,6 +14,7 @@ module.exports = function (app, middleware, controllers) {
|
||||
|
||||
const middlewares = [
|
||||
middleware.activitypub.enabled,
|
||||
middleware.activitypub.pageview,
|
||||
middleware.activitypub.assertS2S,
|
||||
middleware.activitypub.verify,
|
||||
middleware.activitypub.configureResponse,
|
||||
|
||||
@@ -30,6 +30,7 @@ Analytics.get = async function (socket, data) {
|
||||
pageviewsRegistered: getStats('analytics:pageviews:registered', until, data.amount),
|
||||
pageviewsGuest: getStats('analytics:pageviews:guest', until, data.amount),
|
||||
pageviewsBot: getStats('analytics:pageviews:bot', until, data.amount),
|
||||
appageviews: getStats('analytics:pageviews:ap', until, data.amount),
|
||||
summary: analytics.getSummary(),
|
||||
});
|
||||
result.pastDay = result.pageviews.reduce((a, b) => parseInt(a, 10) + parseInt(b, 10));
|
||||
|
||||
Reference in New Issue
Block a user