( pathnames: string[], days = 30 )
| 258 | } |
| 259 | |
| 260 | export async function fetchPathPageviewCounts( |
| 261 | pathnames: string[], |
| 262 | days = 30 |
| 263 | ): Promise<PathPageviewCounts | null> { |
| 264 | const uniquePathnames = Array.from( |
| 265 | new Set( |
| 266 | pathnames |
| 267 | .filter((pathname): pathname is string => typeof pathname === "string" && pathname.trim().length > 0) |
| 268 | .map((pathname) => pathname.trim()) |
| 269 | ) |
| 270 | ); |
| 271 | |
| 272 | if (uniquePathnames.length === 0) { |
| 273 | return {}; |
| 274 | } |
| 275 | |
| 276 | const cacheKey = `${days}:${uniquePathnames.join("|")}`; |
| 277 | const cached = pathPageviewCache.get(cacheKey); |
| 278 | if (cached && Date.now() - cached.timestamp < PATH_PAGEVIEW_CACHE_TTL_MS) { |
| 279 | return cached.data; |
| 280 | } |
| 281 | |
| 282 | const config = getPostHogQueryConfig(); |
| 283 | if (!config) return null; |
| 284 | |
| 285 | const regex = `^(?:${uniquePathnames.map(escapeRegExp).join("|")})$`; |
| 286 | |
| 287 | try { |
| 288 | const query = { |
| 289 | kind: "InsightVizNode", |
| 290 | source: { |
| 291 | kind: "TrendsQuery", |
| 292 | dateRange: { date_from: `-${days}d` }, |
| 293 | series: [ |
| 294 | { |
| 295 | kind: "EventsNode", |
| 296 | event: "$pageview", |
| 297 | custom_name: "Perspective pageviews", |
| 298 | properties: [ |
| 299 | { |
| 300 | key: "$pathname", |
| 301 | type: "event", |
| 302 | operator: "regex", |
| 303 | value: regex, |
| 304 | }, |
| 305 | ], |
| 306 | }, |
| 307 | ], |
| 308 | breakdownFilter: { |
| 309 | breakdown: "$pathname", |
| 310 | breakdown_type: "event", |
| 311 | breakdown_limit: uniquePathnames.length, |
| 312 | }, |
| 313 | trendsFilter: { display: "ActionsBarValue" }, |
| 314 | }, |
| 315 | }; |
| 316 | |
| 317 | const result = await queryPostHog(config, query); |
no test coverage detected