AgentSkillsCN

databuddy-core

借助 Databuddy 核心 SDK,实现浏览器端的跟踪工具、事件追踪以及全局追踪器的接入。在使用原生 JavaScript 实施分析功能,或需要直接调用核心追踪功能时,尤为适用。

SKILL.md
--- frontmatter
name: databuddy-core
description: Use the core Databuddy SDK for browser-side tracking utilities, event tracking, and global tracker access. Use when implementing analytics in vanilla JavaScript or when you need direct access to the core tracking functions.
metadata:
  author: databuddy
  version: "2.3"

Databuddy Core SDK

The core SDK (@databuddy/sdk) provides browser-side tracking utilities and types.

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 analytics in vanilla JavaScript applications
  • Need direct access to core tracking functions
  • Working with the global tracker (window.databuddy)
  • Setting up declarative tracking with data attributes
  • Configuring advanced tracking options and filters

Installation

bash
bun add @databuddy/sdk

Exports

typescript
import {
  detectClientId,
  createScript,
  isScriptInjected,
  // Tracker utilities
  track,
  trackError,
  flush,
  clear,
  getTracker,
  isTrackerAvailable,
  getAnonymousId,
  getSessionId,
  getTrackingIds,
  getTrackingParams,
} from "@databuddy/sdk";

Configuration

DatabuddyConfig

OptionTypeDefaultDescription
clientIdstringAuto-detectProject client ID (auto-detects from NEXT_PUBLIC_DATABUDDY_CLIENT_ID)
clientSecretstringServer-side only secret
apiUrlstringhttps://basket.databuddy.ccCustom API endpoint
scriptUrlstringhttps://cdn.databuddy.cc/databuddy.jsCustom script URL
disabledbooleanfalseDisable all tracking
debugbooleanfalseEnable debug logging

Tracking Options

OptionTypeDefaultDescription
trackHashChangesbooleanfalseTrack URL hash changes
trackAttributesbooleanfalseTrack data-* attributes
trackOutgoingLinksbooleanfalseTrack outgoing link clicks
trackInteractionsbooleanfalseTrack user interactions
trackScrollDepthbooleanfalseTrack scroll depth
trackPerformancebooleantrueTrack performance metrics
trackWebVitalsbooleanfalseTrack Web Vitals
trackErrorsbooleanfalseTrack JavaScript errors

Optimization Options

OptionTypeDefaultDescription
samplingRatenumber1.0Sampling rate (0.0-1.0)
enableRetriesbooleanfalseRetry failed requests
maxRetriesnumber3Max retry attempts
initialRetryDelaynumber500Initial retry delay (ms)
enableBatchingbooleantrueEnable event batching
batchSizenumber10Events per batch (1-50)
batchTimeoutnumber2000Batch timeout (ms)
ignoreBotDetectionbooleanfalseTrack bots
usePixelbooleanfalseUse pixel tracking

Filtering Options

OptionTypeDescription
filter(event) => booleanFilter function to skip events
skipPatternsstring[]Glob patterns to skip tracking
maskPatternsstring[]Glob patterns to mask paths

Global Tracker

The tracker is available at window.databuddy or window.db:

typescript
// Track custom event
window.databuddy.track("signup", { plan: "pro" });

// Manual page view
window.databuddy.screenView({ section: "pricing" });

// Set global properties
window.databuddy.setGlobalProperties({
  plan: "enterprise",
  abVariant: "checkout-v2",
});

// Clear session (call on logout)
window.databuddy.clear();

// Force flush queued events
window.databuddy.flush();

Event Types

Pre-defined Events

EventProperties
screen_viewtime_on_page, scroll_depth, interaction_count, is_bounce
page_exittime_on_page, scroll_depth, interaction_count, page_count, is_bounce
button_clickbutton_text, button_type, button_id, element_class
link_outhref, text, target_domain
form_submitform_id, form_name, form_type, success
web_vitalsfcp, lcp, cls, fid, ttfb, load_time
errormessage, filename, lineno, colno, stack

Base Event Properties

All events include these base properties:

  • __path - Page URL
  • __title - Page title
  • __referrer - Referrer URL
  • __timestamp_ms - Timestamp
  • sessionId - Session ID
  • viewport_size - Viewport dimensions
  • timezone - User timezone
  • language - User language
  • UTM parameters (utm_source, utm_medium, etc.)

Declarative Tracking

Use data attributes for click tracking without JavaScript:

html
<button
  data-track="cta_clicked"
  data-button-text="Get Started"
  data-location="hero"
>
  Get Started
</button>

Properties are auto-converted from kebab-case to camelCase: { buttonText: "Get Started", location: "hero" }

SDK Functions

track

typescript
import { track } from "@databuddy/sdk";

await track("purchase", {
  product_id: "sku-123",
  amount: 99.99,
});

flush

Force send all queued events:

typescript
import { flush } from "@databuddy/sdk";

await flush();

clear

Reset the session (generates new IDs):

typescript
import { clear } from "@databuddy/sdk";

clear();

getTrackingIds

Get current anonymous and session IDs:

typescript
import { getTrackingIds } from "@databuddy/sdk";

const { anonymousId, sessionId } = getTrackingIds();