posthog-node 5.8.1 → 5.8.2
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/edge/index.cjs +693 -8
- package/dist/edge/index.cjs.map +1 -1
- package/dist/edge/index.mjs +693 -8
- package/dist/edge/index.mjs.map +1 -1
- package/dist/index.d.ts +692 -2
- package/dist/node/index.cjs +693 -8
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.mjs +693 -8
- package/dist/node/index.mjs.map +1 -1
- package/package.json +4 -2
package/dist/node/index.mjs
CHANGED
|
@@ -1184,7 +1184,7 @@ function snipLine(line, colno) {
|
|
|
1184
1184
|
return newLine;
|
|
1185
1185
|
}
|
|
1186
1186
|
|
|
1187
|
-
var version = "5.8.
|
|
1187
|
+
var version = "5.8.2";
|
|
1188
1188
|
|
|
1189
1189
|
/**
|
|
1190
1190
|
* A lazy value that is only computed when needed. Inspired by C#'s Lazy<T> class.
|
|
@@ -2085,6 +2085,35 @@ const THIRTY_SECONDS = 30 * 1000;
|
|
|
2085
2085
|
const MAX_CACHE_SIZE = 50 * 1000;
|
|
2086
2086
|
// The actual exported Nodejs API.
|
|
2087
2087
|
class PostHogBackendClient extends PostHogCoreStateless {
|
|
2088
|
+
/**
|
|
2089
|
+
* Initialize a new PostHog client instance.
|
|
2090
|
+
*
|
|
2091
|
+
* @example
|
|
2092
|
+
* ```ts
|
|
2093
|
+
* // Basic initialization
|
|
2094
|
+
* const client = new PostHogBackendClient(
|
|
2095
|
+
* 'your-api-key',
|
|
2096
|
+
* { host: 'https://app.posthog.com' }
|
|
2097
|
+
* )
|
|
2098
|
+
* ```
|
|
2099
|
+
*
|
|
2100
|
+
* @example
|
|
2101
|
+
* ```ts
|
|
2102
|
+
* // With personal API key
|
|
2103
|
+
* const client = new PostHogBackendClient(
|
|
2104
|
+
* 'your-api-key',
|
|
2105
|
+
* {
|
|
2106
|
+
* host: 'https://app.posthog.com',
|
|
2107
|
+
* personalApiKey: 'your-personal-api-key'
|
|
2108
|
+
* }
|
|
2109
|
+
* )
|
|
2110
|
+
* ```
|
|
2111
|
+
*
|
|
2112
|
+
* {@label Initialization}
|
|
2113
|
+
*
|
|
2114
|
+
* @param apiKey - Your PostHog project API key
|
|
2115
|
+
* @param options - Configuration options for the client
|
|
2116
|
+
*/
|
|
2088
2117
|
constructor(apiKey, options = {}) {
|
|
2089
2118
|
super(apiKey, options);
|
|
2090
2119
|
this._memoryStorage = new PostHogMemoryStorage();
|
|
@@ -2120,31 +2149,185 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2120
2149
|
this.distinctIdHasSentFlagCalls = {};
|
|
2121
2150
|
this.maxCacheSize = options.maxCacheSize || MAX_CACHE_SIZE;
|
|
2122
2151
|
}
|
|
2152
|
+
/**
|
|
2153
|
+
* Get a persisted property value from memory storage.
|
|
2154
|
+
*
|
|
2155
|
+
* @example
|
|
2156
|
+
* ```ts
|
|
2157
|
+
* // Get user ID
|
|
2158
|
+
* const userId = client.getPersistedProperty('userId')
|
|
2159
|
+
* ```
|
|
2160
|
+
*
|
|
2161
|
+
* @example
|
|
2162
|
+
* ```ts
|
|
2163
|
+
* // Get session ID
|
|
2164
|
+
* const sessionId = client.getPersistedProperty('sessionId')
|
|
2165
|
+
* ```
|
|
2166
|
+
*
|
|
2167
|
+
* {@label Initialization}
|
|
2168
|
+
*
|
|
2169
|
+
* @param key - The property key to retrieve
|
|
2170
|
+
* @returns The stored property value or undefined if not found
|
|
2171
|
+
*/
|
|
2123
2172
|
getPersistedProperty(key) {
|
|
2124
2173
|
return this._memoryStorage.getProperty(key);
|
|
2125
2174
|
}
|
|
2175
|
+
/**
|
|
2176
|
+
* Set a persisted property value in memory storage.
|
|
2177
|
+
*
|
|
2178
|
+
* @example
|
|
2179
|
+
* ```ts
|
|
2180
|
+
* // Set user ID
|
|
2181
|
+
* client.setPersistedProperty('userId', 'user_123')
|
|
2182
|
+
* ```
|
|
2183
|
+
*
|
|
2184
|
+
* @example
|
|
2185
|
+
* ```ts
|
|
2186
|
+
* // Set session ID
|
|
2187
|
+
* client.setPersistedProperty('sessionId', 'session_456')
|
|
2188
|
+
* ```
|
|
2189
|
+
*
|
|
2190
|
+
* {@label Initialization}
|
|
2191
|
+
*
|
|
2192
|
+
* @param key - The property key to set
|
|
2193
|
+
* @param value - The value to store (null to remove)
|
|
2194
|
+
*/
|
|
2126
2195
|
setPersistedProperty(key, value) {
|
|
2127
2196
|
return this._memoryStorage.setProperty(key, value);
|
|
2128
2197
|
}
|
|
2198
|
+
/**
|
|
2199
|
+
* Make an HTTP request using the configured fetch function or default fetch.
|
|
2200
|
+
*
|
|
2201
|
+
* @example
|
|
2202
|
+
* ```ts
|
|
2203
|
+
* // POST request
|
|
2204
|
+
* const response = await client.fetch('/api/endpoint', {
|
|
2205
|
+
* method: 'POST',
|
|
2206
|
+
* headers: { 'Content-Type': 'application/json' },
|
|
2207
|
+
* body: JSON.stringify(data)
|
|
2208
|
+
* })
|
|
2209
|
+
* ```
|
|
2210
|
+
*
|
|
2211
|
+
* @internal
|
|
2212
|
+
*
|
|
2213
|
+
* {@label Initialization}
|
|
2214
|
+
*
|
|
2215
|
+
* @param url - The URL to fetch
|
|
2216
|
+
* @param options - Fetch options
|
|
2217
|
+
* @returns Promise resolving to the fetch response
|
|
2218
|
+
*/
|
|
2129
2219
|
fetch(url, options) {
|
|
2130
2220
|
return this.options.fetch ? this.options.fetch(url, options) : fetch(url, options);
|
|
2131
2221
|
}
|
|
2222
|
+
/**
|
|
2223
|
+
* Get the library version from package.json.
|
|
2224
|
+
*
|
|
2225
|
+
* @example
|
|
2226
|
+
* ```ts
|
|
2227
|
+
* // Get version
|
|
2228
|
+
* const version = client.getLibraryVersion()
|
|
2229
|
+
* console.log(`Using PostHog SDK version: ${version}`)
|
|
2230
|
+
* ```
|
|
2231
|
+
*
|
|
2232
|
+
* {@label Initialization}
|
|
2233
|
+
*
|
|
2234
|
+
* @returns The current library version string
|
|
2235
|
+
*/
|
|
2132
2236
|
getLibraryVersion() {
|
|
2133
2237
|
return version;
|
|
2134
2238
|
}
|
|
2239
|
+
/**
|
|
2240
|
+
* Get the custom user agent string for this client.
|
|
2241
|
+
*
|
|
2242
|
+
* @example
|
|
2243
|
+
* ```ts
|
|
2244
|
+
* // Get user agent
|
|
2245
|
+
* const userAgent = client.getCustomUserAgent()
|
|
2246
|
+
* // Returns: "posthog-node/5.7.0"
|
|
2247
|
+
* ```
|
|
2248
|
+
*
|
|
2249
|
+
* {@label Identification}
|
|
2250
|
+
*
|
|
2251
|
+
* @returns The formatted user agent string
|
|
2252
|
+
*/
|
|
2135
2253
|
getCustomUserAgent() {
|
|
2136
2254
|
return `${this.getLibraryId()}/${this.getLibraryVersion()}`;
|
|
2137
2255
|
}
|
|
2256
|
+
/**
|
|
2257
|
+
* Enable the PostHog client (opt-in).
|
|
2258
|
+
*
|
|
2259
|
+
* @example
|
|
2260
|
+
* ```ts
|
|
2261
|
+
* // Enable client
|
|
2262
|
+
* await client.enable()
|
|
2263
|
+
* // Client is now enabled and will capture events
|
|
2264
|
+
* ```
|
|
2265
|
+
*
|
|
2266
|
+
* {@label Privacy}
|
|
2267
|
+
*
|
|
2268
|
+
* @returns Promise that resolves when the client is enabled
|
|
2269
|
+
*/
|
|
2138
2270
|
enable() {
|
|
2139
2271
|
return super.optIn();
|
|
2140
2272
|
}
|
|
2273
|
+
/**
|
|
2274
|
+
* Disable the PostHog client (opt-out).
|
|
2275
|
+
*
|
|
2276
|
+
* @example
|
|
2277
|
+
* ```ts
|
|
2278
|
+
* // Disable client
|
|
2279
|
+
* await client.disable()
|
|
2280
|
+
* // Client is now disabled and will not capture events
|
|
2281
|
+
* ```
|
|
2282
|
+
*
|
|
2283
|
+
* {@label Privacy}
|
|
2284
|
+
*
|
|
2285
|
+
* @returns Promise that resolves when the client is disabled
|
|
2286
|
+
*/
|
|
2141
2287
|
disable() {
|
|
2142
2288
|
return super.optOut();
|
|
2143
2289
|
}
|
|
2290
|
+
/**
|
|
2291
|
+
* Enable or disable debug logging.
|
|
2292
|
+
*
|
|
2293
|
+
* @example
|
|
2294
|
+
* ```ts
|
|
2295
|
+
* // Enable debug logging
|
|
2296
|
+
* client.debug(true)
|
|
2297
|
+
* ```
|
|
2298
|
+
*
|
|
2299
|
+
* @example
|
|
2300
|
+
* ```ts
|
|
2301
|
+
* // Disable debug logging
|
|
2302
|
+
* client.debug(false)
|
|
2303
|
+
* ```
|
|
2304
|
+
*
|
|
2305
|
+
* {@label Initialization}
|
|
2306
|
+
*
|
|
2307
|
+
* @param enabled - Whether to enable debug logging
|
|
2308
|
+
*/
|
|
2144
2309
|
debug(enabled = true) {
|
|
2145
2310
|
super.debug(enabled);
|
|
2146
2311
|
this.featureFlagsPoller?.debug(enabled);
|
|
2147
2312
|
}
|
|
2313
|
+
/**
|
|
2314
|
+
* Capture an event manually.
|
|
2315
|
+
*
|
|
2316
|
+
* @example
|
|
2317
|
+
* ```ts
|
|
2318
|
+
* // Basic capture
|
|
2319
|
+
* client.capture({
|
|
2320
|
+
* distinctId: 'user_123',
|
|
2321
|
+
* event: 'button_clicked',
|
|
2322
|
+
* properties: { button_color: 'red' }
|
|
2323
|
+
* })
|
|
2324
|
+
* ```
|
|
2325
|
+
*
|
|
2326
|
+
* {@label Capture}
|
|
2327
|
+
*
|
|
2328
|
+
* @param props - The event properties
|
|
2329
|
+
* @returns void
|
|
2330
|
+
*/
|
|
2148
2331
|
capture(props) {
|
|
2149
2332
|
if (typeof props === 'string') {
|
|
2150
2333
|
this.logMsgIfDebug(() => console.warn('Called capture() with a string as the first argument when an object was expected.'));
|
|
@@ -2166,6 +2349,49 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2166
2349
|
}
|
|
2167
2350
|
}));
|
|
2168
2351
|
}
|
|
2352
|
+
/**
|
|
2353
|
+
* Capture an event immediately (synchronously).
|
|
2354
|
+
*
|
|
2355
|
+
* @example
|
|
2356
|
+
* ```ts
|
|
2357
|
+
* // Basic immediate capture
|
|
2358
|
+
* await client.captureImmediate({
|
|
2359
|
+
* distinctId: 'user_123',
|
|
2360
|
+
* event: 'button_clicked',
|
|
2361
|
+
* properties: { button_color: 'red' }
|
|
2362
|
+
* })
|
|
2363
|
+
* ```
|
|
2364
|
+
*
|
|
2365
|
+
* @example
|
|
2366
|
+
* ```ts
|
|
2367
|
+
* // With feature flags
|
|
2368
|
+
* await client.captureImmediate({
|
|
2369
|
+
* distinctId: 'user_123',
|
|
2370
|
+
* event: 'user_action',
|
|
2371
|
+
* sendFeatureFlags: true
|
|
2372
|
+
* })
|
|
2373
|
+
* ```
|
|
2374
|
+
*
|
|
2375
|
+
* @example
|
|
2376
|
+
* ```ts
|
|
2377
|
+
* // With custom feature flags options
|
|
2378
|
+
* await client.captureImmediate({
|
|
2379
|
+
* distinctId: 'user_123',
|
|
2380
|
+
* event: 'user_action',
|
|
2381
|
+
* sendFeatureFlags: {
|
|
2382
|
+
* onlyEvaluateLocally: true,
|
|
2383
|
+
* personProperties: { plan: 'premium' },
|
|
2384
|
+
* groupProperties: { org: { tier: 'enterprise' } }
|
|
2385
|
+
* flagKeys: ['flag1', 'flag2']
|
|
2386
|
+
* }
|
|
2387
|
+
* })
|
|
2388
|
+
* ```
|
|
2389
|
+
*
|
|
2390
|
+
* {@label Capture}
|
|
2391
|
+
*
|
|
2392
|
+
* @param props - The event properties
|
|
2393
|
+
* @returns Promise that resolves when the event is captured
|
|
2394
|
+
*/
|
|
2169
2395
|
async captureImmediate(props) {
|
|
2170
2396
|
if (typeof props === 'string') {
|
|
2171
2397
|
this.logMsgIfDebug(() => console.warn('Called captureImmediate() with a string as the first argument when an object was expected.'));
|
|
@@ -2187,6 +2413,38 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2187
2413
|
}
|
|
2188
2414
|
}));
|
|
2189
2415
|
}
|
|
2416
|
+
/**
|
|
2417
|
+
* Identify a user and set their properties.
|
|
2418
|
+
*
|
|
2419
|
+
* @example
|
|
2420
|
+
* ```ts
|
|
2421
|
+
* // Basic identify with properties
|
|
2422
|
+
* client.identify({
|
|
2423
|
+
* distinctId: 'user_123',
|
|
2424
|
+
* properties: {
|
|
2425
|
+
* name: 'John Doe',
|
|
2426
|
+
* email: 'john@example.com',
|
|
2427
|
+
* plan: 'premium'
|
|
2428
|
+
* }
|
|
2429
|
+
* })
|
|
2430
|
+
* ```
|
|
2431
|
+
*
|
|
2432
|
+
* @example
|
|
2433
|
+
* ```ts
|
|
2434
|
+
* // Using $set and $set_once
|
|
2435
|
+
* client.identify({
|
|
2436
|
+
* distinctId: 'user_123',
|
|
2437
|
+
* properties: {
|
|
2438
|
+
* $set: { name: 'John Doe', email: 'john@example.com' },
|
|
2439
|
+
* $set_once: { first_login: new Date().toISOString() }
|
|
2440
|
+
* }
|
|
2441
|
+
* })
|
|
2442
|
+
* ```
|
|
2443
|
+
*
|
|
2444
|
+
* {@label Identification}
|
|
2445
|
+
*
|
|
2446
|
+
* @param data - The identify data containing distinctId and properties
|
|
2447
|
+
*/
|
|
2190
2448
|
identify({
|
|
2191
2449
|
distinctId,
|
|
2192
2450
|
properties,
|
|
@@ -2205,6 +2463,26 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2205
2463
|
disableGeoip
|
|
2206
2464
|
});
|
|
2207
2465
|
}
|
|
2466
|
+
/**
|
|
2467
|
+
* Identify a user and set their properties immediately (synchronously).
|
|
2468
|
+
*
|
|
2469
|
+
* @example
|
|
2470
|
+
* ```ts
|
|
2471
|
+
* // Basic immediate identify
|
|
2472
|
+
* await client.identifyImmediate({
|
|
2473
|
+
* distinctId: 'user_123',
|
|
2474
|
+
* properties: {
|
|
2475
|
+
* name: 'John Doe',
|
|
2476
|
+
* email: 'john@example.com'
|
|
2477
|
+
* }
|
|
2478
|
+
* })
|
|
2479
|
+
* ```
|
|
2480
|
+
*
|
|
2481
|
+
* {@label Identification}
|
|
2482
|
+
*
|
|
2483
|
+
* @param data - The identify data containing distinctId and properties
|
|
2484
|
+
* @returns Promise that resolves when the identify is processed
|
|
2485
|
+
*/
|
|
2208
2486
|
async identifyImmediate({
|
|
2209
2487
|
distinctId,
|
|
2210
2488
|
properties,
|
|
@@ -2222,19 +2500,96 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2222
2500
|
disableGeoip
|
|
2223
2501
|
});
|
|
2224
2502
|
}
|
|
2503
|
+
/**
|
|
2504
|
+
* Create an alias to link two distinct IDs together.
|
|
2505
|
+
*
|
|
2506
|
+
* @example
|
|
2507
|
+
* ```ts
|
|
2508
|
+
* // Link an anonymous user to an identified user
|
|
2509
|
+
* client.alias({
|
|
2510
|
+
* distinctId: 'anonymous_123',
|
|
2511
|
+
* alias: 'user_456'
|
|
2512
|
+
* })
|
|
2513
|
+
* ```
|
|
2514
|
+
*
|
|
2515
|
+
* {@label Identification}
|
|
2516
|
+
*
|
|
2517
|
+
* @param data - The alias data containing distinctId and alias
|
|
2518
|
+
*/
|
|
2225
2519
|
alias(data) {
|
|
2226
2520
|
super.aliasStateless(data.alias, data.distinctId, undefined, {
|
|
2227
2521
|
disableGeoip: data.disableGeoip
|
|
2228
2522
|
});
|
|
2229
2523
|
}
|
|
2524
|
+
/**
|
|
2525
|
+
* Create an alias to link two distinct IDs together immediately (synchronously).
|
|
2526
|
+
*
|
|
2527
|
+
* @example
|
|
2528
|
+
* ```ts
|
|
2529
|
+
* // Link an anonymous user to an identified user immediately
|
|
2530
|
+
* await client.aliasImmediate({
|
|
2531
|
+
* distinctId: 'anonymous_123',
|
|
2532
|
+
* alias: 'user_456'
|
|
2533
|
+
* })
|
|
2534
|
+
* ```
|
|
2535
|
+
*
|
|
2536
|
+
* {@label Identification}
|
|
2537
|
+
*
|
|
2538
|
+
* @param data - The alias data containing distinctId and alias
|
|
2539
|
+
* @returns Promise that resolves when the alias is processed
|
|
2540
|
+
*/
|
|
2230
2541
|
async aliasImmediate(data) {
|
|
2231
2542
|
await super.aliasStatelessImmediate(data.alias, data.distinctId, undefined, {
|
|
2232
2543
|
disableGeoip: data.disableGeoip
|
|
2233
2544
|
});
|
|
2234
2545
|
}
|
|
2546
|
+
/**
|
|
2547
|
+
* Check if local evaluation of feature flags is ready.
|
|
2548
|
+
*
|
|
2549
|
+
* @example
|
|
2550
|
+
* ```ts
|
|
2551
|
+
* // Check if ready
|
|
2552
|
+
* if (client.isLocalEvaluationReady()) {
|
|
2553
|
+
* // Local evaluation is ready, can evaluate flags locally
|
|
2554
|
+
* const flag = await client.getFeatureFlag('flag-key', 'user_123')
|
|
2555
|
+
* } else {
|
|
2556
|
+
* // Local evaluation not ready, will use remote evaluation
|
|
2557
|
+
* const flag = await client.getFeatureFlag('flag-key', 'user_123')
|
|
2558
|
+
* }
|
|
2559
|
+
* ```
|
|
2560
|
+
*
|
|
2561
|
+
* {@label Feature flags}
|
|
2562
|
+
*
|
|
2563
|
+
* @returns true if local evaluation is ready, false otherwise
|
|
2564
|
+
*/
|
|
2235
2565
|
isLocalEvaluationReady() {
|
|
2236
2566
|
return this.featureFlagsPoller?.isLocalEvaluationReady() ?? false;
|
|
2237
2567
|
}
|
|
2568
|
+
/**
|
|
2569
|
+
* Wait for local evaluation of feature flags to be ready.
|
|
2570
|
+
*
|
|
2571
|
+
* @example
|
|
2572
|
+
* ```ts
|
|
2573
|
+
* // Wait for local evaluation
|
|
2574
|
+
* const isReady = await client.waitForLocalEvaluationReady()
|
|
2575
|
+
* if (isReady) {
|
|
2576
|
+
* console.log('Local evaluation is ready')
|
|
2577
|
+
* } else {
|
|
2578
|
+
* console.log('Local evaluation timed out')
|
|
2579
|
+
* }
|
|
2580
|
+
* ```
|
|
2581
|
+
*
|
|
2582
|
+
* @example
|
|
2583
|
+
* ```ts
|
|
2584
|
+
* // Wait with custom timeout
|
|
2585
|
+
* const isReady = await client.waitForLocalEvaluationReady(10000) // 10 seconds
|
|
2586
|
+
* ```
|
|
2587
|
+
*
|
|
2588
|
+
* {@label Feature flags}
|
|
2589
|
+
*
|
|
2590
|
+
* @param timeoutMs - Timeout in milliseconds (default: 30000)
|
|
2591
|
+
* @returns Promise that resolves to true if ready, false if timed out
|
|
2592
|
+
*/
|
|
2238
2593
|
async waitForLocalEvaluationReady(timeoutMs = THIRTY_SECONDS) {
|
|
2239
2594
|
if (this.isLocalEvaluationReady()) {
|
|
2240
2595
|
return true;
|
|
@@ -2254,6 +2609,47 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2254
2609
|
});
|
|
2255
2610
|
});
|
|
2256
2611
|
}
|
|
2612
|
+
/**
|
|
2613
|
+
* Get the value of a feature flag for a specific user.
|
|
2614
|
+
*
|
|
2615
|
+
* @example
|
|
2616
|
+
* ```ts
|
|
2617
|
+
* // Basic feature flag check
|
|
2618
|
+
* const flagValue = await client.getFeatureFlag('new-feature', 'user_123')
|
|
2619
|
+
* if (flagValue === 'variant-a') {
|
|
2620
|
+
* // Show variant A
|
|
2621
|
+
* } else if (flagValue === 'variant-b') {
|
|
2622
|
+
* // Show variant B
|
|
2623
|
+
* } else {
|
|
2624
|
+
* // Flag is disabled or not found
|
|
2625
|
+
* }
|
|
2626
|
+
* ```
|
|
2627
|
+
*
|
|
2628
|
+
* @example
|
|
2629
|
+
* ```ts
|
|
2630
|
+
* // With groups and properties
|
|
2631
|
+
* const flagValue = await client.getFeatureFlag('org-feature', 'user_123', {
|
|
2632
|
+
* groups: { organization: 'acme-corp' },
|
|
2633
|
+
* personProperties: { plan: 'enterprise' },
|
|
2634
|
+
* groupProperties: { organization: { tier: 'premium' } }
|
|
2635
|
+
* })
|
|
2636
|
+
* ```
|
|
2637
|
+
*
|
|
2638
|
+
* @example
|
|
2639
|
+
* ```ts
|
|
2640
|
+
* // Only evaluate locally
|
|
2641
|
+
* const flagValue = await client.getFeatureFlag('local-flag', 'user_123', {
|
|
2642
|
+
* onlyEvaluateLocally: true
|
|
2643
|
+
* })
|
|
2644
|
+
* ```
|
|
2645
|
+
*
|
|
2646
|
+
* {@label Feature flags}
|
|
2647
|
+
*
|
|
2648
|
+
* @param key - The feature flag key
|
|
2649
|
+
* @param distinctId - The user's distinct ID
|
|
2650
|
+
* @param options - Optional configuration for flag evaluation
|
|
2651
|
+
* @returns Promise that resolves to the flag value or undefined
|
|
2652
|
+
*/
|
|
2257
2653
|
async getFeatureFlag(key, distinctId, options) {
|
|
2258
2654
|
const {
|
|
2259
2655
|
groups,
|
|
@@ -2273,7 +2669,7 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2273
2669
|
onlyEvaluateLocally = false;
|
|
2274
2670
|
}
|
|
2275
2671
|
if (sendFeatureFlagEvents == undefined) {
|
|
2276
|
-
sendFeatureFlagEvents = true;
|
|
2672
|
+
sendFeatureFlagEvents = this.options.sendFeatureFlagEvent ?? true;
|
|
2277
2673
|
}
|
|
2278
2674
|
let response = await this.featureFlagsPoller?.getFeatureFlag(key, distinctId, groups, personProperties, groupProperties);
|
|
2279
2675
|
const flagWasLocallyEvaluated = response !== undefined;
|
|
@@ -2317,6 +2713,41 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2317
2713
|
}
|
|
2318
2714
|
return response;
|
|
2319
2715
|
}
|
|
2716
|
+
/**
|
|
2717
|
+
* Get the payload for a feature flag.
|
|
2718
|
+
*
|
|
2719
|
+
* @example
|
|
2720
|
+
* ```ts
|
|
2721
|
+
* // Get payload for a feature flag
|
|
2722
|
+
* const payload = await client.getFeatureFlagPayload('flag-key', 'user_123')
|
|
2723
|
+
* if (payload) {
|
|
2724
|
+
* console.log('Flag payload:', payload)
|
|
2725
|
+
* }
|
|
2726
|
+
* ```
|
|
2727
|
+
*
|
|
2728
|
+
* @example
|
|
2729
|
+
* ```ts
|
|
2730
|
+
* // Get payload with specific match value
|
|
2731
|
+
* const payload = await client.getFeatureFlagPayload('flag-key', 'user_123', 'variant-a')
|
|
2732
|
+
* ```
|
|
2733
|
+
*
|
|
2734
|
+
* @example
|
|
2735
|
+
* ```ts
|
|
2736
|
+
* // With groups and properties
|
|
2737
|
+
* const payload = await client.getFeatureFlagPayload('org-flag', 'user_123', undefined, {
|
|
2738
|
+
* groups: { organization: 'acme-corp' },
|
|
2739
|
+
* personProperties: { plan: 'enterprise' }
|
|
2740
|
+
* })
|
|
2741
|
+
* ```
|
|
2742
|
+
*
|
|
2743
|
+
* {@label Feature flags}
|
|
2744
|
+
*
|
|
2745
|
+
* @param key - The feature flag key
|
|
2746
|
+
* @param distinctId - The user's distinct ID
|
|
2747
|
+
* @param matchValue - Optional match value to get payload for
|
|
2748
|
+
* @param options - Optional configuration for flag evaluation
|
|
2749
|
+
* @returns Promise that resolves to the flag payload or undefined
|
|
2750
|
+
*/
|
|
2320
2751
|
async getFeatureFlagPayload(key, distinctId, matchValue, options) {
|
|
2321
2752
|
const {
|
|
2322
2753
|
groups,
|
|
@@ -2324,7 +2755,6 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2324
2755
|
} = options || {};
|
|
2325
2756
|
let {
|
|
2326
2757
|
onlyEvaluateLocally,
|
|
2327
|
-
sendFeatureFlagEvents,
|
|
2328
2758
|
personProperties,
|
|
2329
2759
|
groupProperties
|
|
2330
2760
|
} = options || {};
|
|
@@ -2349,15 +2779,30 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2349
2779
|
if (onlyEvaluateLocally == undefined) {
|
|
2350
2780
|
onlyEvaluateLocally = false;
|
|
2351
2781
|
}
|
|
2352
|
-
if (sendFeatureFlagEvents == undefined) {
|
|
2353
|
-
sendFeatureFlagEvents = true;
|
|
2354
|
-
}
|
|
2355
2782
|
const payloadWasLocallyEvaluated = response !== undefined;
|
|
2356
2783
|
if (!payloadWasLocallyEvaluated && !onlyEvaluateLocally) {
|
|
2357
2784
|
response = await super.getFeatureFlagPayloadStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
|
|
2358
2785
|
}
|
|
2359
2786
|
return response;
|
|
2360
2787
|
}
|
|
2788
|
+
/**
|
|
2789
|
+
* Get the remote config payload for a feature flag.
|
|
2790
|
+
*
|
|
2791
|
+
* @example
|
|
2792
|
+
* ```ts
|
|
2793
|
+
* // Get remote config payload
|
|
2794
|
+
* const payload = await client.getRemoteConfigPayload('flag-key')
|
|
2795
|
+
* if (payload) {
|
|
2796
|
+
* console.log('Remote config payload:', payload)
|
|
2797
|
+
* }
|
|
2798
|
+
* ```
|
|
2799
|
+
*
|
|
2800
|
+
* {@label Feature flags}
|
|
2801
|
+
*
|
|
2802
|
+
* @param flagKey - The feature flag key
|
|
2803
|
+
* @returns Promise that resolves to the remote config payload or undefined
|
|
2804
|
+
* @throws Error if personal API key is not provided
|
|
2805
|
+
*/
|
|
2361
2806
|
async getRemoteConfigPayload(flagKey) {
|
|
2362
2807
|
if (!this.options.personalApiKey) {
|
|
2363
2808
|
throw new Error('Personal API key is required for remote config payload decryption');
|
|
@@ -2381,6 +2826,38 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2381
2826
|
}
|
|
2382
2827
|
return parsed;
|
|
2383
2828
|
}
|
|
2829
|
+
/**
|
|
2830
|
+
* Check if a feature flag is enabled for a specific user.
|
|
2831
|
+
*
|
|
2832
|
+
* @example
|
|
2833
|
+
* ```ts
|
|
2834
|
+
* // Basic feature flag check
|
|
2835
|
+
* const isEnabled = await client.isFeatureEnabled('new-feature', 'user_123')
|
|
2836
|
+
* if (isEnabled) {
|
|
2837
|
+
* // Feature is enabled
|
|
2838
|
+
* console.log('New feature is active')
|
|
2839
|
+
* } else {
|
|
2840
|
+
* // Feature is disabled
|
|
2841
|
+
* console.log('New feature is not active')
|
|
2842
|
+
* }
|
|
2843
|
+
* ```
|
|
2844
|
+
*
|
|
2845
|
+
* @example
|
|
2846
|
+
* ```ts
|
|
2847
|
+
* // With groups and properties
|
|
2848
|
+
* const isEnabled = await client.isFeatureEnabled('org-feature', 'user_123', {
|
|
2849
|
+
* groups: { organization: 'acme-corp' },
|
|
2850
|
+
* personProperties: { plan: 'enterprise' }
|
|
2851
|
+
* })
|
|
2852
|
+
* ```
|
|
2853
|
+
*
|
|
2854
|
+
* {@label Feature flags}
|
|
2855
|
+
*
|
|
2856
|
+
* @param key - The feature flag key
|
|
2857
|
+
* @param distinctId - The user's distinct ID
|
|
2858
|
+
* @param options - Optional configuration for flag evaluation
|
|
2859
|
+
* @returns Promise that resolves to true if enabled, false if disabled, undefined if not found
|
|
2860
|
+
*/
|
|
2384
2861
|
async isFeatureEnabled(key, distinctId, options) {
|
|
2385
2862
|
const feat = await this.getFeatureFlag(key, distinctId, options);
|
|
2386
2863
|
if (feat === undefined) {
|
|
@@ -2388,10 +2865,77 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2388
2865
|
}
|
|
2389
2866
|
return !!feat || false;
|
|
2390
2867
|
}
|
|
2868
|
+
/**
|
|
2869
|
+
* Get all feature flag values for a specific user.
|
|
2870
|
+
*
|
|
2871
|
+
* @example
|
|
2872
|
+
* ```ts
|
|
2873
|
+
* // Get all flags for a user
|
|
2874
|
+
* const allFlags = await client.getAllFlags('user_123')
|
|
2875
|
+
* console.log('User flags:', allFlags)
|
|
2876
|
+
* // Output: { 'flag-1': 'variant-a', 'flag-2': false, 'flag-3': 'variant-b' }
|
|
2877
|
+
* ```
|
|
2878
|
+
*
|
|
2879
|
+
* @example
|
|
2880
|
+
* ```ts
|
|
2881
|
+
* // With specific flag keys
|
|
2882
|
+
* const specificFlags = await client.getAllFlags('user_123', {
|
|
2883
|
+
* flagKeys: ['flag-1', 'flag-2']
|
|
2884
|
+
* })
|
|
2885
|
+
* ```
|
|
2886
|
+
*
|
|
2887
|
+
* @example
|
|
2888
|
+
* ```ts
|
|
2889
|
+
* // With groups and properties
|
|
2890
|
+
* const orgFlags = await client.getAllFlags('user_123', {
|
|
2891
|
+
* groups: { organization: 'acme-corp' },
|
|
2892
|
+
* personProperties: { plan: 'enterprise' }
|
|
2893
|
+
* })
|
|
2894
|
+
* ```
|
|
2895
|
+
*
|
|
2896
|
+
* {@label Feature flags}
|
|
2897
|
+
*
|
|
2898
|
+
* @param distinctId - The user's distinct ID
|
|
2899
|
+
* @param options - Optional configuration for flag evaluation
|
|
2900
|
+
* @returns Promise that resolves to a record of flag keys and their values
|
|
2901
|
+
*/
|
|
2391
2902
|
async getAllFlags(distinctId, options) {
|
|
2392
2903
|
const response = await this.getAllFlagsAndPayloads(distinctId, options);
|
|
2393
2904
|
return response.featureFlags || {};
|
|
2394
2905
|
}
|
|
2906
|
+
/**
|
|
2907
|
+
* Get all feature flag values and payloads for a specific user.
|
|
2908
|
+
*
|
|
2909
|
+
* @example
|
|
2910
|
+
* ```ts
|
|
2911
|
+
* // Get all flags and payloads for a user
|
|
2912
|
+
* const result = await client.getAllFlagsAndPayloads('user_123')
|
|
2913
|
+
* console.log('Flags:', result.featureFlags)
|
|
2914
|
+
* console.log('Payloads:', result.featureFlagPayloads)
|
|
2915
|
+
* ```
|
|
2916
|
+
*
|
|
2917
|
+
* @example
|
|
2918
|
+
* ```ts
|
|
2919
|
+
* // With specific flag keys
|
|
2920
|
+
* const result = await client.getAllFlagsAndPayloads('user_123', {
|
|
2921
|
+
* flagKeys: ['flag-1', 'flag-2']
|
|
2922
|
+
* })
|
|
2923
|
+
* ```
|
|
2924
|
+
*
|
|
2925
|
+
* @example
|
|
2926
|
+
* ```ts
|
|
2927
|
+
* // Only evaluate locally
|
|
2928
|
+
* const result = await client.getAllFlagsAndPayloads('user_123', {
|
|
2929
|
+
* onlyEvaluateLocally: true
|
|
2930
|
+
* })
|
|
2931
|
+
* ```
|
|
2932
|
+
*
|
|
2933
|
+
* {@label Feature flags}
|
|
2934
|
+
*
|
|
2935
|
+
* @param distinctId - The user's distinct ID
|
|
2936
|
+
* @param options - Optional configuration for flag evaluation
|
|
2937
|
+
* @returns Promise that resolves to flags and payloads
|
|
2938
|
+
*/
|
|
2395
2939
|
async getAllFlagsAndPayloads(distinctId, options) {
|
|
2396
2940
|
const {
|
|
2397
2941
|
groups,
|
|
@@ -2435,6 +2979,41 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2435
2979
|
featureFlagPayloads
|
|
2436
2980
|
};
|
|
2437
2981
|
}
|
|
2982
|
+
/**
|
|
2983
|
+
* Create or update a group and its properties.
|
|
2984
|
+
*
|
|
2985
|
+
* @example
|
|
2986
|
+
* ```ts
|
|
2987
|
+
* // Create a company group
|
|
2988
|
+
* client.groupIdentify({
|
|
2989
|
+
* groupType: 'company',
|
|
2990
|
+
* groupKey: 'acme-corp',
|
|
2991
|
+
* properties: {
|
|
2992
|
+
* name: 'Acme Corporation',
|
|
2993
|
+
* industry: 'Technology',
|
|
2994
|
+
* employee_count: 500
|
|
2995
|
+
* },
|
|
2996
|
+
* distinctId: 'user_123'
|
|
2997
|
+
* })
|
|
2998
|
+
* ```
|
|
2999
|
+
*
|
|
3000
|
+
* @example
|
|
3001
|
+
* ```ts
|
|
3002
|
+
* // Update organization properties
|
|
3003
|
+
* client.groupIdentify({
|
|
3004
|
+
* groupType: 'organization',
|
|
3005
|
+
* groupKey: 'org-456',
|
|
3006
|
+
* properties: {
|
|
3007
|
+
* plan: 'enterprise',
|
|
3008
|
+
* region: 'US-West'
|
|
3009
|
+
* }
|
|
3010
|
+
* })
|
|
3011
|
+
* ```
|
|
3012
|
+
*
|
|
3013
|
+
* {@label Identification}
|
|
3014
|
+
*
|
|
3015
|
+
* @param data - The group identify data
|
|
3016
|
+
*/
|
|
2438
3017
|
groupIdentify({
|
|
2439
3018
|
groupType,
|
|
2440
3019
|
groupKey,
|
|
@@ -2447,12 +3026,49 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2447
3026
|
}, distinctId);
|
|
2448
3027
|
}
|
|
2449
3028
|
/**
|
|
2450
|
-
*
|
|
2451
|
-
*
|
|
3029
|
+
* Reload feature flag definitions from the server for local evaluation.
|
|
3030
|
+
*
|
|
3031
|
+
* @example
|
|
3032
|
+
* ```ts
|
|
3033
|
+
* // Force reload of feature flags
|
|
3034
|
+
* await client.reloadFeatureFlags()
|
|
3035
|
+
* console.log('Feature flags reloaded')
|
|
3036
|
+
* ```
|
|
3037
|
+
*
|
|
3038
|
+
* @example
|
|
3039
|
+
* ```ts
|
|
3040
|
+
* // Reload before checking a specific flag
|
|
3041
|
+
* await client.reloadFeatureFlags()
|
|
3042
|
+
* const flag = await client.getFeatureFlag('flag-key', 'user_123')
|
|
3043
|
+
* ```
|
|
3044
|
+
*
|
|
3045
|
+
* {@label Feature flags}
|
|
3046
|
+
*
|
|
3047
|
+
* @returns Promise that resolves when flags are reloaded
|
|
2452
3048
|
*/
|
|
2453
3049
|
async reloadFeatureFlags() {
|
|
2454
3050
|
await this.featureFlagsPoller?.loadFeatureFlags(true);
|
|
2455
3051
|
}
|
|
3052
|
+
/**
|
|
3053
|
+
* Shutdown the PostHog client gracefully.
|
|
3054
|
+
*
|
|
3055
|
+
* @example
|
|
3056
|
+
* ```ts
|
|
3057
|
+
* // Shutdown with default timeout
|
|
3058
|
+
* await client._shutdown()
|
|
3059
|
+
* ```
|
|
3060
|
+
*
|
|
3061
|
+
* @example
|
|
3062
|
+
* ```ts
|
|
3063
|
+
* // Shutdown with custom timeout
|
|
3064
|
+
* await client._shutdown(5000) // 5 seconds
|
|
3065
|
+
* ```
|
|
3066
|
+
*
|
|
3067
|
+
* {@label Shutdown}
|
|
3068
|
+
*
|
|
3069
|
+
* @param shutdownTimeoutMs - Timeout in milliseconds for shutdown
|
|
3070
|
+
* @returns Promise that resolves when shutdown is complete
|
|
3071
|
+
*/
|
|
2456
3072
|
async _shutdown(shutdownTimeoutMs) {
|
|
2457
3073
|
this.featureFlagsPoller?.stopPoller();
|
|
2458
3074
|
this.errorTracking.shutdown();
|
|
@@ -2581,12 +3197,81 @@ class PostHogBackendClient extends PostHogCoreStateless {
|
|
|
2581
3197
|
allGroupProperties
|
|
2582
3198
|
};
|
|
2583
3199
|
}
|
|
3200
|
+
/**
|
|
3201
|
+
* Capture an error exception as an event.
|
|
3202
|
+
*
|
|
3203
|
+
* @example
|
|
3204
|
+
* ```ts
|
|
3205
|
+
* // Capture an error with user ID
|
|
3206
|
+
* try {
|
|
3207
|
+
* // Some risky operation
|
|
3208
|
+
* riskyOperation()
|
|
3209
|
+
* } catch (error) {
|
|
3210
|
+
* client.captureException(error, 'user_123')
|
|
3211
|
+
* }
|
|
3212
|
+
* ```
|
|
3213
|
+
*
|
|
3214
|
+
* @example
|
|
3215
|
+
* ```ts
|
|
3216
|
+
* // Capture with additional properties
|
|
3217
|
+
* try {
|
|
3218
|
+
* apiCall()
|
|
3219
|
+
* } catch (error) {
|
|
3220
|
+
* client.captureException(error, 'user_123', {
|
|
3221
|
+
* endpoint: '/api/users',
|
|
3222
|
+
* method: 'POST',
|
|
3223
|
+
* status_code: 500
|
|
3224
|
+
* })
|
|
3225
|
+
* }
|
|
3226
|
+
* ```
|
|
3227
|
+
*
|
|
3228
|
+
* {@label Error tracking}
|
|
3229
|
+
*
|
|
3230
|
+
* @param error - The error to capture
|
|
3231
|
+
* @param distinctId - Optional user distinct ID
|
|
3232
|
+
* @param additionalProperties - Optional additional properties to include
|
|
3233
|
+
*/
|
|
2584
3234
|
captureException(error, distinctId, additionalProperties) {
|
|
2585
3235
|
const syntheticException = new Error('PostHog syntheticException');
|
|
2586
3236
|
this.addPendingPromise(ErrorTracking.buildEventMessage(error, {
|
|
2587
3237
|
syntheticException
|
|
2588
3238
|
}, distinctId, additionalProperties).then(msg => this.capture(msg)));
|
|
2589
3239
|
}
|
|
3240
|
+
/**
|
|
3241
|
+
* Capture an error exception as an event immediately (synchronously).
|
|
3242
|
+
*
|
|
3243
|
+
* @example
|
|
3244
|
+
* ```ts
|
|
3245
|
+
* // Capture an error immediately with user ID
|
|
3246
|
+
* try {
|
|
3247
|
+
* // Some risky operation
|
|
3248
|
+
* riskyOperation()
|
|
3249
|
+
* } catch (error) {
|
|
3250
|
+
* await client.captureExceptionImmediate(error, 'user_123')
|
|
3251
|
+
* }
|
|
3252
|
+
* ```
|
|
3253
|
+
*
|
|
3254
|
+
* @example
|
|
3255
|
+
* ```ts
|
|
3256
|
+
* // Capture with additional properties
|
|
3257
|
+
* try {
|
|
3258
|
+
* apiCall()
|
|
3259
|
+
* } catch (error) {
|
|
3260
|
+
* await client.captureExceptionImmediate(error, 'user_123', {
|
|
3261
|
+
* endpoint: '/api/users',
|
|
3262
|
+
* method: 'POST',
|
|
3263
|
+
* status_code: 500
|
|
3264
|
+
* })
|
|
3265
|
+
* }
|
|
3266
|
+
* ```
|
|
3267
|
+
*
|
|
3268
|
+
* {@label Error tracking}
|
|
3269
|
+
*
|
|
3270
|
+
* @param error - The error to capture
|
|
3271
|
+
* @param distinctId - Optional user distinct ID
|
|
3272
|
+
* @param additionalProperties - Optional additional properties to include
|
|
3273
|
+
* @returns Promise that resolves when the error is captured
|
|
3274
|
+
*/
|
|
2590
3275
|
async captureExceptionImmediate(error, distinctId, additionalProperties) {
|
|
2591
3276
|
const syntheticException = new Error('PostHog syntheticException');
|
|
2592
3277
|
this.addPendingPromise(ErrorTracking.buildEventMessage(error, {
|