UNPKG

@capawesome/capacitor-posthog

Version:

Unofficial Capacitor plugin for PostHog SDK.

125 lines (119 loc) 3.74 kB
'use strict'; var core = require('@capacitor/core'); var posthog = require('posthog-js'); const Posthog = core.registerPlugin('Posthog', { web: () => Promise.resolve().then(function () { return web; }).then(m => new m.PosthogWeb()), }); class PosthogWeb extends core.WebPlugin { async alias(options) { posthog.alias(options.alias); } async capture(options) { posthog.capture(options.event, options.properties); } async flush() { this.throwUnimplementedError(); } async getDistinctId() { return { distinctId: posthog.get_distinct_id() }; } async getFeatureFlag(options) { const value = posthog.getFeatureFlag(options.key); return value === undefined ? { value: null } : { value }; } async getFeatureFlagPayload(options) { return { value: posthog.getFeatureFlagPayload(options.key) }; } async group(options) { posthog.group(options.type, options.key, options.groupProperties); } async identify(options) { posthog.identify(options.distinctId, options.userProperties); } async isFeatureEnabled(options) { const enabled = posthog.isFeatureEnabled(options.key); return { enabled: enabled || false }; } async isOptOut() { return { optedOut: posthog.has_opted_out_capturing() }; } async optIn() { posthog.opt_in_capturing(); } async optOut() { posthog.opt_out_capturing(); } async register(options) { posthog.register({ [options.key]: options.value, }); } async reloadFeatureFlags() { posthog.reloadFeatureFlags(); } async reset() { posthog.reset(); } async screen(_options) { this.throwUnimplementedError(); } async setup(options) { const apiHost = this.getApiHost(options.apiHost, options.host); const config = { api_host: apiHost, }; if (options.uiHost) { config.ui_host = options.uiHost; } if (options.optOut) { config.opt_out_capturing_by_default = true; } if (options.cookielessMode) { config.cookieless_mode = options.cookielessMode; } // Configure session recording if enabled if (options.enableSessionReplay) { config.session_recording = { recordCrossOriginIframes: true, }; if (options.sessionReplayConfig) { if (options.sessionReplayConfig.maskAllTextInputs !== undefined) { config.session_recording.maskAllInputs = options.sessionReplayConfig.maskAllTextInputs; } } } posthog.init(options.apiKey, config); } async startSessionRecording() { posthog.startSessionRecording(); } async stopSessionRecording() { posthog.stopSessionRecording(); } async unregister(options) { posthog.unregister(options.key); } throwUnimplementedError() { throw this.unimplemented('Not implemented on web.'); } getApiHost(apiHost, host) { if (apiHost) { if (host && host !== apiHost) { console.warn('[Posthog] Both apiHost and host are set. Using apiHost.'); } return apiHost; } if (host) { console.warn('[Posthog] host is deprecated. Use apiHost instead.'); return host; } return 'https://us.i.posthog.com'; } } var web = /*#__PURE__*/Object.freeze({ __proto__: null, PosthogWeb: PosthogWeb }); exports.Posthog = Posthog; //# sourceMappingURL=plugin.cjs.js.map