-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfetch-analytics-data.js
190 lines (161 loc) · 4.59 KB
/
fetch-analytics-data.js
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
import 'dotenv/config'
import fs from "fs";
import glob from "fast-glob";
import matter from "gray-matter";
import { queryData as queryElizabeaconData } from "./_11ty/fetch-analytics/elizabeacon.js";
import { fetchData as queryGoatcounterData } from "./_11ty/fetch-analytics/goatcounter.js";
import googleData from "./_11ty/fetch-analytics/google-analytics-export.json" with { type: "json" };
const MINIMUM_PAGEVIEWS = 50;
async function fetchAnalyticsData() {
let elizabeaconData = await queryElizabeaconData();
let goatcounterData = await queryGoatcounterData();
let unordered = [];
let visited = {};
for(let entry of Object.values(googleData)) {
let {url, pageViews} = entry;
if(url.startsWith("/opengraph/")) {
continue;
}
visited[url] = true;
unordered.push({
url,
pageViews: parseInt(pageViews) + (elizabeaconData[url]?.count || 0) + (goatcounterData[url]?.count || 0),
})
}
// add urls not in google data
for(let url in elizabeaconData) {
if(url.startsWith("/opengraph/")) {
continue;
}
if(!visited[url]) {
visited[url] = true;
unordered.push({
url,
pageViews: elizabeaconData[url].count + (goatcounterData[url]?.count || 0)
})
}
}
// add urls not in google data
for(let url in goatcounterData) {
if(url.startsWith("/opengraph/")) {
continue;
}
if(!visited[url]) {
visited[url] = true;
unordered.push({
url,
pageViews: goatcounterData[url].count
})
}
}
return unordered.sort((a, b) => {
return b.pageViews - a.pageViews;
});
};
function getInputMap(globs) {
let results = {};
let alternates = {};
let entries = glob.sync(globs);
for(let inputPath of entries) {
let result = {
inputPath,
};
let matcher = inputPath.match(/(\d{4}\-\d{2}\-\d{2})\-([^\/]+)/);
if(matcher && matcher.length) {
let [, date, filename] = matcher;
let slugSplit = filename.split(".");
slugSplit.pop();
let slug = slugSplit.join(".");
result.url = `/web/${slug}/`;
// old /YYYY/MM/DD/ urls
alternates[`/web/${date.split("-").join("/")}/${slug}/`] = result.url;
result.date = date;
result.time = (new Date(date)).getTime();
}
// if permalink set in front matter, use that.
let content = fs.readFileSync(inputPath, 'utf8');
let frontmatter = matter( content );
if(frontmatter.data.permalink) {
result.url = frontmatter.data.permalink;
}
results[result.url] = result;
}
return {
inputMap: results,
alternates,
};
}
function getPageViewsPerDayRanks(analyticsData) {
let ranksForDaysPosted = [];
for(let entry of analyticsData) {
if(entry.from) {
ranksForDaysPosted.push({
url: entry.url,
pageViewsPerDay: entry.pageViewsPerDay,
});
}
}
ranksForDaysPosted.sort((a, b) => {
return b.pageViewsPerDay - a.pageViewsPerDay;
});
let lookupRanksForDaysPosted = {};
let k = 1;
for(let entry of ranksForDaysPosted) {
lookupRanksForDaysPosted[entry.url] = k;
k++;
}
return lookupRanksForDaysPosted;
}
(async () => {
let { inputMap, alternates } = getInputMap(["./_posts/*.md"]);
let analyticsData = await fetchAnalyticsData();
// Important: these may not exist in analytics data
let alternatesPageViews = {};
for(let entry of analyticsData) {
let rootUrl = alternates[entry.url];
if(rootUrl) {
if(!alternatesPageViews[rootUrl]) {
alternatesPageViews[rootUrl] = 0;
}
alternatesPageViews[rootUrl] += entry.pageViews;
}
}
// Add /web/YYYY/MM/DD/slug urls to root /web/slug data
for(let entry of analyticsData) {
if(alternatesPageViews[entry.url]) {
entry.pageViews += alternatesPageViews[entry.url];
}
}
let j = 1;
let totalPageViews = 0;
for(let entry of analyticsData) {
if(entry.url.startsWith("/search?") || entry.url.startsWith("/translate_c?")) {
continue;
}
if(inputMap[entry.url]) {
entry.from = inputMap[entry.url];
let pageViewsPerDay = entry.pageViews / ((Date.now() - entry.from.time) / (1000*60*60*24));
entry.pageViewsPerDay = pageViewsPerDay;
entry.rankTotal = j;
j++;
}
if(entry.pageViews) {
totalPageViews += entry.pageViews;
}
}
let rankedPageViewsPerDay = getPageViewsPerDayRanks(analyticsData);
for(let entry of analyticsData) {
if(rankedPageViewsPerDay[entry.url]) {
entry.rankPerDaysPosted = rankedPageViewsPerDay[entry.url];
}
}
let finalResults = {};
for(let entry of analyticsData) {
if(entry.pageViews > MINIMUM_PAGEVIEWS) {
finalResults[entry.url] = entry;
}
}
let globalDataFilePath = "./_data/analytics.json";
fs.writeFileSync(globalDataFilePath, JSON.stringify(finalResults, null, 2), "utf8");
console.log( `Wrote ${globalDataFilePath} with ${totalPageViews} total page views.` );
})();