|
| 1 | +/** |
| 2 | + * Analytics for server-side events. |
| 3 | + * |
| 4 | + * @module analytics |
| 5 | + */ |
| 6 | +import { default as UserAgentBag } from "user-agent-bag"; |
| 7 | +import { PLAUSIBLE_ANALYTICS_DOMAIN, PLAUSIBLE_ANALYTICS_ENDPOINT } from "./config.js"; |
| 8 | +import { fetch } from "./fetch.js"; |
| 9 | + |
| 10 | + |
| 11 | +/** |
| 12 | + * Generate middleware to record an analytics event via a request to |
| 13 | + * Plausible's API. |
| 14 | + * |
| 15 | + * Intended for use in an Express routing chain. The generated middleware |
| 16 | + * function does not block on the analytics request before returning. |
| 17 | + * |
| 18 | + * If the User-Agent of the incoming request starts with "Nextstrain-CLI/", |
| 19 | + * then it is parsed for several bits of information and those are |
| 20 | + * automatically attached to the analytics event as custom properties. |
| 21 | + * |
| 22 | + * @function recordEvent |
| 23 | + * @param {object} data |
| 24 | + * @param {string} data.name - Event name. Defaults to "pageview" (Plausible's default). |
| 25 | + * @param {object} data.props - Custom properties. Defaults to {}. |
| 26 | + * @param {boolean} data.interactive - Interactive session or not? Defaults to |
| 27 | + * false (opposite of Plausible's default), with the reasoning that most events |
| 28 | + * we're likely to record server-side aren't for interactive browser-based |
| 29 | + * sessions. |
| 30 | + * @returns {expressMiddlewareAsync} |
| 31 | + */ |
| 32 | +export const recordEvent = ({name = "pageview", props = {}, interactive = false} = {}) => async (req, res, next) => { |
| 33 | + if (PLAUSIBLE_ANALYTICS_DOMAIN) { |
| 34 | + /* We intentionally do not "await fetch()" as we do not want to block |
| 35 | + * request processing on analytics; we'd rather ignore failures. |
| 36 | + * -trs, 28 April 2025 |
| 37 | + */ |
| 38 | + fetch( |
| 39 | + // <https://plausible.io/docs/events-api> |
| 40 | + PLAUSIBLE_ANALYTICS_ENDPOINT, { |
| 41 | + method: "POST", |
| 42 | + headers: { |
| 43 | + "User-Agent": req.header("User-Agent"), |
| 44 | + "X-Forwarded-For": req.ip, |
| 45 | + "Content-Type": "application/json", |
| 46 | + }, |
| 47 | + body: JSON.stringify({ |
| 48 | + name, |
| 49 | + domain: PLAUSIBLE_ANALYTICS_DOMAIN, |
| 50 | + url: new URL(req.originalUrl, req.context.origin), |
| 51 | + referrer: req.header("Referer"), |
| 52 | + interactive, |
| 53 | + props: { |
| 54 | + // <https://plausible.io/docs/custom-props/introduction> |
| 55 | + ...propsFromUserAgent(req.header("User-Agent")), |
| 56 | + ...props, |
| 57 | + }, |
| 58 | + }), |
| 59 | + } |
| 60 | + ); |
| 61 | + } |
| 62 | + return next(); |
| 63 | +}; |
| 64 | + |
| 65 | + |
| 66 | +const userAgentToPlausibleKeys = new Map([ |
| 67 | + ["Nextstrain-CLI", "nextstrain-cli/version"], |
| 68 | + ["Python", "nextstrain-cli/python"], |
| 69 | + ["installer", "nextstrain-cli/installer"], |
| 70 | + ["platform", "nextstrain-cli/platform"], |
| 71 | + ["tty", "nextstrain-cli/tty"], |
| 72 | +]); |
| 73 | + |
| 74 | +function propsFromUserAgent(ua) { |
| 75 | + if (ua && ua.match(/^Nextstrain-CLI\//)) { |
| 76 | + const uaComponents = new UserAgentBag(ua); |
| 77 | + |
| 78 | + return Object.fromEntries( |
| 79 | + Array.from(userAgentToPlausibleKeys) |
| 80 | + .map(([srcKey, dstKey]) => [dstKey, uaComponents.get(srcKey) ?? null]) |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + return {}; |
| 85 | +} |
0 commit comments