pgp-sdk
v0.1.1
Published
Isomorphic (Node.js + browser) event ingestion SDK for the PGP platform
Readme
pgp-sdk (TypeScript)
Isomorphic (Node.js + browser) event ingestion SDK for the PGP platform.
Install
npm i pgp-sdkNode.js
import { PgpClient } from "pgp-sdk";
const pgp = new PgpClient({ apiKey: "pgp_...", baseUrl: "https://pgp.int.wepromotion.cn" });
pgp.capture("signup", { plan: "free" });
pgp.setUserId("user-123");
pgp.capture("purchase", { amount: 99 });
await pgp.close(); // flush on shutdownBrowser
With a bundler (vite/webpack), identical to Node:
import { PgpClient } from "pgp-sdk";
const pgp = new PgpClient({ apiKey: "pgp_..." });
pgp.capture("page_view");No bundler — script tag via CDN, exposes a PgpSDK global:
<script src="https://unpkg.com/pgp-sdk/dist/index.global.js"></script>
<script>
const pgp = new PgpSDK.PgpClient({ apiKey: "pgp_..." });
pgp.capture("page_view");
</script>With no userId, an anonymous id is generated and persisted in localStorage.
Queued events are flushed with keepalive on pagehide, so nothing is lost on navigation.
Behavior
- Events queue in memory, flush at
flushAt(default 20) events, everyflushIntervalms (default 5000), or onflush()/close(). - Failed requests re-queue (events are never dropped on network errors) and retry on the next flush.
- Concurrent
flush()calls coalesce into one request.
Options
| Option | Default | Description |
|---|---|---|
| apiKey | — | required, from POST /api/projects |
| baseUrl | https://pgp.int.wepromotion.cn | server base URL |
| userId | anonymous id | user_id sent with each event |
| flushAt | 20 | queue size that triggers a flush |
| flushInterval | 5000 | ms between flushes |
| fetchFn | global fetch | injectable for tests |
Dev
npm test # node:test, no server needed
npm run build # dist/ ESM + CJS + types