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