@daemonitor/plugins 1.1.39 → 1.1.40

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.
@@ -10,7 +10,9 @@ import { getJson, googleAccessToken, shiftDate, todayIn } from "./AdsensePlugin.
10
10
  // property asks the Data API for: active users right now (realtime), users,
11
11
  // sessions and page views for today, yesterday and the last 7 days, and 30
12
12
  // days of daily figures. 7-day users come from GA itself, never from summing
13
- // days: one person on three days is one user, not three.
13
+ // days: one person on three days is one user, not three. Each property also
14
+ // says which sites it covers (its web streams) and its most viewed pages, so a
15
+ // site in daemonitor can show only its own.
14
16
  //
15
17
  // The report carries its own 30-day history, so the server stores no history
16
18
  // rows for this type. Hiding properties and nicknames live in the portal.
@@ -38,9 +40,10 @@ const totals = (row) => row ? { users: num(row.metricValues?.[0]), sessions: num
38
40
  * batchRunReports response -> the figures the portal shows.
39
41
  * Report 0: no dimensions, three named date ranges (today, yesterday, last7).
40
42
  * Report 1: one row per date over the last 30 days.
43
+ * Reports 2 and 3: the most viewed pages over 30 days and since yesterday.
41
44
  */
42
45
  export function parseProperty(batch, now = new Date()) {
43
- const [summary, byDate] = batch?.reports || [];
46
+ const [summary, byDate, popular, trending] = batch?.reports || [];
44
47
  // A multi-range report adds a "dateRange" dimension holding each range's name.
45
48
  const range = (name) => totals((summary?.rows || []).find((r) => r?.dimensionValues?.[0]?.value === name));
46
49
  // Gap-fill the 30 days in the property's own time zone: GA leaves days with
@@ -65,11 +68,87 @@ export function parseProperty(batch, now = new Date()) {
65
68
  sessions: days.map((d) => d.sessions),
66
69
  views: days.map((d) => d.views),
67
70
  },
71
+ pages: { popular: parsePages(popular), trending: parsePages(trending) },
68
72
  };
69
73
  }
70
74
  /** runRealtimeReport -> users active in the last 30 minutes. No rows means zero. */
71
75
  export const parseRealtime = (r) => num(r?.rows?.[0]?.metricValues?.[0]);
72
- export const BATCH_BODY = {
76
+ // What a list of top pages leaves out: the home page, section fronts (a single
77
+ // path segment) and archives, which outrank every article and say nothing.
78
+ export const EXCLUDE_REGEX = ["^/([^/]+)?/?$", "^/(calendar|category|tag|author)/.*$"];
79
+ // Per host, per list: a property's reports stay small enough to send every poll.
80
+ const PAGES_PER_HOST = 10;
81
+ /** A runReport request for a date range's most viewed pages. `/a` and `/a/` are one page, so both are excluded. */
82
+ export function pagesRequest(startDate, exclude = []) {
83
+ const exact = exclude.flatMap((p) => [p.replace(/\/$/, ""), `${p.replace(/\/$/, "")}/`]);
84
+ return {
85
+ dateRanges: [{ startDate, endDate: "today" }],
86
+ dimensions: [{ name: "hostName" }, { name: "pagePath" }, { name: "pageTitle" }],
87
+ metrics: [{ name: "screenPageViews" }],
88
+ orderBys: [{ metric: { metricName: "screenPageViews" }, desc: true }],
89
+ limit: 50,
90
+ dimensionFilter: {
91
+ notExpression: {
92
+ orGroup: {
93
+ expressions: [
94
+ ...(exact.length ? [{ filter: { fieldName: "pagePath", inListFilter: { values: exact } } }] : []),
95
+ ...EXCLUDE_REGEX.map((value) => ({ filter: { fieldName: "pagePath", stringFilter: { matchType: "FULL_REGEXP", value } } })),
96
+ ],
97
+ },
98
+ },
99
+ },
100
+ };
101
+ }
102
+ const bareHost = (h) => h.trim().toLowerCase().replace(/^www\./, "");
103
+ /**
104
+ * A pages report -> the most viewed pages per host. `/a` and `/a/` add up, and a
105
+ * site name most titles end in (" | The Architect's Newspaper") comes off.
106
+ */
107
+ export function parsePages(report) {
108
+ const rows = (report?.rows || []).map((r) => ({
109
+ host: bareHost(String(r?.dimensionValues?.[0]?.value || "")),
110
+ path: String(r?.dimensionValues?.[1]?.value || "").replace(/(.)\/$/, "$1"),
111
+ title: String(r?.dimensionValues?.[2]?.value || ""),
112
+ views: num(r?.metricValues?.[0]),
113
+ }));
114
+ const tails = new Map();
115
+ for (const r of rows) {
116
+ const m = /\s[|–—-]\s[^|–—-]+$/.exec(r.title);
117
+ if (m)
118
+ tails.set(m[0], (tails.get(m[0]) ?? 0) + 1);
119
+ }
120
+ const [tail, n] = [...tails].sort((a, b) => b[1] - a[1])[0] ?? ["", 0];
121
+ const shared = n >= 2 && n >= rows.length * 0.6 ? tail : "";
122
+ const by = new Map();
123
+ for (const r of rows) {
124
+ const key = `${r.host}${r.path}`;
125
+ const had = by.get(key);
126
+ if (had)
127
+ had.views += r.views;
128
+ else
129
+ by.set(key, { ...r, title: shared && r.title.endsWith(shared) ? r.title.slice(0, -shared.length) : r.title });
130
+ }
131
+ const counts = new Map();
132
+ return [...by.values()].sort((a, b) => b.views - a.views).filter((p) => {
133
+ const c = (counts.get(p.host) ?? 0) + 1;
134
+ counts.set(p.host, c);
135
+ return c <= PAGES_PER_HOST;
136
+ });
137
+ }
138
+ /** dataStreams response -> the sites a property covers: each web stream's host, without www. */
139
+ export function parseDomains(r) {
140
+ const uris = (r?.dataStreams || []).map((s) => s?.webStreamData?.defaultUri).filter(Boolean);
141
+ const hosts = uris.flatMap((u) => {
142
+ try {
143
+ return [bareHost(new URL(u.includes("://") ? u : `https://${u}`).hostname)];
144
+ }
145
+ catch {
146
+ return [];
147
+ }
148
+ });
149
+ return [...new Set(hosts)];
150
+ }
151
+ export const batchBody = (exclude = []) => ({
73
152
  requests: [
74
153
  {
75
154
  dateRanges: [
@@ -85,8 +164,11 @@ export const BATCH_BODY = {
85
164
  metrics: METRICS,
86
165
  limit: DAYS + 5,
87
166
  },
167
+ // Popular over 30 days, trending since yesterday, as the site's own lists are.
168
+ pagesRequest("30daysAgo", exclude),
169
+ pagesRequest("1daysAgo", exclude),
88
170
  ],
89
- };
171
+ });
90
172
  async function inBatches(items, size, fn) {
91
173
  const out = [];
92
174
  for (let i = 0; i < items.length; i += size)
@@ -120,11 +202,12 @@ export function createGa4Plugin() {
120
202
  account.properties = await inBatches(account.properties, CONCURRENCY, async (p) => {
121
203
  // One property failing (no access to its data, quota) must not hide the rest.
122
204
  try {
123
- const [batch, realtime] = await Promise.all([
124
- post(`${DATA}/properties/${p.id}:batchRunReports`, BATCH_BODY),
205
+ const [batch, realtime, streams] = await Promise.all([
206
+ post(`${DATA}/properties/${p.id}:batchRunReports`, batchBody(cfg.excludePages)),
125
207
  post(`${DATA}/properties/${p.id}:runRealtimeReport`, { metrics: [{ name: "activeUsers" }] }).catch(() => null),
208
+ get(`${ADMIN}/properties/${p.id}/dataStreams?pageSize=50`).catch(() => null),
126
209
  ]);
127
- return { ...p, ...parseProperty(batch), activeNow: realtime ? parseRealtime(realtime) : null };
210
+ return { ...p, ...parseProperty(batch), activeNow: realtime ? parseRealtime(realtime) : null, domains: parseDomains(streams) };
128
211
  }
129
212
  catch (e) {
130
213
  return { ...p, error: String(e?.message || e).slice(0, 160) };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daemonitor/plugins",
3
- "version": "1.1.39",
3
+ "version": "1.1.40",
4
4
  "description": "daemonitor plugins",
5
5
  "types": "types/index.d.ts",
6
6
  "main": "dist/index.js",