posthog-js
Version:
Posthog-js allows you to automatically capture usage and send events to PostHog.
132 lines • 7.38 kB
JavaScript
;
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 __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BrowserSurveys = void 0;
exports.getSurveyRenderContext = getSurveyRenderContext;
var general_utils_1 = require("@posthog/browser-common/utils/general-utils");
var browser_common_1 = require("@posthog/browser-common");
var surveys_1 = require("@posthog/browser-common/surveys");
var globals_1 = require("./utils/globals");
var browser_client_1 = require("./extensions/browser-client");
var BrowserSurveysConfigSource = /** @class */ (function () {
function BrowserSurveysConfigSource(_instance) {
this._instance = _instance;
}
BrowserSurveysConfigSource.prototype.get = function () {
var _a, _b, _c, _d, _e;
var config = (_a = this._instance) === null || _a === void 0 ? void 0 : _a.config;
return {
disableSurveys: !!(config === null || config === void 0 ? void 0 : config.disable_surveys),
cookielessMode: !!(config === null || config === void 0 ? void 0 : config.cookieless_mode),
advancedEnableSurveys: !!(config === null || config === void 0 ? void 0 : config.advanced_enable_surveys),
requestTimeoutMs: (_b = config === null || config === void 0 ? void 0 : config.surveys_request_timeout_ms) !== null && _b !== void 0 ? _b : 10000,
prefillFromUrl: !!((_c = config === null || config === void 0 ? void 0 : config.surveys) === null || _c === void 0 ? void 0 : _c.prefillFromUrl),
automaticDisplay: !(config === null || config === void 0 ? void 0 : config.disable_surveys_automatic_display),
featureFlagEvaluation: !(config === null || config === void 0 ? void 0 : config.advanced_disable_feature_flags),
overrideLanguage: config === null || config === void 0 ? void 0 : config.override_display_language,
prepareStylesheet: config === null || config === void 0 ? void 0 : config.prepare_external_dependency_stylesheet,
get_current_url: config === null || config === void 0 ? void 0 : config.get_current_url,
uiHost: (_e = (_d = this._instance) === null || _d === void 0 ? void 0 : _d.requestRouter) === null || _e === void 0 ? void 0 : _e.endpointFor('ui', ''),
};
};
BrowserSurveysConfigSource.prototype.getExtensions = function () {
var extensions = globals_1.assignableWindow === null || globals_1.assignableWindow === void 0 ? void 0 : globals_1.assignableWindow.__PosthogExtensions__;
if (!extensions || !this._instance)
return;
var instance = this._instance;
var generateSurveys = extensions.generateSurveys, loadExternalDependency = extensions.loadExternalDependency;
return {
generateSurveys: generateSurveys
? function (isSurveysEnabled) { return generateSurveys(instance, isSurveysEnabled); }
: undefined,
loadExternalDependency: loadExternalDependency
? function (callback) { return loadExternalDependency(instance, 'surveys', callback); }
: undefined,
};
};
BrowserSurveysConfigSource.prototype.onMatchingConditionsChanged = function (callback) {
var _a, _b;
var unsubscribeCapture = (_a = this._instance) === null || _a === void 0 ? void 0 : _a._addCaptureHook(function (event) {
// Capture hooks run from `eventCaptured`, after `PostHog.capture` has applied
// survey seen-state for dismissal/submission lifecycle events. Re-evaluate here
// so untargeted surveys are removed immediately as well as event/action surveys.
if (event === '$pageview' ||
event === browser_common_1.SurveyEventName.SHOWN ||
event === browser_common_1.SurveyEventName.DISMISSED ||
event === browser_common_1.SurveyEventName.SENT) {
callback();
}
});
// Rendering updates cooldown state even when shown-event telemetry is suppressed.
var onShown = function () { return callback(); };
(0, general_utils_1.addEventListener)(globals_1.window, 'PHSurveyShown', onShown);
// onFeatureFlags may synchronously deliver its cached value while registering.
// The subscription establishes its own initial value after these hooks are attached.
var listening = false;
var unsubscribeFlags = (_b = this._instance) === null || _b === void 0 ? void 0 : _b.onFeatureFlags(function () {
if (listening) {
callback();
}
});
listening = true;
return function () {
listening = false;
unsubscribeCapture === null || unsubscribeCapture === void 0 ? void 0 : unsubscribeCapture();
unsubscribeFlags === null || unsubscribeFlags === void 0 ? void 0 : unsubscribeFlags();
globals_1.window === null || globals_1.window === void 0 ? void 0 : globals_1.window.removeEventListener('PHSurveyShown', onShown);
};
};
return BrowserSurveysConfigSource;
}());
/** Browser-v1 configuration and lazy-loading adapter for the shared surveys extension. */
var BrowserSurveys = /** @class */ (function (_super) {
__extends(BrowserSurveys, _super);
function BrowserSurveys(instance) {
return _super.call(this, new BrowserSurveysConfigSource(instance)) || this;
}
return BrowserSurveys;
}(surveys_1.PostHogSurveys));
exports.BrowserSurveys = BrowserSurveys;
var compatibilityContexts = new WeakMap();
/** Called by the lazy bundle and preview entrypoint, never as a modern pre-setup fallback. */
function getSurveyRenderContext(instance) {
var _a;
var config = new BrowserSurveysConfigSource(instance);
if (!instance)
return {
get config() {
return config.get();
},
};
if ((_a = instance.surveys) === null || _a === void 0 ? void 0 : _a.getRenderContext)
return instance.surveys.getRenderContext();
var context = compatibilityContexts.get(instance);
if (!context) {
// Older posthog-js cores can load a newer surveys bundle. Adapt their existing
// instances for lookup only; the core retains ownership of setup and disposal.
context = {
client: new browser_client_1.BrowserClientAdapter(instance),
get config() {
return config.get();
},
surveys: instance.surveys,
};
compatibilityContexts.set(instance, context);
}
return context;
}
//# sourceMappingURL=browser-surveys.js.map