@trakoo/bento 0.0.0
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/LICENSE +21 -0
- package/README.md +56 -0
- package/dist/server.d.ts +58 -0
- package/dist/server.js +194 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 trakoo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @trakoo/bento
|
|
2
|
+
|
|
3
|
+
Bento server provider for [trakoo](https://www.npmjs.com/package/trakoo), the typed, provider-agnostic analytics library.
|
|
4
|
+
|
|
5
|
+
The browser provider, `BentoClientProvider`, needs no SDK and ships with trakoo in `trakoo/providers/client`. Install this package only for server-side tracking.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add trakoo @trakoo/bento @bentonow/bento-node-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
`appEvents` is your event registry, created with `defineEvents()` from `trakoo`.
|
|
16
|
+
|
|
17
|
+
```typescript title="lib/server-analytics.ts"
|
|
18
|
+
import { createServerAnalytics } from 'trakoo/server';
|
|
19
|
+
import { BentoServerProvider } from '@trakoo/bento/server';
|
|
20
|
+
import { appEvents } from './events';
|
|
21
|
+
|
|
22
|
+
export const serverAnalytics = createServerAnalytics({
|
|
23
|
+
events: appEvents,
|
|
24
|
+
providers: [
|
|
25
|
+
{
|
|
26
|
+
provider: new BentoServerProvider({
|
|
27
|
+
siteUuid: process.env.BENTO_SITE_UUID!,
|
|
28
|
+
authentication: {
|
|
29
|
+
publishableKey: process.env.BENTO_PUBLISHABLE_KEY!,
|
|
30
|
+
secretKey: process.env.BENTO_SECRET_KEY!
|
|
31
|
+
}
|
|
32
|
+
}),
|
|
33
|
+
exclude: ['pageView']
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Bento requires an email for every event. Pass it with each server call, and route anonymous page views to another provider:
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
await serverAnalytics.track('subscription_renewed', { plan: 'pro' }, {
|
|
43
|
+
user: { email: 'user@example.com' }
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## What reaches Bento
|
|
48
|
+
|
|
49
|
+
- `identify()` sends `$update_fields`, so traits become subscriber fields. It does not send `$subscribe`. To subscribe someone, call the Bento SDK's `V1.addSubscriber()` directly.
|
|
50
|
+
- `track()` sends `$<event name>`. For example, `subscription_renewed` arrives as `$subscription_renewed`. The event timestamp becomes the Bento event `date`.
|
|
51
|
+
- `pageView()` sends `$view`.
|
|
52
|
+
- Failed requests, and events Bento does not queue, are logged without the event payload and are not thrown. Set `logErrors: true` to have the Bento SDK log the HTTP response of failed requests.
|
|
53
|
+
|
|
54
|
+
## Documentation
|
|
55
|
+
|
|
56
|
+
https://trakoo.co/docs/providers/bento
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { BaseAnalyticsProvider, type BaseEvent, type EventContext } from "trakoo";
|
|
2
|
+
export interface BentoAnalyticsOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Your Bento Site UUID from Team Settings
|
|
5
|
+
*/
|
|
6
|
+
siteUuid: string;
|
|
7
|
+
/**
|
|
8
|
+
* Authentication credentials
|
|
9
|
+
*/
|
|
10
|
+
authentication: {
|
|
11
|
+
/**
|
|
12
|
+
* Your Bento Publishable Key from Team Settings
|
|
13
|
+
*/
|
|
14
|
+
publishableKey: string;
|
|
15
|
+
/**
|
|
16
|
+
* Your Bento Secret Key from Team Settings
|
|
17
|
+
*/
|
|
18
|
+
secretKey: string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Optional client configuration
|
|
22
|
+
*/
|
|
23
|
+
clientOptions?: {
|
|
24
|
+
/**
|
|
25
|
+
* Base URL for the Bento API (optional)
|
|
26
|
+
*/
|
|
27
|
+
baseUrl?: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Whether to log errors (optional)
|
|
31
|
+
*/
|
|
32
|
+
logErrors?: boolean;
|
|
33
|
+
}
|
|
34
|
+
export interface BentoServerConfig extends BentoAnalyticsOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Enable debug logging
|
|
37
|
+
*/
|
|
38
|
+
debug?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Enable/disable the provider
|
|
41
|
+
*/
|
|
42
|
+
enabled?: boolean;
|
|
43
|
+
}
|
|
44
|
+
export declare class BentoServerProvider extends BaseAnalyticsProvider {
|
|
45
|
+
name: string;
|
|
46
|
+
private client?;
|
|
47
|
+
private initialized;
|
|
48
|
+
private config;
|
|
49
|
+
constructor(config: BentoServerConfig);
|
|
50
|
+
initialize(): Promise<void>;
|
|
51
|
+
identify(userId: string, traits?: Record<string, unknown>): Promise<void>;
|
|
52
|
+
track(event: BaseEvent, context?: EventContext): Promise<void>;
|
|
53
|
+
pageView(properties?: Record<string, unknown>, context?: EventContext): Promise<void>;
|
|
54
|
+
reset(): Promise<void>;
|
|
55
|
+
shutdown(): Promise<void>;
|
|
56
|
+
/** Logs a failed call without the event, its user, or the SDK's message. */
|
|
57
|
+
private logFailure;
|
|
58
|
+
}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { BaseAnalyticsProvider, } from "trakoo";
|
|
2
|
+
export class BentoServerProvider extends BaseAnalyticsProvider {
|
|
3
|
+
name = "Bento-Server";
|
|
4
|
+
client;
|
|
5
|
+
initialized = false;
|
|
6
|
+
config;
|
|
7
|
+
constructor(config) {
|
|
8
|
+
super({ debug: config.debug, enabled: config.enabled });
|
|
9
|
+
this.config = config;
|
|
10
|
+
}
|
|
11
|
+
async initialize() {
|
|
12
|
+
if (!this.isEnabled())
|
|
13
|
+
return;
|
|
14
|
+
if (this.initialized)
|
|
15
|
+
return;
|
|
16
|
+
// Validate config has required fields
|
|
17
|
+
if (!this.config.siteUuid || typeof this.config.siteUuid !== "string") {
|
|
18
|
+
throw new Error("Bento requires a siteUuid");
|
|
19
|
+
}
|
|
20
|
+
if (!this.config.authentication?.publishableKey ||
|
|
21
|
+
typeof this.config.authentication.publishableKey !== "string") {
|
|
22
|
+
throw new Error("Bento requires authentication.publishableKey");
|
|
23
|
+
}
|
|
24
|
+
if (!this.config.authentication?.secretKey ||
|
|
25
|
+
typeof this.config.authentication.secretKey !== "string") {
|
|
26
|
+
throw new Error("Bento requires authentication.secretKey");
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
// Dynamically import the Bento SDK
|
|
30
|
+
// This will be resolved at runtime when the package is installed
|
|
31
|
+
const { Analytics } = await import("@bentonow/bento-node-sdk");
|
|
32
|
+
const { debug, enabled, ...bentoConfig } = this.config;
|
|
33
|
+
this.client = new Analytics(bentoConfig);
|
|
34
|
+
this.initialized = true;
|
|
35
|
+
this.log("Initialized successfully");
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
this.logFailure("initialize", this.getErrorClass(error));
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async identify(userId, traits) {
|
|
43
|
+
if (!this.isEnabled() || !this.initialized || !this.client)
|
|
44
|
+
return;
|
|
45
|
+
// Extract email from userId or traits
|
|
46
|
+
const email = traits?.email || userId;
|
|
47
|
+
// Validate that we have a proper email format
|
|
48
|
+
if (!email || !email.includes("@")) {
|
|
49
|
+
this.log("Skipping identify - invalid or missing email");
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
// Update the subscriber's fields. Bento creates the subscriber when needed.
|
|
53
|
+
// V1.addSubscriber is not used: it sends $subscribe, which subscribes the
|
|
54
|
+
// person to marketing email and re-runs subscribe automations on every call.
|
|
55
|
+
const { email: _email, ...fields } = traits ?? {};
|
|
56
|
+
try {
|
|
57
|
+
const queued = await this.client.V1.updateFields({ email, fields });
|
|
58
|
+
if (!queued) {
|
|
59
|
+
this.logFailure("identify user", "not queued by Bento");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.log("Identified user");
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
this.logFailure("identify user", this.getErrorClass(error));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async track(event, context) {
|
|
69
|
+
if (!this.isEnabled() || !this.initialized || !this.client)
|
|
70
|
+
return;
|
|
71
|
+
// Server providers use only identity supplied on the current call.
|
|
72
|
+
const email = context?.user?.email ||
|
|
73
|
+
context?.user?.userId ||
|
|
74
|
+
event.userId;
|
|
75
|
+
// Bento SDK does not currently support anonymous events
|
|
76
|
+
// See: https://github.com/bentonow/bento-node-sdk
|
|
77
|
+
if (!email || !email.includes("@")) {
|
|
78
|
+
console.warn("[Bento-Server] Skipping event - Bento requires an email address. " +
|
|
79
|
+
"Anonymous events are not currently supported by the Bento Node SDK. " +
|
|
80
|
+
"For now, use the Bento client provider for anonymous tracking. " +
|
|
81
|
+
"If you're using a proxy, use the hybrid pattern as described in the docs. " +
|
|
82
|
+
"For identified events, provide a valid email in the current call's user context or event userId; calling identify() earlier does not set event identity.");
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const details = {
|
|
86
|
+
...event.properties,
|
|
87
|
+
category: event.category,
|
|
88
|
+
timestamp: event.timestamp || Date.now(),
|
|
89
|
+
...(event.sessionId && { sessionId: event.sessionId }),
|
|
90
|
+
...(context?.page && {
|
|
91
|
+
page: {
|
|
92
|
+
url: context.page.url,
|
|
93
|
+
host: context.page.host,
|
|
94
|
+
path: context.page.path,
|
|
95
|
+
title: context.page.title,
|
|
96
|
+
protocol: context.page.protocol,
|
|
97
|
+
referrer: context.page.referrer,
|
|
98
|
+
...(context.page.search && { search: context.page.search }),
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
...(context?.device && { device: context.device }),
|
|
102
|
+
...(context?.utm && { utm: context.utm }),
|
|
103
|
+
site: this.config.siteUuid,
|
|
104
|
+
...(context?.user?.userId && { visitor: context.user.userId }),
|
|
105
|
+
};
|
|
106
|
+
// Add user traits as fields if available
|
|
107
|
+
const fields = context?.user?.traits || {};
|
|
108
|
+
try {
|
|
109
|
+
const queued = await this.client.V1.track({
|
|
110
|
+
email,
|
|
111
|
+
type: `$${event.action}`,
|
|
112
|
+
details,
|
|
113
|
+
fields,
|
|
114
|
+
...(event.timestamp !== undefined && {
|
|
115
|
+
date: new Date(event.timestamp),
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
if (!queued) {
|
|
119
|
+
this.logFailure("track event", "not queued by Bento");
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
this.log("Tracked event");
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
this.logFailure("track event", this.getErrorClass(error));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async pageView(properties, context) {
|
|
129
|
+
if (!this.isEnabled() || !this.initialized || !this.client)
|
|
130
|
+
return;
|
|
131
|
+
// Page views may only use identity supplied in the current context.
|
|
132
|
+
const email = context?.user?.email || context?.user?.userId;
|
|
133
|
+
// Bento SDK does not currently support anonymous events
|
|
134
|
+
// See: https://github.com/bentonow/bento-node-sdk
|
|
135
|
+
if (!email || !email.includes("@")) {
|
|
136
|
+
console.warn("[Bento-Server] Skipping pageView - Bento requires an email address. " +
|
|
137
|
+
"Anonymous events are not currently supported by the Bento Node SDK. " +
|
|
138
|
+
"For now, use the Bento client provider for anonymous tracking. " +
|
|
139
|
+
"If you're using a proxy, use the hybrid pattern as described in the docs. " +
|
|
140
|
+
"For identified page views, provide a valid email in the current call's user context; calling identify() earlier does not set page identity.");
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const details = {
|
|
144
|
+
...properties,
|
|
145
|
+
date: new Date().toISOString(),
|
|
146
|
+
...(context?.page && {
|
|
147
|
+
page: {
|
|
148
|
+
url: context.page.url,
|
|
149
|
+
host: context.page.host,
|
|
150
|
+
path: context.page.path,
|
|
151
|
+
title: context.page.title,
|
|
152
|
+
protocol: context.page.protocol,
|
|
153
|
+
referrer: context.page.referrer,
|
|
154
|
+
...(context.page.search && { search: context.page.search }),
|
|
155
|
+
},
|
|
156
|
+
}),
|
|
157
|
+
site: this.config.siteUuid,
|
|
158
|
+
...(context?.user?.userId && { visitor: context.user.userId }),
|
|
159
|
+
};
|
|
160
|
+
const fields = context?.user?.traits || {};
|
|
161
|
+
try {
|
|
162
|
+
const queued = await this.client.V1.track({
|
|
163
|
+
email,
|
|
164
|
+
type: "$view",
|
|
165
|
+
details,
|
|
166
|
+
fields,
|
|
167
|
+
});
|
|
168
|
+
if (!queued) {
|
|
169
|
+
this.logFailure("track page view", "not queued by Bento");
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
this.log("Tracked page view");
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
this.logFailure("track page view", this.getErrorClass(error));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async reset() {
|
|
179
|
+
if (!this.isEnabled() || !this.initialized || !this.client)
|
|
180
|
+
return;
|
|
181
|
+
this.log("Reset called; server provider has no retained identity");
|
|
182
|
+
}
|
|
183
|
+
async shutdown() {
|
|
184
|
+
// Bento Node SDK doesn't require explicit shutdown
|
|
185
|
+
// Just clear the reference
|
|
186
|
+
this.client = undefined;
|
|
187
|
+
this.initialized = false;
|
|
188
|
+
this.log("Shutdown complete");
|
|
189
|
+
}
|
|
190
|
+
/** Logs a failed call without the event, its user, or the SDK's message. */
|
|
191
|
+
logFailure(action, reason) {
|
|
192
|
+
console.error(`[${this.name}] Failed to ${action} (${reason})`);
|
|
193
|
+
}
|
|
194
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trakoo/bento",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Bento provider for trakoo",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./server": {
|
|
8
|
+
"types": "./dist/server.d.ts",
|
|
9
|
+
"import": "./dist/server.js",
|
|
10
|
+
"default": "./dist/server.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"keywords": [
|
|
20
|
+
"analytics",
|
|
21
|
+
"trakoo",
|
|
22
|
+
"bento",
|
|
23
|
+
"typescript"
|
|
24
|
+
],
|
|
25
|
+
"author": "Chris Jayden <https://github.com/multiplehats>",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/multiplehats/trakoo.git",
|
|
30
|
+
"directory": "packages/bento"
|
|
31
|
+
},
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/multiplehats/trakoo/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://trakoo.co/docs/providers/bento",
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=20"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@bentonow/bento-node-sdk": "^0.2.1 || ^2.0.0",
|
|
44
|
+
"trakoo": "^1.2.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@bentonow/bento-node-sdk": "^2.0.0",
|
|
48
|
+
"trakoo": "1.2.1"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsc -p tsconfig.build.json",
|
|
52
|
+
"typecheck": "tsc --noEmit",
|
|
53
|
+
"test": "vitest run"
|
|
54
|
+
}
|
|
55
|
+
}
|