UNPKG

posthog-js

Version:

Posthog-js allows you to automatically capture usage and send events to PostHog.

152 lines • 6.78 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BrowserSurveys = void 0; var core_1 = require("@posthog/core"); var posthog_surveys_1 = require("./posthog-surveys"); var request_1 = require("./request"); var globals_1 = require("./utils/globals"); var survey_event_receiver_1 = require("./utils/survey-event-receiver"); var InitialSurveysKeyValueStore = /** @class */ (function () { function InitialSurveysKeyValueStore(_instance) { this._instance = _instance; } InitialSurveysKeyValueStore.prototype.initialize = function () { }; InitialSurveysKeyValueStore.prototype.get = function (keyOrKeys) { var e_1, _a; if (typeof keyOrKeys === 'string') { return this._instance.get_property(keyOrKeys); } var values = {}; try { for (var keyOrKeys_1 = __values(keyOrKeys), keyOrKeys_1_1 = keyOrKeys_1.next(); !keyOrKeys_1_1.done; keyOrKeys_1_1 = keyOrKeys_1.next()) { var key = keyOrKeys_1_1.value; var value = this._instance.get_property(key); if (!(0, core_1.isUndefined)(value)) { values[key] = value; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (keyOrKeys_1_1 && !keyOrKeys_1_1.done && (_a = keyOrKeys_1.return)) _a.call(keyOrKeys_1); } finally { if (e_1) throw e_1.error; } } return values; }; InitialSurveysKeyValueStore.prototype.set = function (keyOrValues, value) { var _a; this._instance.register((typeof keyOrValues === 'string' ? (_a = {}, _a[keyOrValues] = value, _a) : keyOrValues)); }; InitialSurveysKeyValueStore.prototype.remove = function (keyOrKeys) { var _this = this; if (typeof keyOrKeys === 'string') { this._instance.unregister(keyOrKeys); return; } keyOrKeys.forEach(function (key) { return _this._instance.unregister(key); }); }; return InitialSurveysKeyValueStore; }()); var createInitialSurveysClientState = function (instance) { return ({ get projectToken() { return instance.config.token; }, kv: new InitialSurveysKeyValueStore(instance), }); }; var BrowserSurveysConfigSource = /** @class */ (function () { function BrowserSurveysConfigSource(_instance) { this._instance = _instance; } BrowserSurveysConfigSource.prototype.get = function () { var config = this._instance.config; return { disableSurveys: config.disable_surveys, cookielessMode: !!config.cookieless_mode, advancedEnableSurveys: config.advanced_enable_surveys, requestTimeoutMs: config.surveys_request_timeout_ms, }; }; BrowserSurveysConfigSource.prototype.isOptedOut = function () { return this._instance.consent.isOptedOut(); }; BrowserSurveysConfigSource.prototype.getExtensions = function () { var _this = this; var extensions = globals_1.assignableWindow === null || globals_1.assignableWindow === void 0 ? void 0 : globals_1.assignableWindow.__PosthogExtensions__; if (!extensions) { return; } var generateSurveys = extensions.generateSurveys, loadExternalDependency = extensions.loadExternalDependency; return { generateSurveys: generateSurveys ? function (isSurveysEnabled) { return generateSurveys(_this._instance, isSurveysEnabled); } : undefined, loadExternalDependency: loadExternalDependency ? function (callback) { return loadExternalDependency(_this._instance, 'surveys', callback); } : undefined, }; }; BrowserSurveysConfigSource.prototype.createEventReceiver = function () { return new survey_event_receiver_1.SurveyEventReceiver(this._instance); }; return BrowserSurveysConfigSource; }()); /** Browser-v1 compatibility wrapper for the shared surveys extension. */ var BrowserSurveys = /** @class */ (function (_super) { __extends(BrowserSurveys, _super); function BrowserSurveys(_instance) { var _this = _super.call(this, new BrowserSurveysConfigSource(_instance), createInitialSurveysClientState(_instance)) || this; _this._instance = _instance; return _this; } BrowserSurveys.prototype._sendSurveysRequest = function (path, init) { var _this = this; var pathWithQuery = init.query ? (0, request_1.extendURLParams)(path, init.query) : path; // eslint-disable-next-line compat/compat -- Shared extension transport is intentionally Promise-based. return new Promise(function (resolve) { var _a; _this._instance._send_request({ method: init.method, url: _this._instance.requestRouter.endpointFor((_a = init.target) !== null && _a !== void 0 ? _a : 'api', pathWithQuery), data: init.body, headers: init.headers, timeout: init.timeoutMs, fireCallbackOnDrop: true, transport: init.transport, compression: init.compression, timestampMode: init.sentAt, callback: resolve, }); }); }; return BrowserSurveys; }(posthog_surveys_1.PostHogSurveys)); exports.BrowserSurveys = BrowserSurveys; //# sourceMappingURL=browser-surveys.js.map