UNPKG

@posthog/wizard

Version:

The PostHog wizard helps you to configure your project

84 lines 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.analytics = exports.Analytics = void 0; const posthog_node_1 = require("posthog-node"); const constants_1 = require("../lib/constants"); const uuid_1 = require("uuid"); const debug_1 = require("./debug"); class Analytics { client; tags = {}; distinctId; anonymousId; appName = 'wizard'; constructor() { this.client = new posthog_node_1.PostHog(constants_1.ANALYTICS_POSTHOG_PUBLIC_PROJECT_WRITE_KEY, { host: constants_1.ANALYTICS_HOST_URL, flushAt: 1, flushInterval: 0, enableExceptionAutocapture: true, }); this.tags = { $app_name: this.appName }; this.anonymousId = (0, uuid_1.v4)(); this.distinctId = undefined; } setDistinctId(distinctId) { this.distinctId = distinctId; this.client.alias({ distinctId, alias: this.anonymousId, }); } setTag(key, value) { this.tags[key] = value; } captureException(error, properties = {}) { this.client.captureException(error, this.distinctId ?? this.anonymousId, { team: constants_1.ANALYTICS_TEAM_TAG, ...this.tags, ...properties, }); } capture(eventName, properties) { this.client.capture({ distinctId: this.distinctId ?? this.anonymousId, event: eventName, properties: { ...this.tags, ...properties, }, }); } async getFeatureFlag(flagKey) { try { const distinctId = this.distinctId ?? this.anonymousId; return await this.client.getFeatureFlag(flagKey, distinctId, { sendFeatureFlagEvents: true, personProperties: { $app_name: this.appName, }, }); } catch (error) { (0, debug_1.debug)('Failed to get feature flag:', flagKey, error); return undefined; } } async shutdown(status) { if (Object.keys(this.tags).length === 0) { return; } this.client.capture({ distinctId: this.distinctId ?? this.anonymousId, event: 'setup wizard finished', properties: { status, tags: this.tags, }, }); await this.client.shutdown(); } } exports.Analytics = Analytics; exports.analytics = new Analytics(); //# sourceMappingURL=analytics.js.map