@fourierhq/sdk

中文文档
0.2.1发布于 2026/09/25MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 3034

Open source product analytics SDK. identify/track/page, analytics.js-shaped, with first-class Next.js support.

原始使用文档
文档目录5 节
  1. Next.js (App Router)
  2. Anywhere in the browser
  3. Server (API routes, server actions, cron)
  4. Several sites, one identity
  5. Companies / workspaces

@fourierhq/sdk

Open source product analytics SDK. Drop-in compatible with Segment's analytics.js API: identify, track, page, group, alias, reset, user(), ready(), on(), trackLink, trackForm, source middleware and the same ajs_* cookies, so an existing Segment install keeps its anonymous ids.

It talks to a Fourier server you run yourself — self-hosted or on Vercel, with ClickHouse underneath. The SDK is MIT; the server is AGPL-3.0.

pnpm add @fourierhq/sdk

Next.js (App Router)

// app/layout.tsx
import { FourierProvider } from "@fourierhq/sdk/next";

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <FourierProvider writeKey={process.env.NEXT_PUBLIC_FOURIER_WRITE_KEY!} host={process.env.NEXT_PUBLIC_FOURIER_HOST!}>
          {children}
        </FourierProvider>
      </body>
    </html>
  );
}
"use client";
import { useFourier } from "@fourierhq/sdk/next";

export function UpgradeButton() {
  const fourier = useFourier();
  return <button onClick={() => fourier.track("Upgrade Clicked", { plan: "pro" })}>Upgrade</button>;
}

Page views are tracked automatically on route changes.

Anywhere in the browser

import fourier from "@fourierhq/sdk";
fourier.init({ writeKey: "...", host: "https://analytics.example.com" });
fourier.identify("user_123", { email: "[email protected]", plan: "pro" });
fourier.group("acme", { name: "Acme Inc", plan: "enterprise" });
fourier.track("Report Exported", { format: "csv" });

Server (API routes, server actions, cron)

import { FourierServer } from "@fourierhq/sdk/server";
const fourier = new FourierServer({ writeKey: process.env.FOURIER_WRITE_KEY!, host: process.env.FOURIER_HOST! });
fourier.track({ userId: "user_123", event: "Invoice Paid", properties: { amount: 4900 } });
await fourier.closeAndFlush();

Several sites, one identity

fourier.init({
  writeKey,                       // this site's own source key from the dashboard
  host,
  cookieDomain: ".example.com",   // subdomains share the anonymous id via the cookie
  crossDomain: ["example.io"],    // links to other domains carry it as ?ajs_aid=… (and ?ajs_uid=… when identified)
});
fourier.decorateUrl("https://app.example.io/signup"); // for URLs you build yourself
fourier.newSession();                                  // force a new session (attribution touch)

Companies / workspaces

group(groupId, traits) registers a company and links the current user to it. Every subsequent event carries context.groupId, so the dashboard and the query API can roll users and events up by company.

版本与发布记录

@fourierhq/sdk 共发布 3 个版本,首次发布于 2026/09/15;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.1latest2026/09/251 个月前
0.2.02026/09/181 个月前
0.1.02026/09/152 个月前

关于 @fourierhq/sdk 的常见问题

@fourierhq/sdk 是什么?

Open source product analytics SDK. identify/track/page, analytics.js-shaped, with first-class Next.js support.

如何安装 @fourierhq/sdk?

在项目目录运行 npm install @fourierhq/sdk,或使用 pnpm add / yarn add / bun add。

@fourierhq/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fourierhq/sdk 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fourierhq/sdk 可以商用吗?

@fourierhq/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fourierhq/sdk 的下载量有多少?

根据 npm 官方下载统计,@fourierhq/sdk 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,034 次,日均约 101 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fourierhq/sdk 还在维护吗?

最新版本 0.2.1 发布于 2026/09/25(1 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/09/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。