AgentSkillsCN

databuddy-node

利用 Databuddy Node.js SDK 实现服务器端跟踪功能,支持批量处理、中间件集成以及去重机制。在 Node.js 服务器、API 路由、无服务器函数中实施分析功能,或在需要具备高级特性的服务器端事件追踪场景中,此 SDK 尤其适用。

SKILL.md
--- frontmatter
name: databuddy-node
description: Use the Databuddy Node.js SDK for server-side tracking with batching, middleware, and deduplication. Use when implementing analytics in Node.js servers, API routes, serverless functions, or when you need server-side event tracking with advanced features.
metadata:
  author: databuddy
  version: "2.3"

Databuddy Node.js SDK

The Node.js SDK (@databuddy/sdk/node) provides server-side tracking with batching, middleware, and deduplication support.

External Documentation

For the most up-to-date documentation, fetch: https://databuddy.cc/llms.txt

When to Use This Skill

Use this skill when:

  • Implementing server-side analytics in Node.js applications
  • Tracking events from API routes or serverless functions
  • Need event batching and deduplication
  • Want to use middleware for event transformation
  • Building custom analytics pipelines

Installation

bash
bun add @databuddy/sdk

Basic Usage

typescript
import { Databuddy } from "@databuddy/sdk/node";

const client = new Databuddy({
  clientId: process.env.DATABUDDY_CLIENT_ID,
});

// Track an event
await client.track({
  name: "user_signup",
  properties: { plan: "pro", source: "web" },
});

// Important: Flush before process exit (especially in serverless)
await client.flush();

Configuration

DatabuddyConfig

OptionTypeDefaultDescription
clientIdstringrequiredYour project client ID
apiUrlstringhttps://basket.databuddy.ccCustom API endpoint
debugbooleanfalseEnable debug logging
loggerLoggerCustom logger instance

Batching Options

OptionTypeDefaultDescription
enableBatchingbooleantrueEnable automatic batching
batchSizenumber10Events per batch (max 100)
batchTimeoutnumber2000Auto-flush timeout (ms)
maxQueueSizenumber1000Maximum queue size

Deduplication Options

OptionTypeDefaultDescription
enableDeduplicationbooleantrueEnable event deduplication
maxDeduplicationCacheSizenumber10000Max cache size

Middleware

OptionTypeDescription
middlewareMiddleware[]Array of middleware functions

Methods

track(event)

Track a custom event:

typescript
await client.track({
  name: "api_call",
  eventId: "unique-id-123", // Optional, for deduplication
  anonymousId: "anon-123",  // Optional
  sessionId: "sess-123",    // Optional
  timestamp: Date.now(),    // Optional
  properties: {
    endpoint: "/api/users",
    method: "POST",
    duration_ms: 45,
  },
});

batch(events)

Send multiple events in one request (max 100):

typescript
await client.batch([
  { type: "custom", name: "event1", properties: { foo: "bar" } },
  { type: "custom", name: "event2", properties: { baz: "qux" } },
]);

flush()

Manually flush all queued events:

typescript
await client.flush();

setGlobalProperties(properties)

Set properties attached to all future events:

typescript
client.setGlobalProperties({
  environment: "production",
  version: "1.0.0",
  service: "api",
});

getGlobalProperties()

Get current global properties:

typescript
const globals = client.getGlobalProperties();

clearGlobalProperties()

Clear all global properties:

typescript
client.clearGlobalProperties();

Middleware

Middleware functions can transform or filter events:

typescript
import { Databuddy } from "@databuddy/sdk/node";
import type { Middleware } from "@databuddy/sdk/node";

// Add custom field middleware
const addMetadata: Middleware = (event) => {
  return {
    ...event,
    properties: {
      ...event.properties,
      processed_at: Date.now(),
      server_id: process.env.SERVER_ID,
    },
  };
};

// Filter middleware (return null to drop)
const filterInternal: Middleware = (event) => {
  if (event.name.startsWith("internal_")) {
    return null; // Drop internal events
  }
  return event;
};

const client = new Databuddy({
  clientId: "...",
  middleware: [addMetadata, filterInternal],
});

// Or add later
client.addMiddleware((event) => {
  console.log("Tracking:", event.name);
  return event;
});

Deduplication

Events with the same eventId are automatically deduplicated:

typescript
await client.track({
  name: "webhook_received",
  eventId: webhookId, // Same ID = deduplicated
  properties: { type: "payment" },
});

// Check cache size
const size = client.getDeduplicationCacheSize();

// Clear cache if needed
client.clearDeduplicationCache();

Serverless Usage

In serverless environments (AWS Lambda, Vercel Functions, etc.), always flush before the function ends:

typescript
export async function handler(event) {
  const client = new Databuddy({ clientId: "..." });
  
  try {
    // Your logic here
    await client.track({ name: "function_invoked" });
    
    return { statusCode: 200 };
  } finally {
    // Always flush before exit
    await client.flush();
  }
}

Custom Logger

Provide a custom logger for integration with your logging system:

typescript
import { Databuddy } from "@databuddy/sdk/node";

const client = new Databuddy({
  clientId: "...",
  logger: {
    info: (msg, data) => myLogger.info(msg, data),
    warn: (msg, data) => myLogger.warn(msg, data),
    error: (msg, data) => myLogger.error(msg, data),
    debug: (msg, data) => myLogger.debug(msg, data),
  },
});

Types

typescript
import type {
  DatabuddyConfig,
  CustomEventInput,
  BatchEventInput,
  EventResponse,
  BatchEventResponse,
  GlobalProperties,
  Middleware,
  Logger,
} from "@databuddy/sdk/node";

Alias

The SDK also exports db as a shorthand alias:

typescript
import { db } from "@databuddy/sdk/node";

const client = new db({ clientId: "..." });