Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Update Node.js SDK docs

Edward Cui6b9bd4ca6af0
diff --git a/docs/src/content/sdks/node.mdx b/docs/src/content/sdks/node.mdx
index 68826dd8d..45c591d14 100644
--- a/docs/src/content/sdks/node.mdx
+++ b/docs/src/content/sdks/node.mdx
@@ -30,12 +30,12 @@ import { Rybbit } from "@rybbit/node";
const rybbit = new Rybbit({
analyticsHost: "https://api.rybbit.io/api",
siteId: "1",
- origin: "https://rybbit.io",
+ apiKey: "your-secret-api-key",
});
```
<Callout type="warning">
-The `Rybbit` constructor will throw an error if required configuration options (`analyticsHost`, `siteId`, `origin`) are missing or invalid.
+The `Rybbit` constructor will throw an error if required configuration options (`analyticsHost`, `siteId`, `apiKey`) are missing or invalid.
</Callout>
### Configuration Options
@@ -44,9 +44,8 @@ The `Rybbit` constructor will throw an error if required configuration options (
| :-------------- | :----------------- | :------ | :----------------------------------------------------------------------------------------------- |
| `analyticsHost` | `string` | - | **Required.** URL of your Rybbit analytics instance (e.g., `https://rybbit.yourdomain.com/api`). |
| `siteId` | `string \| number` | - | **Required.** The Site ID for your website obtained from your Rybbit instance. |
-| `origin` | `string` | - | **Required.** The domain of your website. |
-| `userAgent` | `string` | `""` | A custom User-Agent string to send with tracking requests. |
-| `debug` | `boolean` | `false` | If `true`, enables detailed logging to the console. |
+| `apiKey` | `string` | - | **Required.** Your secret API key for the site |
+| `debug` | `boolean` | `false` | Enable detailed logging to the console. |
## Tracking Pageviews
@@ -69,6 +68,9 @@ await rybbit.pageview(payload?: Payload);
- `language` (`string`): Language code.
- `page_title` (`string`): The title of the page or resource.
- `referrer` (`string`): The referrer URL.
+ - `user_id` (`string`): A unique identifier for the user.
+ - `user_agent` (`string`): The user agent string of the client.
+ - `ip_address` (`string`): The IP address of the client.
<Callout type="info">
The fields `screenWidth`, `screenHeight`, `language`, `page_title`, and `referrer` are typically browser-derived. When using the Node.js SDK, you would only provide these if they are relevant and available in your server-side context. For many server-side pageview events, `hostname` and `pathname` might be the most relevant.
@@ -84,7 +86,8 @@ async function handleProductPageRequest(req, res) {
await rybbit.pageview({
hostname: req.hostname,
pathname: `/products/${req.params.productId}`,
- page_title: `Product - ${product.name}`
+ page_title: `Product - ${product.name}`,
+ ip_address: req.ip
});
// ... send response ...
@@ -94,7 +97,9 @@ async function handleProductPageRequest(req, res) {
async function serveApiResource(req) {
await rybbit.pageview({
hostname: "api.myservice.com",
- pathname: `/v1/items/${req.params.itemId}`
+ pathname: `/v1/items/${req.params.itemId}`,
+ user_id: req.user.id,
+ ip_address: req.ip
});
// ... return API data ...
}
@@ -122,7 +127,10 @@ await rybbit.event(eventName: string, payload?: Payload, properties?: TrackPrope
- `language` (`string`): Language code.
- `page_title` (`string`): The title of the page or resource context.
- `referrer` (`string`): The referrer URL.
-- **`properties`** (`object`, Optional): An object containing additional key-value pairs related to the event. Values should ideally be strings, numbers, or booleans. Keep the structure flat.
+ - `user_id` (`string`): A unique identifier for the user.
+ - `user_agent` (`string`): The user agent string of the client.
+ - `ip_address` (`string`): The IP address of the client.
+- **`properties`** (`object`, Optional): An object containing additional key-value pairs related to the event. Values can be a `string`, `number`, `boolean`, or an array of these types.
<Callout type="info">
The `payload` fields (`hostname`, `pathname`, etc.) provide general context for the event, similar to a pageview. The `properties` object is for data specific to the custom `eventName` itself (e.g., for an `"order_processed"` event, properties might include `orderId: 12345` and `totalAmount: 99.99`).
@@ -132,12 +140,19 @@ The `payload` fields (`hostname`, `pathname`, etc.) provide general context for
```javascript
// Track a new user registration
-async function registerUser(userData) {
+async function registerUser(userData, req) {
// ... process user registration ...
await rybbit.event("user_registered",
- { hostname: "auth.myservice.com", pathname: "/register" },
- { userId: newUserId, registrationMethod: "email" }
+ {
+ hostname: "auth.myservice.com",
+ pathname: "/register",
+ ip_address: req.ip
+ },
+ {
+ userId: newUserId,
+ registrationMethod: "email"
+ }
);
}
@@ -147,7 +162,11 @@ async function processDailyReport(reportId) {
await rybbit.event("report_generated",
{},
- { reportId: reportId, status: "success", processingTimeMs: 12345 }
+ {
+ reportId: reportId,
+ status: "success",
+ processingTimeMs: 12345
+ }
);
}
@@ -156,8 +175,16 @@ async function handleApiRequest(req) {
// ... handle request ...
await rybbit.event(
"api_v1_item_lookup",
- { hostname: req.hostname, pathname: req.path },
- { itemId: req.params.itemId, clientVersion: req.headers["x-client-version"] }
+ {
+ hostname: req.hostname,
+ pathname: req.path,
+ user_id: req.user.id,
+ ip_address: req.ip
+ },
+ {
+ itemId: req.params.itemId,
+ clientVersion: req.headers["x-client-version"]
+ }
);
// ... return response ...
}