powr-sdk-api 4.12.0 → 4.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/routes/index.js +2 -0
- package/dist/routes/track.js +92 -0
- package/dist/utils/track.js +131 -0
- package/dist/utils/track.test.js +79 -0
- package/package.json +1 -1
package/dist/routes/index.js
CHANGED
|
@@ -22,6 +22,7 @@ const ratingsRoutes = require('./ratings');
|
|
|
22
22
|
const usersRoutes = require('./users');
|
|
23
23
|
const waitlistsRoutes = require('./waitlists');
|
|
24
24
|
const activitiesRoutes = require('./activities');
|
|
25
|
+
const trackRoutes = require('./track');
|
|
25
26
|
const authRoutes = require('./auth');
|
|
26
27
|
const loginCodeRoutes = require('./loginCode');
|
|
27
28
|
const slidesRoutes = require('./slides');
|
|
@@ -53,6 +54,7 @@ const createPowrRoutes = (options = {}) => {
|
|
|
53
54
|
router.use('/users', verifyTokenOrServiceKey, usersRoutes);
|
|
54
55
|
router.use('/waitlists', verifyToken, waitlistsRoutes);
|
|
55
56
|
router.use('/activities', verifyToken, activitiesRoutes);
|
|
57
|
+
router.use('/track', verifyToken, trackRoutes);
|
|
56
58
|
router.use('/slides', verifyToken, slidesRoutes);
|
|
57
59
|
router.use('/notifications', verifyToken, notificationsRoutes);
|
|
58
60
|
router.use('/profiles', verifyTokenOrServiceKey, profilesRoutes);
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const router = express.Router();
|
|
5
|
+
const {
|
|
6
|
+
getDb
|
|
7
|
+
} = require("../services/mongo");
|
|
8
|
+
const {
|
|
9
|
+
PAGEVIEW_TTL_SECONDS,
|
|
10
|
+
PAGEVIEW_LIST_LIMIT,
|
|
11
|
+
PAGEVIEW_LIST_FIELDS,
|
|
12
|
+
buildPageview,
|
|
13
|
+
recentPageviewFilter
|
|
14
|
+
} = require("../utils/track");
|
|
15
|
+
let indexesPromise = null;
|
|
16
|
+
function ensurePageviewIndexes(db) {
|
|
17
|
+
if (!indexesPromise) {
|
|
18
|
+
const col = db.collection("pageviews");
|
|
19
|
+
indexesPromise = Promise.all([col.createIndex({
|
|
20
|
+
createdAt: 1
|
|
21
|
+
}, {
|
|
22
|
+
expireAfterSeconds: PAGEVIEW_TTL_SECONDS,
|
|
23
|
+
name: "pageviews_ttl_30d"
|
|
24
|
+
}), col.createIndex({
|
|
25
|
+
projectId: 1,
|
|
26
|
+
createdAt: -1
|
|
27
|
+
}, {
|
|
28
|
+
name: "pageviews_project_recent"
|
|
29
|
+
})]).catch(error => {
|
|
30
|
+
indexesPromise = null;
|
|
31
|
+
throw error;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return indexesPromise;
|
|
35
|
+
}
|
|
36
|
+
router.get("/", async (req, res) => {
|
|
37
|
+
try {
|
|
38
|
+
var _req$user;
|
|
39
|
+
if (Number((_req$user = req.user) === null || _req$user === void 0 ? void 0 : _req$user.access) !== 100) {
|
|
40
|
+
return res.status(403).json({
|
|
41
|
+
success: false,
|
|
42
|
+
message: "Only an admin can view page views."
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const db = await getDb();
|
|
46
|
+
await ensurePageviewIndexes(db);
|
|
47
|
+
const rows = await db.collection("pageviews").find(recentPageviewFilter(req.projectId), {
|
|
48
|
+
projection: PAGEVIEW_LIST_FIELDS
|
|
49
|
+
}).sort({
|
|
50
|
+
createdAt: -1
|
|
51
|
+
}).limit(PAGEVIEW_LIST_LIMIT).toArray();
|
|
52
|
+
return res.json({
|
|
53
|
+
success: true,
|
|
54
|
+
data: rows
|
|
55
|
+
});
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.error("Error listing pageviews:", error);
|
|
58
|
+
return res.status(500).json({
|
|
59
|
+
success: false,
|
|
60
|
+
message: "Failed to load page views."
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
router.post("/", async (req, res) => {
|
|
65
|
+
try {
|
|
66
|
+
const built = buildPageview({
|
|
67
|
+
projectId: req.projectId,
|
|
68
|
+
user: req.user,
|
|
69
|
+
body: req.body
|
|
70
|
+
});
|
|
71
|
+
if (!built.ok) {
|
|
72
|
+
return res.status(built.status).json({
|
|
73
|
+
success: false,
|
|
74
|
+
message: built.message
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
const db = await getDb();
|
|
78
|
+
await ensurePageviewIndexes(db);
|
|
79
|
+
const result = await db.collection("pageviews").insertOne(built.doc);
|
|
80
|
+
return res.status(201).json({
|
|
81
|
+
success: true,
|
|
82
|
+
id: result.insertedId
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error("Error creating pageview:", error);
|
|
86
|
+
return res.status(500).json({
|
|
87
|
+
success: false,
|
|
88
|
+
message: "Failed to record page view."
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
module.exports = router;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const PAGEVIEW_TTL_SECONDS = 30 * 24 * 60 * 60;
|
|
4
|
+
const PAGEVIEW_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
|
|
5
|
+
const PAGEVIEW_LIST_LIMIT = 200;
|
|
6
|
+
const PAGEVIEW_LIST_FIELDS = {
|
|
7
|
+
projectId: 1,
|
|
8
|
+
userId: 1,
|
|
9
|
+
page: 1,
|
|
10
|
+
session: 1,
|
|
11
|
+
referrer: 1,
|
|
12
|
+
createdAt: 1
|
|
13
|
+
};
|
|
14
|
+
const MAX_PATH = 300;
|
|
15
|
+
const MAX_SESSION = 64;
|
|
16
|
+
function stripQueryAndHash(value) {
|
|
17
|
+
const raw = String(value || "").trim();
|
|
18
|
+
if (!raw) return "";
|
|
19
|
+
const noHash = raw.split("#")[0];
|
|
20
|
+
return noHash.split("?")[0].trim();
|
|
21
|
+
}
|
|
22
|
+
function asPath(value) {
|
|
23
|
+
const stripped = stripQueryAndHash(value);
|
|
24
|
+
if (!stripped) return "";
|
|
25
|
+
try {
|
|
26
|
+
if (/^https?:\/\//i.test(stripped)) {
|
|
27
|
+
const url = new URL(stripped);
|
|
28
|
+
return url.pathname || "/";
|
|
29
|
+
}
|
|
30
|
+
} catch {
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
if (!stripped.startsWith("/")) return "";
|
|
34
|
+
return stripped.slice(0, MAX_PATH);
|
|
35
|
+
}
|
|
36
|
+
function asReferrer(value) {
|
|
37
|
+
const stripped = stripQueryAndHash(value);
|
|
38
|
+
if (!stripped) return null;
|
|
39
|
+
try {
|
|
40
|
+
if (/^https?:\/\//i.test(stripped)) {
|
|
41
|
+
const url = new URL(stripped);
|
|
42
|
+
const path = `${url.origin}${url.pathname || "/"}`;
|
|
43
|
+
return path.slice(0, MAX_PATH);
|
|
44
|
+
}
|
|
45
|
+
} catch {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
if (!stripped.startsWith("/")) return null;
|
|
49
|
+
return stripped.slice(0, MAX_PATH);
|
|
50
|
+
}
|
|
51
|
+
function asSession(value) {
|
|
52
|
+
const raw = String(value || "").trim();
|
|
53
|
+
if (!/^[A-Za-z0-9_-]{8,64}$/.test(raw)) return "";
|
|
54
|
+
return raw.slice(0, MAX_SESSION);
|
|
55
|
+
}
|
|
56
|
+
function actorId(user) {
|
|
57
|
+
if (user && user.impersonatedBy) return String(user.impersonatedBy);
|
|
58
|
+
if (user && user.powrId) return String(user.powrId);
|
|
59
|
+
return "";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* One page view. Drops query, hash, and every field that is not page / session / referrer.
|
|
64
|
+
* Login As stores the admin (impersonatedBy), not the person being viewed.
|
|
65
|
+
*/
|
|
66
|
+
function buildPageview({
|
|
67
|
+
projectId,
|
|
68
|
+
user,
|
|
69
|
+
body
|
|
70
|
+
}) {
|
|
71
|
+
const page = asPath(body && body.page);
|
|
72
|
+
const session = asSession(body && body.session);
|
|
73
|
+
const userId = actorId(user);
|
|
74
|
+
const project = String(projectId || "").trim();
|
|
75
|
+
if (!project) {
|
|
76
|
+
return {
|
|
77
|
+
ok: false,
|
|
78
|
+
status: 400,
|
|
79
|
+
message: "projectId is required"
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
if (!userId) {
|
|
83
|
+
return {
|
|
84
|
+
ok: false,
|
|
85
|
+
status: 401,
|
|
86
|
+
message: "Sign in required"
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
if (!page) {
|
|
90
|
+
return {
|
|
91
|
+
ok: false,
|
|
92
|
+
status: 400,
|
|
93
|
+
message: "page is required"
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (!session) {
|
|
97
|
+
return {
|
|
98
|
+
ok: false,
|
|
99
|
+
status: 400,
|
|
100
|
+
message: "session is required"
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const doc = {
|
|
104
|
+
projectId: project,
|
|
105
|
+
userId,
|
|
106
|
+
page,
|
|
107
|
+
session,
|
|
108
|
+
createdAt: new Date()
|
|
109
|
+
};
|
|
110
|
+
const referrer = asReferrer(body && body.referrer);
|
|
111
|
+
if (referrer) doc.referrer = referrer;
|
|
112
|
+
return {
|
|
113
|
+
ok: true,
|
|
114
|
+
doc
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function recentPageviewFilter(projectId, now = new Date()) {
|
|
118
|
+
return {
|
|
119
|
+
projectId: String(projectId || ""),
|
|
120
|
+
createdAt: {
|
|
121
|
+
$gte: new Date(now.getTime() - PAGEVIEW_WINDOW_MS)
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
module.exports = {
|
|
126
|
+
PAGEVIEW_TTL_SECONDS,
|
|
127
|
+
PAGEVIEW_LIST_LIMIT,
|
|
128
|
+
PAGEVIEW_LIST_FIELDS,
|
|
129
|
+
buildPageview,
|
|
130
|
+
recentPageviewFilter
|
|
131
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
buildPageview,
|
|
5
|
+
PAGEVIEW_TTL_SECONDS,
|
|
6
|
+
recentPageviewFilter
|
|
7
|
+
} = require("./track");
|
|
8
|
+
const projectId = "507f1f77bcf86cd799439011";
|
|
9
|
+
const member = "aaaaaaaaaaaaaaaaaaaaaaaa";
|
|
10
|
+
const admin = "bbbbbbbbbbbbbbbbbbbbbbbb";
|
|
11
|
+
function view(body, user) {
|
|
12
|
+
return buildPageview({
|
|
13
|
+
projectId,
|
|
14
|
+
user: user || {
|
|
15
|
+
powrId: member
|
|
16
|
+
},
|
|
17
|
+
body
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
describe("buildPageview", () => {
|
|
21
|
+
it("keeps page, session, referrer, and project", () => {
|
|
22
|
+
const result = view({
|
|
23
|
+
page: "/desk?token=secret",
|
|
24
|
+
session: "sess_abc123",
|
|
25
|
+
referrer: "https://excelrs.dev/start?utm=1#x",
|
|
26
|
+
phone: "9304251332",
|
|
27
|
+
body: "form text"
|
|
28
|
+
});
|
|
29
|
+
expect(result.ok).toBe(true);
|
|
30
|
+
expect(result.doc.projectId).toBe(projectId);
|
|
31
|
+
expect(result.doc.userId).toBe(member);
|
|
32
|
+
expect(result.doc.page).toBe("/desk");
|
|
33
|
+
expect(result.doc.session).toBe("sess_abc123");
|
|
34
|
+
expect(result.doc.referrer).toBe("https://excelrs.dev/start");
|
|
35
|
+
expect(result.doc.phone).toBeUndefined();
|
|
36
|
+
expect(result.doc.body).toBeUndefined();
|
|
37
|
+
expect(result.doc.createdAt).toBeInstanceOf(Date);
|
|
38
|
+
});
|
|
39
|
+
it("stores the admin when Login As is active", () => {
|
|
40
|
+
const result = view({
|
|
41
|
+
page: "/home",
|
|
42
|
+
session: "sess_abc123"
|
|
43
|
+
}, {
|
|
44
|
+
powrId: member,
|
|
45
|
+
impersonatedBy: admin
|
|
46
|
+
});
|
|
47
|
+
expect(result.doc.userId).toBe(admin);
|
|
48
|
+
});
|
|
49
|
+
it("rejects a missing page and a short session", () => {
|
|
50
|
+
expect(view({
|
|
51
|
+
session: "sess_abc123"
|
|
52
|
+
}).status).toBe(400);
|
|
53
|
+
expect(view({
|
|
54
|
+
page: "/home",
|
|
55
|
+
session: "short"
|
|
56
|
+
}).status).toBe(400);
|
|
57
|
+
});
|
|
58
|
+
it("drops a referrer that is not a path or url", () => {
|
|
59
|
+
const result = view({
|
|
60
|
+
page: "/home",
|
|
61
|
+
session: "sess_abc123",
|
|
62
|
+
referrer: "not a url"
|
|
63
|
+
});
|
|
64
|
+
expect(result.doc.referrer).toBeUndefined();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
describe("recentPageviewFilter", () => {
|
|
68
|
+
it("keeps the last 7 days for one project", () => {
|
|
69
|
+
const now = new Date("2026-09-29T00:00:00.000Z");
|
|
70
|
+
const filter = recentPageviewFilter(projectId, now);
|
|
71
|
+
expect(filter.projectId).toBe(projectId);
|
|
72
|
+
expect(filter.createdAt.$gte.toISOString()).toBe("2026-09-22T00:00:00.000Z");
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe("pageview ttl", () => {
|
|
76
|
+
it("expires after 30 days", () => {
|
|
77
|
+
expect(PAGEVIEW_TTL_SECONDS).toBe(30 * 24 * 60 * 60);
|
|
78
|
+
});
|
|
79
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.14.0",
|
|
4
4
|
"description": "Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|