Repository navigation
Expand file tree
/
Copy pathanalyze-pageview-data.mjs
More file actions
299 lines (251 loc) · 9.37 KB
/
Copy pathanalyze-pageview-data.mjs
File metadata and controls
299 lines (251 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
const csvPath = join(import.meta.dirname, 'harper-docs-analytics.csv');
const dataRaw = readFileSync(csvPath, 'utf8');
const data = dataRaw.split('\r\n').slice(9); // remove first 9 lines
// Safe to naively parse CSV since:
// - there are no `,` characters within the header text
data.shift(); // Remove header row
// - nor are there `,` characters within any of the data values
const records = data.map((row) => row.split(','));
// Parse records into objects with typed values
const pages = records
.filter((row) => row.length >= 2 && row[0]) // Filter out empty rows
.map((row) => ({
path: row[0],
views: parseInt(row[1]) || 0,
activeUsers: parseInt(row[2]) || 0,
}));
// Analysis Functions
function getTotalViews() {
return pages.reduce((sum, page) => sum + page.views, 0);
}
function getPathCount() {
return pages.length;
}
function getViewsByRootPath() {
const rootPaths = {};
pages.forEach((page) => {
const parts = page.path.split('/').filter(Boolean);
const root = parts[0] || 'root';
if (!rootPaths[root]) {
rootPaths[root] = { views: 0, paths: 0 };
}
rootPaths[root].views += page.views;
rootPaths[root].paths += 1;
});
return rootPaths;
}
function getTopPagesByViews(n) {
return [...pages].sort((a, b) => b.views - a.views).slice(0, n);
}
function getCumulativeViewPercentages() {
const sorted = [...pages].sort((a, b) => b.views - a.views);
const totalViews = getTotalViews();
const percentages = [];
let cumulative = 0;
sorted.forEach((page, index) => {
cumulative += page.views;
const percentage = (cumulative / totalViews) * 100;
percentages.push({
rank: index + 1,
path: page.path,
views: page.views,
cumulativeViews: cumulative,
cumulativePercentage: percentage,
});
});
return percentages;
}
function findCoverageThresholds(percentages) {
const thresholds = [50, 75, 80, 90, 95, 99];
const results = [];
thresholds.forEach((threshold) => {
const index = percentages.findIndex((p) => p.cumulativePercentage >= threshold);
if (index !== -1) {
results.push({
percentage: threshold,
pathCount: index + 1,
totalPaths: pages.length,
pathPercentage: (((index + 1) / pages.length) * 100).toFixed(2),
});
}
});
return results;
}
// Output Functions
function printTotalStats() {
console.log('Total Statistics');
console.log('Overview of the entire dataset');
console.log('Results:');
console.log(` Total Paths: ${getPathCount().toLocaleString()}`);
console.log(` Total Views: ${getTotalViews().toLocaleString()}`);
console.log(` Average Views per Path: ${(getTotalViews() / getPathCount()).toFixed(2)}`);
console.log();
}
function printTopPages(n = 20) {
const top = getTopPagesByViews(n);
const totalViews = getTotalViews();
console.log(`Top ${n} Pages by Views`);
console.log(`The most viewed pages and their contribution to total site views`);
console.log('Results:');
top.forEach((page, index) => {
const percentage = ((page.views / totalViews) * 100).toFixed(2);
console.log(` ${(index + 1).toString().padStart(2)}. ${page.path}`);
console.log(` ${page.views.toLocaleString()} views (${percentage}%)`);
});
console.log();
}
function printRootPathAnalysis() {
const rootPaths = getViewsByRootPath();
const totalViews = getTotalViews();
const mainPaths = ['docs', 'release-notes', 'fabric', 'learn'];
console.log('Views by Root Path');
console.log('Distribution of views across top-level paths');
console.log('Results:');
// Display main paths
mainPaths.forEach((root) => {
const stats = rootPaths[root];
if (stats) {
const percentage = ((stats.views / totalViews) * 100).toFixed(2);
console.log(` /${root}`);
console.log(` Views: ${stats.views.toLocaleString()} (${percentage}%)`);
console.log(` Paths: ${stats.paths.toLocaleString()}`);
console.log(` Avg Views/Path: ${(stats.views / stats.paths).toFixed(2)}`);
}
});
// List other root paths
const otherRoots = Object.keys(rootPaths)
.filter((root) => !mainPaths.includes(root))
.sort((a, b) => rootPaths[b].views - rootPaths[a].views);
if (otherRoots.length > 0) {
console.log(' Other root paths:');
otherRoots.forEach((root) => {
const stats = rootPaths[root];
const percentage = ((stats.views / totalViews) * 100).toFixed(2);
console.log(` /${root}: ${stats.views.toLocaleString()} views (${percentage}%), ${stats.paths} paths`);
});
}
console.log();
}
function printCoverageThresholds() {
const percentages = getCumulativeViewPercentages();
const thresholds = findCoverageThresholds(percentages);
console.log('Coverage Analysis');
console.log('How many paths account for X% of total views');
console.log('Results:');
thresholds.forEach((threshold) => {
console.log(
` ${threshold.percentage}% of views: ${threshold.pathCount} paths (${threshold.pathPercentage}% of all paths)`
);
});
console.log();
}
function printViewCountDistribution() {
const totalViews = getTotalViews();
const totalPaths = pages.length;
// Create ranges for high traffic
const highTrafficRanges = [];
for (let i = 100; i < 10000; i += 50) {
highTrafficRanges.push({ min: i, max: i + 49 });
}
// Calculate stats for each category
const highTrafficPaths = pages.filter((p) => p.views >= 100);
const mediumTrafficPaths = pages.filter((p) => p.views >= 10 && p.views < 100);
const lowTrafficPaths = pages.filter((p) => p.views >= 1 && p.views < 10);
const zeroTrafficPaths = pages.filter((p) => p.views === 0);
const highTrafficViews = highTrafficPaths.reduce((sum, p) => sum + p.views, 0);
const mediumTrafficViews = mediumTrafficPaths.reduce((sum, p) => sum + p.views, 0);
const lowTrafficViews = lowTrafficPaths.reduce((sum, p) => sum + p.views, 0);
console.log('View Count Distribution');
console.log('Number of paths grouped by their view count');
console.log('Results:');
// High traffic breakdown
console.log(' High traffic (100+ views):');
console.log(
` Total Paths: ${highTrafficPaths.length} (${((highTrafficPaths.length / totalPaths) * 100).toFixed(2)}% of paths)`
);
console.log(
` Total Views: ${highTrafficViews.toLocaleString()} (${((highTrafficViews / totalViews) * 100).toFixed(2)}% of views)`
);
console.log(` Breakdown by range:`);
highTrafficRanges.forEach((range) => {
const pathsInRange = highTrafficPaths.filter((p) => p.views >= range.min && p.views <= range.max);
if (pathsInRange.length > 0) {
const viewsInRange = pathsInRange.reduce((sum, p) => sum + p.views, 0);
console.log(
` ${range.min}-${range.max} views: ${pathsInRange.length} paths (${viewsInRange.toLocaleString()} views)`
);
}
});
// Medium traffic
console.log(' Medium traffic (10-99 views):');
console.log(
` Total Paths: ${mediumTrafficPaths.length} (${((mediumTrafficPaths.length / totalPaths) * 100).toFixed(2)}% of paths)`
);
console.log(
` Total Views: ${mediumTrafficViews.toLocaleString()} (${((mediumTrafficViews / totalViews) * 100).toFixed(2)}% of views)`
);
// Low traffic
console.log(' Low traffic (1-9 views):');
console.log(
` Total Paths: ${lowTrafficPaths.length} (${((lowTrafficPaths.length / totalPaths) * 100).toFixed(2)}% of paths)`
);
console.log(
` Total Views: ${lowTrafficViews.toLocaleString()} (${((lowTrafficViews / totalViews) * 100).toFixed(2)}% of views)`
);
// Zero views
console.log(' Zero views:');
console.log(
` Total Paths: ${zeroTrafficPaths.length} (${((zeroTrafficPaths.length / totalPaths) * 100).toFixed(2)}% of paths)`
);
console.log();
}
function printLongTailAnalysis() {
const sorted = [...pages].sort((a, b) => b.views - a.views);
const totalViews = getTotalViews();
const totalPaths = pages.length;
console.log('Long Tail Analysis');
console.log('Understanding the distribution of low-traffic pages');
console.log('Results:');
const singleDigitViews = sorted.filter((p) => p.views < 10 && p.views > 0);
const singleDigitViewsTotal = singleDigitViews.reduce((sum, p) => sum + p.views, 0);
const singleDigitPercentage = ((singleDigitViewsTotal / totalViews) * 100).toFixed(2);
console.log(` Paths with 1-9 views:`);
console.log(
` Count: ${singleDigitViews.length} (${((singleDigitViews.length / totalPaths) * 100).toFixed(2)}% of paths)`
);
console.log(` Total Views: ${singleDigitViewsTotal.toLocaleString()} (${singleDigitPercentage}% of total views)`);
const zeroViews = sorted.filter((p) => p.views === 0);
console.log(` Paths with 0 views:`);
console.log(` Count: ${zeroViews.length} (${((zeroViews.length / totalPaths) * 100).toFixed(2)}% of paths)`);
console.log();
}
function printRedirectStrategyData() {
const percentages = getCumulativeViewPercentages();
const totalPaths = pages.length;
console.log('Redirect Strategy Quick Reference');
console.log('Key data points for redirect planning');
console.log('Results:');
[10, 25, 50, 100, 200].forEach((n) => {
if (n <= totalPaths) {
const data = percentages[n - 1];
const percentage = data.cumulativePercentage.toFixed(2);
console.log(` Top ${n} paths: ${percentage}% of views`);
}
});
console.log();
}
// Run all analyses
console.log('Harper Docs Analytics Report');
console.log('Data Period: October 21, 2025 - February 4, 2026');
console.log();
console.log('═'.repeat(80));
console.log();
printTotalStats();
printCoverageThresholds();
printRedirectStrategyData();
printTopPages(25);
printRootPathAnalysis();
printViewCountDistribution();
printLongTailAnalysis();