posthog-js
Version:
Posthog-js allows you to automatically capture usage and send events to PostHog.
123 lines • 6.61 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.SurveyEventReceiver = void 0;
var surveys_1 = require("@posthog/core/surveys");
var core_1 = require("@posthog/core");
var constants_1 = require("../constants");
var posthog_surveys_types_1 = require("../posthog-surveys-types");
var survey_utils_1 = require("./survey-utils");
var event_receiver_1 = require("./event-receiver");
var SurveyEventReceiver = /** @class */ (function (_super) {
__extends(SurveyEventReceiver, _super);
function SurveyEventReceiver(instance) {
return _super.call(this, instance) || this;
}
SurveyEventReceiver.prototype._getActivatedKey = function () {
return constants_1.SURVEYS_ACTIVATED;
};
SurveyEventReceiver.prototype._getActivatedSessionKey = function () {
return constants_1.SURVEYS_ACTIVATED_SESSION;
};
SurveyEventReceiver.prototype._getActivationTimestampsKey = function () {
return constants_1.SURVEYS_ACTIVATED_TIMESTAMPS;
};
SurveyEventReceiver.prototype._writeActivationTimestamps = function (timestamps) {
var _a;
var _b, _c;
(_c = (_b = this._instance) === null || _b === void 0 ? void 0 : _b.persistence) === null || _c === void 0 ? void 0 : _c.register((_a = {}, _a[constants_1.SURVEYS_ACTIVATED_TIMESTAMPS] = timestamps, _a));
};
SurveyEventReceiver.prototype._clearActivationTimestampsStore = function () {
var _a, _b;
(_b = (_a = this._instance) === null || _a === void 0 ? void 0 : _a.persistence) === null || _b === void 0 ? void 0 : _b.unregister(constants_1.SURVEYS_ACTIVATED_TIMESTAMPS);
};
/**
* A survey with a popup delay must survive navigation so the delay resumes from the recorded
* activation time on the next page instead of restarting from zero. Surveys without a delay
* keep the in-memory arming, so an exit-intent trigger does not surface them on a later page.
*/
SurveyEventReceiver.prototype._shouldPersistArmedActivation = function (itemId) {
var _a;
var survey;
this._getItems(function (surveys) {
survey = surveys.find(function (s) { return s.id === itemId; });
});
var delaySeconds = (_a = survey === null || survey === void 0 ? void 0 : survey.appearance) === null || _a === void 0 ? void 0 : _a.surveyPopupDelaySeconds;
return (0, core_1.isNumber)(delaySeconds) && delaySeconds > 0;
};
SurveyEventReceiver.prototype._getShownEventName = function () {
return posthog_surveys_types_1.SurveyEventName.SHOWN;
};
SurveyEventReceiver.prototype._getItems = function (callback) {
var _a;
(_a = this._instance) === null || _a === void 0 ? void 0 : _a.getSurveys(callback);
};
SurveyEventReceiver.prototype._cancelPendingItem = function (itemId) {
var _a;
(_a = this._instance) === null || _a === void 0 ? void 0 : _a.cancelPendingSurvey(itemId);
};
SurveyEventReceiver.prototype._getLogger = function () {
return survey_utils_1.SURVEY_LOGGER;
};
SurveyEventReceiver.prototype._setActivatedItems = function (eligibleItems) {
var _a;
var _b, _c;
(_c = (_b = this._instance) === null || _b === void 0 ? void 0 : _b.persistence) === null || _c === void 0 ? void 0 : _c.register((_a = {}, _a[constants_1.SURVEYS_ACTIVATED] = eligibleItems, _a));
};
SurveyEventReceiver.prototype._setActivatedSession = function (sessionId) {
var _a;
var _b, _c;
(_c = (_b = this._instance) === null || _b === void 0 ? void 0 : _b.persistence) === null || _c === void 0 ? void 0 : _c.register((_a = {}, _a[constants_1.SURVEYS_ACTIVATED_SESSION] = sessionId, _a));
};
SurveyEventReceiver.prototype._clearActivatedSession = function () {
var _a, _b;
(_b = (_a = this._instance) === null || _a === void 0 ? void 0 : _a.persistence) === null || _b === void 0 ? void 0 : _b.unregister(constants_1.SURVEYS_ACTIVATED_SESSION);
};
SurveyEventReceiver.prototype._isItemPermanentlyIneligible = function () {
// Surveys have complex eligibility rules checked at display time
// For now, we don't filter at activation time
return false;
};
SurveyEventReceiver.prototype._activationOutcome = function (event, itemId) {
var survey;
this._getItems(function (surveys) {
survey = surveys.find(function (s) { return s.id === itemId; });
});
// A repeatable survey (or one we can't resolve yet) shows once per trigger, so it's consumed
// when shown. A non-repeatable survey is instead promoted to persistence on shown — so it
// survives a reload — and only consumed once the user dismisses or answers it.
var consumedOnShown = !survey || (0, surveys_1.canSurveyActivateRepeatedly)(survey);
if (consumedOnShown) {
return event === posthog_surveys_types_1.SurveyEventName.SHOWN ? 'consume' : 'ignore';
}
if (event === posthog_surveys_types_1.SurveyEventName.SHOWN) {
return 'persist';
}
return event === posthog_surveys_types_1.SurveyEventName.DISMISSED || event === posthog_surveys_types_1.SurveyEventName.SENT ? 'consume' : 'ignore';
};
// Backward compatibility - keep getSurveys() as alias for getActivatedIds()
SurveyEventReceiver.prototype.getSurveys = function () {
return this.getActivatedIds();
};
// Backward compatibility - keep getEventToSurveys() as alias
SurveyEventReceiver.prototype.getEventToSurveys = function () {
return this.getEventToItemsMap();
};
return SurveyEventReceiver;
}(event_receiver_1.EventReceiver));
exports.SurveyEventReceiver = SurveyEventReceiver;
//# sourceMappingURL=survey-event-receiver.js.map