mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-16 02:36:16 +01:00
API route for returning tracked analytics keys (#10019)
* feat: track metrics saved by NodeBB (and assoc. plugins), #9949 * feat: route to retrieve analytics keys, closes #9949
This commit is contained in:
@@ -53,6 +53,8 @@ Analytics.increment = function (keys, callback) {
|
||||
}
|
||||
};
|
||||
|
||||
Analytics.getKeys = async () => db.getSortedSetRange('analyticsKeys', 0, -1);
|
||||
|
||||
Analytics.pageView = async function (payload) {
|
||||
pageViews += 1;
|
||||
|
||||
@@ -90,6 +92,17 @@ Analytics.writeData = async function () {
|
||||
const month = new Date();
|
||||
const dbQueue = [];
|
||||
|
||||
// Build list of metrics that were updated
|
||||
let metrics = [
|
||||
'pageviews',
|
||||
'pageviews:month',
|
||||
];
|
||||
metrics.forEach((metric) => {
|
||||
const toAdd = ['registered', 'guest', 'bot'].map(type => `${metric}:${type}`);
|
||||
metrics = [...metrics, ...toAdd];
|
||||
});
|
||||
metrics.push('uniquevisitors');
|
||||
|
||||
today.setHours(today.getHours(), 0, 0, 0);
|
||||
month.setMonth(month.getMonth(), 1);
|
||||
month.setHours(0, 0, 0, 0);
|
||||
@@ -130,8 +143,13 @@ Analytics.writeData = async function () {
|
||||
|
||||
for (const [key, value] of Object.entries(counters)) {
|
||||
dbQueue.push(db.sortedSetIncrBy(`analytics:${key}`, value, today.getTime()));
|
||||
metrics.push(key);
|
||||
delete counters[key];
|
||||
}
|
||||
|
||||
// Update list of tracked metrics
|
||||
dbQueue.push(db.sortedSetAdd('analyticsKeys', metrics.map(() => +Date.now()), metrics));
|
||||
|
||||
try {
|
||||
await Promise.all(dbQueue);
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user