@ciridae/telemetry 0.1.0 → 0.2.1

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @ciridae/telemetry
2
2
 
3
- Shared PostHog analytics, identity, replay, and privacy lifecycle for Ciridae frontends.
3
+ Shared PostHog analytics, feature flags, errors, replay, identity, and privacy lifecycle for Ciridae frontends.
4
4
 
5
5
  Install from npm:
6
6
 
@@ -14,5 +14,10 @@ Create one telemetry facade and pass it to the optional React integration:
14
14
 
15
15
  ```ts
16
16
  import { createCiridaeTelemetry } from "@ciridae/telemetry/client";
17
- import { CiridaeTelemetryProvider } from "@ciridae/telemetry/react";
17
+ import { CiridaeTelemetryProvider, useFeatureFlag } from "@ciridae/telemetry/react";
18
+
19
+ const telemetry = createCiridaeTelemetry({ posthogApiKey: "phc_..." });
20
+ telemetry.captureException(new Error("Checkout failed"));
21
+ telemetry.setGroup("organization", "org_123");
22
+ telemetry.getSessionReplayUrl({ withTimestamp: true });
18
23
  ```
@@ -4,6 +4,11 @@ export type TelemetryUser = {
4
4
  email?: string | null;
5
5
  emailVerified?: boolean;
6
6
  };
7
+ export type FeatureFlagValue = boolean | string;
8
+ export type SessionReplayUrlOptions = {
9
+ withTimestamp?: boolean;
10
+ timestampLookBack?: number;
11
+ };
7
12
  export type CiridaeTelemetryConfiguration = {
8
13
  posthogApiKey: string;
9
14
  posthogApiHost?: string;
@@ -19,5 +24,20 @@ export type CiridaeTelemetry = {
19
24
  identify(user: TelemetryUser): void;
20
25
  clearIdentity(): void;
21
26
  capture(eventName: string, properties?: Record<string, unknown>): void;
27
+ getFeatureFlag(key: string): FeatureFlagValue | undefined;
28
+ getFeatureFlagPayload(key: string): unknown;
29
+ subscribeToFeatureFlags(subscriber: () => void): () => void;
30
+ reloadFeatureFlags(): void;
31
+ /** Context must not contain credentials, private payloads, prompts, or raw user-authored text. */
32
+ captureException(error: unknown, context?: Record<string, unknown>): void;
33
+ /** Use a stable step name and non-sensitive context. */
34
+ addExceptionStep(stepName: string, context?: Record<string, unknown>): void;
35
+ /** Group properties must be stable, non-sensitive analytics dimensions. */
36
+ setGroup(groupType: string, groupId: string, properties?: Record<string, unknown>): void;
37
+ clearGroups(): void;
38
+ getSessionReplayUrl(options?: SessionReplayUrlOptions): string | null;
39
+ startSessionRecording(): void;
40
+ stopSessionRecording(): void;
41
+ isSessionRecording(): boolean;
22
42
  };
23
43
  export declare function createCiridaeTelemetry(configuration: CiridaeTelemetryConfiguration): CiridaeTelemetry;
@@ -9,6 +9,10 @@ export function createCiridaeTelemetry(configuration) {
9
9
  let initialized = false;
10
10
  let identifiedUser = null;
11
11
  const initializationSubscribers = new Set();
12
+ function subscribeToInitialization(subscriber) {
13
+ initializationSubscribers.add(subscriber);
14
+ return () => initializationSubscribers.delete(subscriber);
15
+ }
12
16
  function resetPostHog() {
13
17
  if (!initialized)
14
18
  return;
@@ -19,6 +23,7 @@ export function createCiridaeTelemetry(configuration) {
19
23
  console.error("Error resetting PostHog:", error);
20
24
  }
21
25
  }
26
+ const productCapabilities = createPostHogProductCapabilities(() => initialized, subscribeToInitialization);
22
27
  return {
23
28
  initialize(pathname) {
24
29
  if (initialized || isSensitiveTelemetryPath(pathname))
@@ -39,8 +44,7 @@ export function createCiridaeTelemetry(configuration) {
39
44
  return initialized;
40
45
  },
41
46
  subscribeToInitialization(subscriber) {
42
- initializationSubscribers.add(subscriber);
43
- return () => initializationSubscribers.delete(subscriber);
47
+ return subscribeToInitialization(subscriber);
44
48
  },
45
49
  identify(user) {
46
50
  if (!initialized || sameUser(identifiedUser, user))
@@ -67,6 +71,70 @@ export function createCiridaeTelemetry(configuration) {
67
71
  return;
68
72
  posthog.capture(eventName, properties);
69
73
  },
74
+ ...productCapabilities,
75
+ };
76
+ }
77
+ function createPostHogProductCapabilities(isInitialized, subscribeToInitialization) {
78
+ return {
79
+ getFeatureFlag(key) {
80
+ if (!isInitialized())
81
+ return undefined;
82
+ return posthog.getFeatureFlag(key);
83
+ },
84
+ getFeatureFlagPayload(key) {
85
+ if (!isInitialized())
86
+ return undefined;
87
+ return posthog.getFeatureFlagResult(key)?.payload;
88
+ },
89
+ subscribeToFeatureFlags(subscriber) {
90
+ let unsubscribeFromFeatureFlags = () => undefined;
91
+ const attachSubscriber = () => {
92
+ unsubscribeFromFeatureFlags = posthog.onFeatureFlags(() => subscriber());
93
+ };
94
+ if (isInitialized())
95
+ attachSubscriber();
96
+ const unsubscribeFromInitialization = subscribeToInitialization(attachSubscriber);
97
+ return () => {
98
+ unsubscribeFromInitialization();
99
+ unsubscribeFromFeatureFlags();
100
+ };
101
+ },
102
+ reloadFeatureFlags() {
103
+ if (isInitialized())
104
+ posthog.reloadFeatureFlags();
105
+ },
106
+ captureException(error, context) {
107
+ if (isInitialized())
108
+ posthog.captureException(error, context);
109
+ },
110
+ addExceptionStep(stepName, context) {
111
+ if (isInitialized())
112
+ posthog.addExceptionStep(stepName, context);
113
+ },
114
+ setGroup(groupType, groupId, properties) {
115
+ if (isInitialized())
116
+ posthog.group(groupType, groupId, properties);
117
+ },
118
+ clearGroups() {
119
+ if (isInitialized())
120
+ posthog.resetGroups();
121
+ },
122
+ getSessionReplayUrl(options) {
123
+ if (!isInitialized())
124
+ return null;
125
+ return posthog.get_session_replay_url(options) || null;
126
+ },
127
+ startSessionRecording() {
128
+ if (isInitialized())
129
+ posthog.startSessionRecording();
130
+ },
131
+ stopSessionRecording() {
132
+ if (isInitialized())
133
+ posthog.stopSessionRecording();
134
+ },
135
+ isSessionRecording() {
136
+ return isInitialized() && posthog.sessionRecordingStarted();
137
+ },
70
138
  };
71
139
  }
72
140
  function buildPostHogConfiguration(configuration) {
@@ -1,3 +1,4 @@
1
1
  export * from "../CiridaeTelemetryProvider.js";
2
2
  export * from "../CiridaeTelemetryContext.js";
3
+ export * from "../useFeatureFlag.js";
3
4
  export type { CiridaeTelemetry, TelemetryUser } from "../createCiridaeTelemetry.js";
@@ -1,2 +1,3 @@
1
1
  export * from "../CiridaeTelemetryProvider.js";
2
2
  export * from "../CiridaeTelemetryContext.js";
3
+ export * from "../useFeatureFlag.js";
@@ -0,0 +1,2 @@
1
+ import type { FeatureFlagValue } from "./createCiridaeTelemetry.js";
2
+ export declare function useFeatureFlag(key: string): FeatureFlagValue | undefined;
@@ -0,0 +1,12 @@
1
+ import { useEffect, useState } from "react";
2
+ import { useCiridaeTelemetry } from "./CiridaeTelemetryContext.js";
3
+ export function useFeatureFlag(key) {
4
+ const telemetry = useCiridaeTelemetry();
5
+ const [value, setValue] = useState(() => telemetry.getFeatureFlag(key));
6
+ useEffect(() => {
7
+ const updateValue = () => setValue(telemetry.getFeatureFlag(key));
8
+ updateValue();
9
+ return telemetry.subscribeToFeatureFlags(updateValue);
10
+ }, [key, telemetry]);
11
+ return value;
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ciridae/telemetry",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "engines": {
@@ -36,6 +36,6 @@
36
36
  }
37
37
  },
38
38
  "dependencies": {
39
- "posthog-js": "1.398.2"
39
+ "posthog-js": "1.421.0"
40
40
  }
41
41
  }