@databuddy/sdk 2.2.0 → 2.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai/vercel/index.d.mts +12 -0
- package/dist/ai/vercel/index.d.ts +12 -0
- package/dist/ai/vercel/index.mjs +43 -0
- package/dist/core/index.d.mts +27 -10
- package/dist/core/index.d.ts +27 -10
- package/dist/core/index.mjs +31 -9
- package/dist/node/index.d.mts +144 -39
- package/dist/node/index.d.ts +144 -39
- package/dist/node/index.mjs +212 -38
- package/dist/react/index.d.mts +18 -5
- package/dist/react/index.d.ts +18 -5
- package/dist/react/index.mjs +20 -3
- package/dist/shared/@databuddy/{sdk.C8T93n5r.d.mts → sdk.B1NyVg8b.d.mts} +26 -81
- package/dist/shared/@databuddy/{sdk.C8T93n5r.d.ts → sdk.B1NyVg8b.d.ts} +26 -81
- package/dist/shared/@databuddy/{sdk.aVQee-4k.mjs → sdk.BUsPV0LH.mjs} +1 -20
- package/dist/shared/@databuddy/{sdk.tFAHtL2M.mjs → sdk.DihibvA_.mjs} +1 -1
- package/dist/shared/@databuddy/{sdk.YfiY9DoZ.d.mts → sdk.OK9Nbqlf.d.mts} +3 -3
- package/dist/shared/@databuddy/{sdk.YfiY9DoZ.d.ts → sdk.OK9Nbqlf.d.ts} +3 -3
- package/dist/vue/index.d.mts +56 -3
- package/dist/vue/index.d.ts +56 -3
- package/dist/vue/index.mjs +3 -7
- package/package.json +35 -7
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { LanguageModelV2 } from '@ai-sdk/provider';
|
|
2
|
+
import { db as Databuddy } from '../../node/index.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Wrap a Vercel language model with Databuddy middleware
|
|
6
|
+
* @param model - The Vercel language model to wrap, if you are using the Vercel AI Gateway, please use the LanguageModelV2 type e.g. gateway('xai/grok-3') instead of the string type e.g. 'xai/grok-3'
|
|
7
|
+
* @param buddy - The Databuddy instance to use
|
|
8
|
+
* @returns The wrapped language model, can be used in e.g. generateText from the ai package
|
|
9
|
+
*/
|
|
10
|
+
declare const wrapVercelLanguageModel: (model: LanguageModelV2, buddy: Databuddy) => LanguageModelV2;
|
|
11
|
+
|
|
12
|
+
export { wrapVercelLanguageModel as withBuddy };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { LanguageModelV2 } from '@ai-sdk/provider';
|
|
2
|
+
import { db as Databuddy } from '../../node/index.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Wrap a Vercel language model with Databuddy middleware
|
|
6
|
+
* @param model - The Vercel language model to wrap, if you are using the Vercel AI Gateway, please use the LanguageModelV2 type e.g. gateway('xai/grok-3') instead of the string type e.g. 'xai/grok-3'
|
|
7
|
+
* @param buddy - The Databuddy instance to use
|
|
8
|
+
* @returns The wrapped language model, can be used in e.g. generateText from the ai package
|
|
9
|
+
*/
|
|
10
|
+
declare const wrapVercelLanguageModel: (model: LanguageModelV2, buddy: Databuddy) => LanguageModelV2;
|
|
11
|
+
|
|
12
|
+
export { wrapVercelLanguageModel as withBuddy };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { wrapLanguageModel } from 'ai';
|
|
2
|
+
import { computeCostUSD } from 'tokenlens';
|
|
3
|
+
|
|
4
|
+
const buddyWare = (buddy) => {
|
|
5
|
+
return {
|
|
6
|
+
wrapGenerate: async ({ doGenerate, model }) => {
|
|
7
|
+
const result = await doGenerate();
|
|
8
|
+
const isToolCall = (part) => part.type === "tool-call";
|
|
9
|
+
const isToolResult = (part) => part.type === "tool-result";
|
|
10
|
+
const toolCalls = result.content.filter(isToolCall);
|
|
11
|
+
const toolResults = result.content.filter(isToolResult);
|
|
12
|
+
const toolCallNames = Array.from(
|
|
13
|
+
new Set(toolCalls.map((c) => c.toolName))
|
|
14
|
+
);
|
|
15
|
+
const costs = await computeCostUSD({
|
|
16
|
+
modelId: model.modelId,
|
|
17
|
+
provider: model.provider,
|
|
18
|
+
usage: result.usage
|
|
19
|
+
});
|
|
20
|
+
const payload = {
|
|
21
|
+
inputTokens: result.usage.inputTokens,
|
|
22
|
+
outputTokens: result.usage.outputTokens,
|
|
23
|
+
totalTokens: result.usage.totalTokens,
|
|
24
|
+
cachedInputTokens: result.usage.cachedInputTokens,
|
|
25
|
+
finishReason: result.finishReason,
|
|
26
|
+
toolCallCount: toolCalls.length,
|
|
27
|
+
toolResultCount: toolResults.length,
|
|
28
|
+
inputTokenCostUSD: costs.inputTokenCostUSD,
|
|
29
|
+
outputTokenCostUSD: costs.outputTokenCostUSD,
|
|
30
|
+
totalTokenCostUSD: costs.totalTokenCostUSD,
|
|
31
|
+
toolCallNames
|
|
32
|
+
};
|
|
33
|
+
console.log("payload", payload);
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
const wrapVercelLanguageModel = (model, buddy) => wrapLanguageModel({
|
|
39
|
+
model,
|
|
40
|
+
middleware: buddyWare()
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export { wrapVercelLanguageModel as withBuddy };
|
package/dist/core/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { S as StorageInterface, F as FlagsManager, a as FlagsManagerOptions, b as FlagResult, c as FlagState, d as FlagsConfig } from '../shared/@databuddy/sdk.
|
|
2
|
-
export { e as FlagsContext } from '../shared/@databuddy/sdk.
|
|
3
|
-
import { D as DatabuddyConfig, a as DatabuddyTracker, T as TrackFunction
|
|
4
|
-
export { B as BaseEventProperties,
|
|
1
|
+
import { S as StorageInterface, F as FlagsManager, a as FlagsManagerOptions, b as FlagResult, c as FlagState, d as FlagsConfig } from '../shared/@databuddy/sdk.OK9Nbqlf.mjs';
|
|
2
|
+
export { e as FlagsContext } from '../shared/@databuddy/sdk.OK9Nbqlf.mjs';
|
|
3
|
+
import { D as DatabuddyConfig, a as DatabuddyTracker, T as TrackFunction } from '../shared/@databuddy/sdk.B1NyVg8b.mjs';
|
|
4
|
+
export { B as BaseEventProperties, d as DataAttributes, c as EventName, E as EventProperties, b as EventTypeMap, P as PropertiesForEvent, S as ScreenViewFunction, e as SetGlobalPropertiesFunction } from '../shared/@databuddy/sdk.B1NyVg8b.mjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Auto-detect Databuddy client ID from environment variables
|
|
@@ -40,7 +40,7 @@ declare class CoreFlagsManager implements FlagsManager {
|
|
|
40
40
|
private fetchFlag;
|
|
41
41
|
isEnabled(key: string): FlagState;
|
|
42
42
|
refresh(forceClear?: boolean): void;
|
|
43
|
-
updateUser(user: FlagsConfig[
|
|
43
|
+
updateUser(user: FlagsConfig["user"]): void;
|
|
44
44
|
updateConfig(config: FlagsConfig): void;
|
|
45
45
|
getMemoryFlags(): Record<string, FlagResult>;
|
|
46
46
|
getPendingFlags(): Set<string>;
|
|
@@ -84,12 +84,29 @@ declare function trackError(message: string, properties?: {
|
|
|
84
84
|
colno?: number;
|
|
85
85
|
stack?: string;
|
|
86
86
|
error_type?: string;
|
|
87
|
-
[key: string]:
|
|
87
|
+
[key: string]: string | number | boolean | null | undefined;
|
|
88
88
|
}): Promise<void>;
|
|
89
|
-
|
|
90
89
|
/**
|
|
91
|
-
*
|
|
90
|
+
* Get anonymous ID from multiple sources
|
|
91
|
+
* Priority: URL params > tracker instance > localStorage
|
|
92
|
+
*/
|
|
93
|
+
declare function getAnonymousId(urlParams?: URLSearchParams): string | null;
|
|
94
|
+
/**
|
|
95
|
+
* Get session ID from multiple sources
|
|
96
|
+
* Priority: URL params > tracker instance > sessionStorage
|
|
97
|
+
*/
|
|
98
|
+
declare function getSessionId(urlParams?: URLSearchParams): string | null;
|
|
99
|
+
/**
|
|
100
|
+
* Get tracking IDs (anonymous ID and session ID) from multiple sources
|
|
101
|
+
* Priority: URL params > tracker instance > localStorage/sessionStorage
|
|
102
|
+
*/
|
|
103
|
+
declare function getTrackingIds(urlParams?: URLSearchParams): {
|
|
104
|
+
anonId: string | null;
|
|
105
|
+
sessionId: string | null;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Get tracking IDs as URL search params string
|
|
92
109
|
*/
|
|
93
|
-
declare
|
|
110
|
+
declare function getTrackingParams(urlParams?: URLSearchParams): string;
|
|
94
111
|
|
|
95
|
-
export { BrowserFlagStorage, CoreFlagsManager,
|
|
112
|
+
export { BrowserFlagStorage, CoreFlagsManager, DatabuddyConfig, DatabuddyTracker, FlagResult, FlagState, FlagsConfig, FlagsManager, FlagsManagerOptions, StorageInterface, TrackFunction, clear, createScript, detectClientId, flush, getAnonymousId, getSessionId, getTracker, getTrackingIds, getTrackingParams, isScriptInjected, isTrackerAvailable, track, trackError };
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { S as StorageInterface, F as FlagsManager, a as FlagsManagerOptions, b as FlagResult, c as FlagState, d as FlagsConfig } from '../shared/@databuddy/sdk.
|
|
2
|
-
export { e as FlagsContext } from '../shared/@databuddy/sdk.
|
|
3
|
-
import { D as DatabuddyConfig, a as DatabuddyTracker, T as TrackFunction
|
|
4
|
-
export { B as BaseEventProperties,
|
|
1
|
+
import { S as StorageInterface, F as FlagsManager, a as FlagsManagerOptions, b as FlagResult, c as FlagState, d as FlagsConfig } from '../shared/@databuddy/sdk.OK9Nbqlf.js';
|
|
2
|
+
export { e as FlagsContext } from '../shared/@databuddy/sdk.OK9Nbqlf.js';
|
|
3
|
+
import { D as DatabuddyConfig, a as DatabuddyTracker, T as TrackFunction } from '../shared/@databuddy/sdk.B1NyVg8b.js';
|
|
4
|
+
export { B as BaseEventProperties, d as DataAttributes, c as EventName, E as EventProperties, b as EventTypeMap, P as PropertiesForEvent, S as ScreenViewFunction, e as SetGlobalPropertiesFunction } from '../shared/@databuddy/sdk.B1NyVg8b.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Auto-detect Databuddy client ID from environment variables
|
|
@@ -40,7 +40,7 @@ declare class CoreFlagsManager implements FlagsManager {
|
|
|
40
40
|
private fetchFlag;
|
|
41
41
|
isEnabled(key: string): FlagState;
|
|
42
42
|
refresh(forceClear?: boolean): void;
|
|
43
|
-
updateUser(user: FlagsConfig[
|
|
43
|
+
updateUser(user: FlagsConfig["user"]): void;
|
|
44
44
|
updateConfig(config: FlagsConfig): void;
|
|
45
45
|
getMemoryFlags(): Record<string, FlagResult>;
|
|
46
46
|
getPendingFlags(): Set<string>;
|
|
@@ -84,12 +84,29 @@ declare function trackError(message: string, properties?: {
|
|
|
84
84
|
colno?: number;
|
|
85
85
|
stack?: string;
|
|
86
86
|
error_type?: string;
|
|
87
|
-
[key: string]:
|
|
87
|
+
[key: string]: string | number | boolean | null | undefined;
|
|
88
88
|
}): Promise<void>;
|
|
89
|
-
|
|
90
89
|
/**
|
|
91
|
-
*
|
|
90
|
+
* Get anonymous ID from multiple sources
|
|
91
|
+
* Priority: URL params > tracker instance > localStorage
|
|
92
|
+
*/
|
|
93
|
+
declare function getAnonymousId(urlParams?: URLSearchParams): string | null;
|
|
94
|
+
/**
|
|
95
|
+
* Get session ID from multiple sources
|
|
96
|
+
* Priority: URL params > tracker instance > sessionStorage
|
|
97
|
+
*/
|
|
98
|
+
declare function getSessionId(urlParams?: URLSearchParams): string | null;
|
|
99
|
+
/**
|
|
100
|
+
* Get tracking IDs (anonymous ID and session ID) from multiple sources
|
|
101
|
+
* Priority: URL params > tracker instance > localStorage/sessionStorage
|
|
102
|
+
*/
|
|
103
|
+
declare function getTrackingIds(urlParams?: URLSearchParams): {
|
|
104
|
+
anonId: string | null;
|
|
105
|
+
sessionId: string | null;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Get tracking IDs as URL search params string
|
|
92
109
|
*/
|
|
93
|
-
declare
|
|
110
|
+
declare function getTrackingParams(urlParams?: URLSearchParams): string;
|
|
94
111
|
|
|
95
|
-
export { BrowserFlagStorage, CoreFlagsManager,
|
|
112
|
+
export { BrowserFlagStorage, CoreFlagsManager, DatabuddyConfig, DatabuddyTracker, FlagResult, FlagState, FlagsConfig, FlagsManager, FlagsManagerOptions, StorageInterface, TrackFunction, clear, createScript, detectClientId, flush, getAnonymousId, getSessionId, getTracker, getTrackingIds, getTrackingParams, isScriptInjected, isTrackerAvailable, track, trackError };
|
package/dist/core/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
export { B as BrowserFlagStorage, C as CoreFlagsManager, c as createScript, i as isScriptInjected } from '../shared/@databuddy/sdk.tFAHtL2M.mjs';
|
|
1
|
+
export { d as detectClientId } from '../shared/@databuddy/sdk.BUsPV0LH.mjs';
|
|
2
|
+
export { B as BrowserFlagStorage, C as CoreFlagsManager, c as createScript, i as isScriptInjected } from '../shared/@databuddy/sdk.DihibvA_.mjs';
|
|
4
3
|
|
|
5
4
|
function isTrackerAvailable() {
|
|
6
5
|
return typeof window !== "undefined" && (!!window.databuddy || !!window.db);
|
|
@@ -21,7 +20,8 @@ const track = async (eventName, properties) => {
|
|
|
21
20
|
}
|
|
22
21
|
try {
|
|
23
22
|
await tracker(eventName, properties);
|
|
24
|
-
} catch (
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.error("Databuddy tracking error:", error);
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
27
|
function clear() {
|
|
@@ -34,7 +34,8 @@ function clear() {
|
|
|
34
34
|
}
|
|
35
35
|
try {
|
|
36
36
|
tracker();
|
|
37
|
-
} catch (
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.error("Databuddy clear error:", error);
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
function flush() {
|
|
@@ -47,13 +48,34 @@ function flush() {
|
|
|
47
48
|
}
|
|
48
49
|
try {
|
|
49
50
|
tracker();
|
|
50
|
-
} catch (
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error("Databuddy flush error:", error);
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
function trackError(message, properties) {
|
|
54
56
|
return track("error", { message, ...properties });
|
|
55
57
|
}
|
|
58
|
+
function getAnonymousId(urlParams) {
|
|
59
|
+
if (typeof window === "undefined") return null;
|
|
60
|
+
return urlParams?.get("anonId") || window.databuddy?.anonymousId || localStorage.getItem("did") || null;
|
|
61
|
+
}
|
|
62
|
+
function getSessionId(urlParams) {
|
|
63
|
+
if (typeof window === "undefined") return null;
|
|
64
|
+
return urlParams?.get("sessionId") || window.databuddy?.sessionId || sessionStorage.getItem("did_session") || null;
|
|
65
|
+
}
|
|
66
|
+
function getTrackingIds(urlParams) {
|
|
67
|
+
return {
|
|
68
|
+
anonId: getAnonymousId(urlParams),
|
|
69
|
+
sessionId: getSessionId(urlParams)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function getTrackingParams(urlParams) {
|
|
73
|
+
const anonId = getAnonymousId(urlParams);
|
|
74
|
+
const sessionId = getSessionId(urlParams);
|
|
75
|
+
const params = new URLSearchParams();
|
|
76
|
+
if (anonId) params.set("anonId", anonId);
|
|
77
|
+
if (sessionId) params.set("sessionId", sessionId);
|
|
78
|
+
return params.toString();
|
|
79
|
+
}
|
|
56
80
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
export { Databuddy, clear, flush, getTracker, isTrackerAvailable, track, trackError };
|
|
81
|
+
export { clear, flush, getAnonymousId, getSessionId, getTracker, getTrackingIds, getTrackingParams, isTrackerAvailable, track, trackError };
|
package/dist/node/index.d.mts
CHANGED
|
@@ -5,57 +5,85 @@ interface Logger {
|
|
|
5
5
|
debug(msg: string, data?: Record<string, unknown>): void;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Middleware function that can transform or filter events
|
|
10
|
+
* Return null to drop the event, or return a modified event
|
|
11
|
+
*/
|
|
12
|
+
type Middleware = (event: BatchEventInput) => BatchEventInput | null | Promise<BatchEventInput | null>;
|
|
8
13
|
interface DatabuddyConfig {
|
|
14
|
+
/** Client ID from Databuddy dashboard */
|
|
9
15
|
clientId: string;
|
|
16
|
+
/** Custom API endpoint (default: https://basket.databuddy.cc) */
|
|
10
17
|
apiUrl?: string;
|
|
18
|
+
/** Enable debug logging */
|
|
11
19
|
debug?: boolean;
|
|
20
|
+
/** Custom logger instance */
|
|
12
21
|
logger?: Logger;
|
|
13
|
-
/**
|
|
14
|
-
* Enable automatic batching of events
|
|
15
|
-
* Default: true
|
|
16
|
-
*/
|
|
22
|
+
/** Enable automatic batching (default: true) */
|
|
17
23
|
enableBatching?: boolean;
|
|
18
|
-
/**
|
|
19
|
-
* Number of events to batch before auto-flushing
|
|
20
|
-
* Default: 10, Max: 100
|
|
21
|
-
*/
|
|
24
|
+
/** Number of events to batch before flushing (default: 10, max: 100) */
|
|
22
25
|
batchSize?: number;
|
|
23
|
-
/**
|
|
24
|
-
* Time in milliseconds before auto-flushing batched events
|
|
25
|
-
* Default: 2000ms (2 seconds)
|
|
26
|
-
*/
|
|
26
|
+
/** Time in ms before auto-flushing batched events (default: 2000) */
|
|
27
27
|
batchTimeout?: number;
|
|
28
|
-
/**
|
|
29
|
-
* Maximum number of events to queue before auto-flushing
|
|
30
|
-
* Default: 1000
|
|
31
|
-
*/
|
|
28
|
+
/** Maximum number of events to queue (default: 1000) */
|
|
32
29
|
maxQueueSize?: number;
|
|
30
|
+
/** Middleware functions to transform events */
|
|
31
|
+
middleware?: Middleware[];
|
|
32
|
+
/** Enable event deduplication based on eventId (default: true) */
|
|
33
|
+
enableDeduplication?: boolean;
|
|
34
|
+
/** Maximum deduplication cache size (default: 10000) */
|
|
35
|
+
maxDeduplicationCacheSize?: number;
|
|
33
36
|
}
|
|
34
37
|
interface CustomEventInput {
|
|
38
|
+
/** Event name (required) */
|
|
35
39
|
name: string;
|
|
40
|
+
/** Unique event ID for deduplication */
|
|
36
41
|
eventId?: string;
|
|
42
|
+
/** Anonymous user ID */
|
|
37
43
|
anonymousId?: string | null;
|
|
44
|
+
/** Session ID */
|
|
38
45
|
sessionId?: string | null;
|
|
46
|
+
/** Event timestamp in milliseconds */
|
|
39
47
|
timestamp?: number | null;
|
|
48
|
+
/** Event properties/metadata */
|
|
40
49
|
properties?: Record<string, unknown> | null;
|
|
41
50
|
}
|
|
42
51
|
interface EventResponse {
|
|
52
|
+
/** Whether the event was successfully sent */
|
|
43
53
|
success: boolean;
|
|
54
|
+
/** Server-assigned event ID */
|
|
44
55
|
eventId?: string;
|
|
56
|
+
/** Error message if failed */
|
|
45
57
|
error?: string;
|
|
46
58
|
}
|
|
47
59
|
interface BatchEventInput {
|
|
48
|
-
type
|
|
60
|
+
/** Event type */
|
|
61
|
+
type: "custom";
|
|
62
|
+
/** Event name */
|
|
49
63
|
name: string;
|
|
64
|
+
/** Unique event ID for deduplication */
|
|
50
65
|
eventId?: string;
|
|
66
|
+
/** Anonymous user ID */
|
|
51
67
|
anonymousId?: string | null;
|
|
68
|
+
/** Session ID */
|
|
52
69
|
sessionId?: string | null;
|
|
70
|
+
/** Event timestamp in milliseconds */
|
|
53
71
|
timestamp?: number | null;
|
|
72
|
+
/** Event properties/metadata */
|
|
54
73
|
properties?: Record<string, unknown> | null;
|
|
55
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Global properties that will be attached to all events
|
|
77
|
+
*/
|
|
78
|
+
interface GlobalProperties {
|
|
79
|
+
[key: string]: unknown;
|
|
80
|
+
}
|
|
56
81
|
interface BatchEventResponse {
|
|
82
|
+
/** Whether the batch was successfully sent */
|
|
57
83
|
success: boolean;
|
|
84
|
+
/** Number of events processed */
|
|
58
85
|
processed?: number;
|
|
86
|
+
/** Results for each event in the batch */
|
|
59
87
|
results?: Array<{
|
|
60
88
|
status: string;
|
|
61
89
|
type?: string;
|
|
@@ -63,11 +91,12 @@ interface BatchEventResponse {
|
|
|
63
91
|
message?: string;
|
|
64
92
|
error?: string;
|
|
65
93
|
}>;
|
|
94
|
+
/** Error message if batch failed */
|
|
66
95
|
error?: string;
|
|
67
96
|
}
|
|
68
97
|
|
|
69
98
|
declare class Databuddy {
|
|
70
|
-
private clientId;
|
|
99
|
+
private readonly clientId;
|
|
71
100
|
private apiUrl;
|
|
72
101
|
private logger;
|
|
73
102
|
private enableBatching;
|
|
@@ -75,20 +104,21 @@ declare class Databuddy {
|
|
|
75
104
|
private batchTimeout;
|
|
76
105
|
private queue;
|
|
77
106
|
private flushTimer;
|
|
107
|
+
private globalProperties;
|
|
108
|
+
private middleware;
|
|
109
|
+
private enableDeduplication;
|
|
110
|
+
private deduplicationCache;
|
|
111
|
+
private maxDeduplicationCacheSize;
|
|
78
112
|
constructor(config: DatabuddyConfig);
|
|
79
113
|
/**
|
|
80
114
|
* Track a custom event
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* @param event - Custom event data
|
|
85
|
-
* @returns Response indicating success or failure
|
|
86
|
-
*
|
|
115
|
+
* @param event - Event data to track
|
|
116
|
+
* @returns Promise with success/error response
|
|
87
117
|
* @example
|
|
88
118
|
* ```typescript
|
|
89
119
|
* await client.track({
|
|
90
120
|
* name: 'user_signup',
|
|
91
|
-
* properties: { plan: 'pro' }
|
|
121
|
+
* properties: { plan: 'pro', source: 'web' }
|
|
92
122
|
* });
|
|
93
123
|
* ```
|
|
94
124
|
*/
|
|
@@ -97,26 +127,19 @@ declare class Databuddy {
|
|
|
97
127
|
private scheduleFlush;
|
|
98
128
|
/**
|
|
99
129
|
* Manually flush all queued events
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* @returns Response with batch results
|
|
103
|
-
*
|
|
130
|
+
* Useful in serverless environments to ensure events are sent before process exits
|
|
131
|
+
* @returns Promise with batch results
|
|
104
132
|
* @example
|
|
105
133
|
* ```typescript
|
|
106
|
-
* // In serverless function
|
|
107
134
|
* await client.track({ name: 'api_call' });
|
|
108
|
-
* await client.flush(); // Ensure events are sent
|
|
135
|
+
* await client.flush(); // Ensure events are sent
|
|
109
136
|
* ```
|
|
110
137
|
*/
|
|
111
138
|
flush(): Promise<BatchEventResponse>;
|
|
112
139
|
/**
|
|
113
|
-
* Send multiple
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* @param events - Array of custom events
|
|
118
|
-
* @returns Response with results for each event
|
|
119
|
-
*
|
|
140
|
+
* Send multiple events in a single batch request
|
|
141
|
+
* @param events - Array of events to batch (max 100)
|
|
142
|
+
* @returns Promise with batch results
|
|
120
143
|
* @example
|
|
121
144
|
* ```typescript
|
|
122
145
|
* await client.batch([
|
|
@@ -126,7 +149,89 @@ declare class Databuddy {
|
|
|
126
149
|
* ```
|
|
127
150
|
*/
|
|
128
151
|
batch(events: BatchEventInput[]): Promise<BatchEventResponse>;
|
|
152
|
+
/**
|
|
153
|
+
* Set global properties attached to all events
|
|
154
|
+
* Properties are merged with existing globals; event properties override globals
|
|
155
|
+
* @param properties - Properties to merge with existing globals
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* client.setGlobalProperties({ environment: 'production', version: '1.0.0' });
|
|
159
|
+
* // All subsequent events will include these properties
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
setGlobalProperties(properties: GlobalProperties): void;
|
|
163
|
+
/**
|
|
164
|
+
* Get current global properties
|
|
165
|
+
* @returns Copy of current global properties
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const globals = client.getGlobalProperties();
|
|
169
|
+
* console.log(globals); // { environment: 'production', version: '1.0.0' }
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
getGlobalProperties(): GlobalProperties;
|
|
173
|
+
/**
|
|
174
|
+
* Clear all global properties
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* client.clearGlobalProperties();
|
|
178
|
+
* // No more global properties will be attached to events
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
clearGlobalProperties(): void;
|
|
182
|
+
/**
|
|
183
|
+
* Add middleware to transform events
|
|
184
|
+
* Middleware runs before deduplication and is executed in order
|
|
185
|
+
* Return null to drop the event, or return a modified event
|
|
186
|
+
* @param middleware - Middleware function
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* client.addMiddleware((event) => {
|
|
190
|
+
* // Add custom field
|
|
191
|
+
* event.properties = { ...event.properties, processed: true };
|
|
192
|
+
* return event;
|
|
193
|
+
* });
|
|
194
|
+
*
|
|
195
|
+
* // Drop events matching a condition
|
|
196
|
+
* client.addMiddleware((event) => {
|
|
197
|
+
* if (event.name === 'unwanted_event') return null;
|
|
198
|
+
* return event;
|
|
199
|
+
* });
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
addMiddleware(middleware: Middleware): void;
|
|
203
|
+
/**
|
|
204
|
+
* Remove all middleware functions
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* client.clearMiddleware();
|
|
208
|
+
* // All middleware transformations removed
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
clearMiddleware(): void;
|
|
212
|
+
/**
|
|
213
|
+
* Get current deduplication cache size
|
|
214
|
+
* @returns Number of event IDs in cache
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* const size = client.getDeduplicationCacheSize();
|
|
218
|
+
* console.log(`Cache size: ${size}`);
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
getDeduplicationCacheSize(): number;
|
|
222
|
+
/**
|
|
223
|
+
* Clear the deduplication cache
|
|
224
|
+
* Useful for testing or resetting duplicate detection
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* client.clearDeduplicationCache();
|
|
228
|
+
* // All cached event IDs cleared
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
clearDeduplicationCache(): void;
|
|
232
|
+
private applyMiddleware;
|
|
233
|
+
private addToDeduplicationCache;
|
|
129
234
|
}
|
|
130
235
|
|
|
131
236
|
export { Databuddy, Databuddy as db };
|
|
132
|
-
export type { BatchEventInput, BatchEventResponse, CustomEventInput, DatabuddyConfig, EventResponse, Logger };
|
|
237
|
+
export type { BatchEventInput, BatchEventResponse, CustomEventInput, DatabuddyConfig, EventResponse, GlobalProperties, Logger, Middleware };
|