|
| 1 | +/** |
| 2 | + * Module dependencies |
| 3 | + */ |
| 4 | +import config from '../../../config/index.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * PostHog client instance (null when not configured) |
| 8 | + * @type {import('posthog-node').PostHog|null} |
| 9 | + */ |
| 10 | +let client = null; |
| 11 | + |
| 12 | +/** |
| 13 | + * Initialise the PostHog client using application config. |
| 14 | + * When `posthog.apiKey` is absent the service stays in no-op mode — |
| 15 | + * every public method silently returns without side-effects so that |
| 16 | + * downstream projects that don't use PostHog are never affected. |
| 17 | + * |
| 18 | + * The `posthog-node` SDK is lazy-loaded (dynamic import) so that |
| 19 | + * applications running on Node versions outside the SDK's engine |
| 20 | + * range never pay the import cost when analytics is unconfigured. |
| 21 | + * @returns {Promise<void>} |
| 22 | + */ |
| 23 | +const init = async () => { |
| 24 | + const { apiKey, host } = config.posthog ?? {}; |
| 25 | + if (!apiKey) return; |
| 26 | + const { PostHog } = await import('posthog-node'); |
| 27 | + client = new PostHog(apiKey, { host: host || 'https://us.i.posthog.com' }); |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * Capture an analytics event. |
| 32 | + * @param {string} distinctId - User or anonymous identifier |
| 33 | + * @param {string} event - Event name |
| 34 | + * @param {Object} [properties] - Additional event properties |
| 35 | + * @param {Object} [groups] - Group identifiers (e.g. { company: orgId }) |
| 36 | + * @returns {void} |
| 37 | + */ |
| 38 | +const track = (distinctId, event, properties, groups) => { |
| 39 | + if (!client) return; |
| 40 | + client.capture({ distinctId, event, properties, groups }); |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * Identify a user with optional properties. |
| 45 | + * @param {string} distinctId - User identifier |
| 46 | + * @param {Object} [properties] - User properties to set |
| 47 | + * @returns {void} |
| 48 | + */ |
| 49 | +const identify = (distinctId, properties) => { |
| 50 | + if (!client) return; |
| 51 | + client.identify({ distinctId, properties }); |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Identify a group (e.g. organisation). |
| 56 | + * @param {string} groupType - Group type (e.g. "company") |
| 57 | + * @param {string} groupKey - Group identifier |
| 58 | + * @param {Object} [properties] - Group properties to set |
| 59 | + * @returns {void} |
| 60 | + */ |
| 61 | +const groupIdentify = (groupType, groupKey, properties) => { |
| 62 | + if (!client) return; |
| 63 | + client.groupIdentify({ groupType, groupKey, properties }); |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * Evaluate a feature flag for the given user. |
| 68 | + * @param {string} flag - Feature flag key |
| 69 | + * @param {string} distinctId - User identifier |
| 70 | + * @param {Object} [options] - Additional options forwarded to PostHog |
| 71 | + * @returns {Promise<string|boolean|undefined>} Flag value, or undefined when not configured |
| 72 | + */ |
| 73 | +const getFeatureFlag = async (flag, distinctId, options) => { |
| 74 | + if (!client) return undefined; |
| 75 | + return client.getFeatureFlag(flag, distinctId, options); |
| 76 | +}; |
| 77 | + |
| 78 | +/** |
| 79 | + * Check whether a feature flag is enabled for the given user. |
| 80 | + * @param {string} flag - Feature flag key |
| 81 | + * @param {string} distinctId - User identifier |
| 82 | + * @param {Object} [options] - Additional options forwarded to PostHog |
| 83 | + * @returns {Promise<boolean|undefined>} true/false, or undefined when not configured |
| 84 | + */ |
| 85 | +const isFeatureEnabled = async (flag, distinctId, options) => { |
| 86 | + if (!client) return undefined; |
| 87 | + return client.isFeatureEnabled(flag, distinctId, options); |
| 88 | +}; |
| 89 | + |
| 90 | +/** |
| 91 | + * Flush pending events and shut down the PostHog client. |
| 92 | + * Safe to call even when the client was never initialised. |
| 93 | + * @returns {Promise<void>} |
| 94 | + */ |
| 95 | +const shutdown = async () => { |
| 96 | + if (!client) return; |
| 97 | + await client.shutdown(); |
| 98 | + client = null; |
| 99 | +}; |
| 100 | + |
| 101 | +export default { |
| 102 | + init, |
| 103 | + track, |
| 104 | + identify, |
| 105 | + groupIdentify, |
| 106 | + getFeatureFlag, |
| 107 | + isFeatureEnabled, |
| 108 | + shutdown, |
| 109 | +}; |
0 commit comments