@liiift-studio/sanity-visitor-insights 0.2.0 → 0.2.2

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/server.mjs CHANGED
@@ -369,33 +369,43 @@ function sumFirstMetric(report) {
369
369
  }
370
370
 
371
371
  // src/server/vercel.ts
372
- var API_BASE = "https://api.vercel.com";
372
+ var API_BASE = "https://api.vercel.com/v1/query/web-analytics";
373
373
  function createVercelClient(projectId, token, teamId) {
374
+ async function query(path, extra) {
375
+ const params = new URLSearchParams({ projectId, ...extra });
376
+ if (teamId) params.set("teamId", teamId);
377
+ const response = await fetch(`${API_BASE}/${path}?${params.toString()}`, {
378
+ headers: { Authorization: `Bearer ${token}` }
379
+ });
380
+ if (!response.ok) {
381
+ throw new Error(`Vercel Web Analytics ${path} failed with ${response.status}`);
382
+ }
383
+ return await response.json();
384
+ }
374
385
  return {
375
386
  async pageviews(start, end) {
376
- const params = new URLSearchParams({
377
- projectId,
378
- since: `${start}T00:00:00.000Z`,
379
- until: `${end}T23:59:59.999Z`
380
- });
381
- if (teamId) params.set("teamId", teamId);
382
- const response = await fetch(`${API_BASE}/v1/web-analytics/timeseries?${params.toString()}`, {
383
- headers: { Authorization: `Bearer ${token}` }
384
- });
385
- if (!response.ok) {
386
- throw new Error(`Vercel Web Analytics failed with ${response.status}`);
387
- }
388
- const body = await response.json();
387
+ const [totals, series] = await Promise.all([
388
+ query("visits/count", {
389
+ since: start,
390
+ until: end
391
+ }),
392
+ query("visits/aggregate", {
393
+ since: start,
394
+ until: end,
395
+ by: "day",
396
+ limit: "100"
397
+ })
398
+ ]);
389
399
  const byDate = {};
390
- let total = 0;
391
- for (const point of body.data ?? []) {
392
- if (!point.key) continue;
393
- const date = point.key.slice(0, 10);
394
- const count = typeof point.total === "number" ? point.total : 0;
395
- byDate[date] = (byDate[date] ?? 0) + count;
396
- total += count;
400
+ for (const row of series.data ?? []) {
401
+ if (!row.timestamp) continue;
402
+ byDate[row.timestamp.slice(0, 10)] = row.pageviews ?? 0;
397
403
  }
398
- return { byDate, total };
404
+ return {
405
+ byDate,
406
+ total: totals.data?.pageviews ?? 0,
407
+ visitors: totals.data?.visitors ?? 0
408
+ };
399
409
  }
400
410
  };
401
411
  }
@@ -1137,12 +1147,15 @@ export {
1137
1147
  clearCache,
1138
1148
  coverageForRange,
1139
1149
  coverageNotices,
1150
+ createGa4Client,
1151
+ createVercelClient,
1140
1152
  createVisitorInsightsHandler,
1141
1153
  daysBetween,
1142
1154
  formatInTimeZone,
1143
1155
  isReportName,
1144
1156
  isValidTimeZone,
1145
1157
  ok,
1158
+ parseServiceAccountKey,
1146
1159
  partial,
1147
1160
  previousRange,
1148
1161
  provisionalDates,