schematic-js
is a client-side JavaScript SDK for tracking event-based usage, identifying users, and checking flags using Schematic.
npm install @schematichq/schematic-js
# or
yarn add @schematichq/schematic-js
# or
pnpm add @schematichq/schematic-js
You can use Schematic to identify users; after this, your subsequent track events and flag checks will be associated with this user.
A number of these examples use keys
to identify companies and users. Learn more about keys here.
import { Schematic } from "@schematichq/schematic-js";
const schematic = new Schematic("your-api-key");
// Send an identify event
schematic.identify({
keys: {
id: "my-user-id",
},
traits: {
anykey: "anyval",
},
company: {
name: "My Company",
keys: {
id: "my-company-id",
},
traits: {
location: "Atlanta, GA",
},
},
});
// Send a track event to record usage
schematic.track({ event: "query" });
// OR, Send a track event with a quantity to record multiple units of usage
schematic.track({ event: "query", quantity: 10 });
// Check a flag
await schematic.checkFlag({ key: "some-flag-key" });
By default, checkFlag
will perform a network request to get the flag value for this user. If you'd like to check all flags at once in order to minimize network requests, you can use checkFlags
:
import { Schematic } from "@schematichq/schematic-js";
const schematic = new Schematic("your-api-key");
schematic.identify({
keys: { id: "my-user-id" },
company: {
keys: { id: "my-company-id" },
},
});
await schematic.checkFlags();
Alternatively, you can run in websocket mode, which will keep a persistent connection open to the Schematic service and receive flag updates in real time:
import { Schematic } from "@schematichq/schematic-js";
const schematic = new Schematic("your-api-key", { useWebSocket: true });
schematic.identify({
keys: { id: "my-user-id" },
company: { keys: { id: "my-company-id" } },
});
await schematic.checkFlag("some-flag-key");
// Close the connection when you're done with the Schematic client
schematic.cleanup();
For debugging and development, Schematic supports two special modes:
Enables console logging of all Schematic operations:
// Enable at initialization
const schematic = new Schematic("your-api-key", { debug: true });
// Or via URL parameter
// https://yoursite.com/?schematic_debug=true
Prevents network requests and returns fallback values for all flag checks:
// Enable at initialization
const schematic = new Schematic("your-api-key", { offline: true });
// Or via URL parameter
// https://yoursite.com/?schematic_offline=true
Offline mode automatically enables debug mode to help with troubleshooting.
MIT
Need help? Please open a GitHub issue or reach out to support@schematichq.com and we'll be happy to assist.