posthog-js
Version:
Posthog-js allows you to automatically capture usage and send events to PostHog.
199 lines • 8.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostHogProductTours = void 0;
var constants_1 = require("./constants");
var logger_1 = require("@posthog/browser-common/utils/logger");
var core_1 = require("@posthog/core");
var globals_1 = require("./utils/globals");
var logger = (0, logger_1.createLogger)('[Product Tours]');
var isProductToursEnabled = function (instance) {
var _a;
if (instance.config.disable_product_tours) {
return false;
}
return !!((_a = instance.persistence) === null || _a === void 0 ? void 0 : _a.get_property(constants_1.PRODUCT_TOURS_ENABLED_SERVER_SIDE));
};
var PostHogProductTours = /** @class */ (function () {
function PostHogProductTours(instance) {
this._productTourManager = null;
this._cachedTours = null;
this._instance = instance;
}
Object.defineProperty(PostHogProductTours.prototype, "_persistence", {
get: function () {
return this._instance.persistence;
},
enumerable: false,
configurable: true
});
PostHogProductTours.prototype.initialize = function () {
this.loadIfEnabled();
};
PostHogProductTours.prototype.onRemoteConfig = function (result) {
var _a;
var _b, _c;
if (!result.ok) {
// Failure behaves like a response without a productTours key.
return;
}
var response = result.config;
if (!('productTours' in response)) {
return;
}
if (this._persistence) {
this._persistence.register((_a = {},
_a[constants_1.PRODUCT_TOURS_ENABLED_SERVER_SIDE] = !!response.productTours,
_a));
}
if (!isProductToursEnabled(this._instance)) {
if (this._productTourManager || !(0, core_1.isNullish)((_b = this._persistence) === null || _b === void 0 ? void 0 : _b.props[constants_1.PRODUCT_TOURS])) {
logger.info('product tours disabled; stopping and clearing cached tours');
}
// a manager started from the previously-persisted enabled flag keeps
// serving tours until told to stop
(_c = this._productTourManager) === null || _c === void 0 ? void 0 : _c.stop();
this._productTourManager = null;
// tours cached while enabled would otherwise ride along in the main
// persistence blob (and every cross-tab storage broadcast) forever
this.clearCache();
return;
}
this.loadIfEnabled();
};
PostHogProductTours.prototype.loadIfEnabled = function () {
var _this = this;
if (this._productTourManager || !isProductToursEnabled(this._instance)) {
return;
}
this._loadScript(function () { return _this._startProductTours(); });
};
PostHogProductTours.prototype._loadScript = function (cb) {
var _a, _b, _c;
if ((_a = globals_1.assignableWindow.__PosthogExtensions__) === null || _a === void 0 ? void 0 : _a.generateProductTours) {
cb();
return;
}
(_c = (_b = globals_1.assignableWindow.__PosthogExtensions__) === null || _b === void 0 ? void 0 : _b.loadExternalDependency) === null || _c === void 0 ? void 0 : _c.call(_b, this._instance, 'product-tours', function (err) {
if (err) {
logger.error('Could not load product tours script', err);
return;
}
cb();
});
};
PostHogProductTours.prototype._startProductTours = function () {
var _a;
if (this._productTourManager || !((_a = globals_1.assignableWindow.__PosthogExtensions__) === null || _a === void 0 ? void 0 : _a.generateProductTours)) {
return;
}
this._productTourManager = globals_1.assignableWindow.__PosthogExtensions__.generateProductTours(this._instance, true);
};
PostHogProductTours.prototype.getProductTours = function (callback, forceReload) {
var _this = this;
if (forceReload === void 0) { forceReload = false; }
if ((0, core_1.isArray)(this._cachedTours) && !forceReload) {
callback(this._cachedTours, { isLoaded: true });
return;
}
var persistence = this._persistence;
if (persistence) {
var storedTours = persistence.props[constants_1.PRODUCT_TOURS];
if ((0, core_1.isArray)(storedTours) && !forceReload) {
this._cachedTours = storedTours;
callback(storedTours, { isLoaded: true });
return;
}
}
this._instance._send_request({
url: this._instance.requestRouter.endpointFor('api', "/api/product_tours/?token=".concat(this._instance.config.token)),
method: 'GET',
timestampMode: 'query',
callback: function (response) {
var _a;
if (!isProductToursEnabled(_this._instance)) {
// a disable can land while this request is in flight; honouring
// the response would re-create the cache the disable just cleared
callback([], { isLoaded: true });
return;
}
var statusCode = response.statusCode;
if (statusCode !== 200 || !response.json) {
var error = "Product Tours API could not be loaded, status: ".concat(statusCode);
if (statusCode === 0) {
if (!response.error) {
logger.warn(error);
}
}
else {
logger.error(error);
}
callback([], { isLoaded: false, error: error });
return;
}
var tours = (0, core_1.isArray)(response.json.product_tours) ? response.json.product_tours : [];
_this._cachedTours = tours;
if (persistence) {
persistence.register((_a = {}, _a[constants_1.PRODUCT_TOURS] = tours, _a));
}
callback(tours, { isLoaded: true });
},
});
};
PostHogProductTours.prototype.getActiveProductTours = function (callback) {
if ((0, core_1.isNullish)(this._productTourManager)) {
callback([], { isLoaded: false, error: 'Product tours not loaded' });
return;
}
this._productTourManager.getActiveProductTours(callback);
};
PostHogProductTours.prototype.showProductTour = function (tourId) {
var _a;
(_a = this._productTourManager) === null || _a === void 0 ? void 0 : _a.showTourById(tourId);
};
// force load product tours extension and render a tour,
// ignoring all display conditions.
PostHogProductTours.prototype.previewTour = function (tour) {
var _this = this;
if (this._productTourManager) {
this._productTourManager.previewTour(tour);
return;
}
this._loadScript(function () {
var _a;
_this._startProductTours();
(_a = _this._productTourManager) === null || _a === void 0 ? void 0 : _a.previewTour(tour);
});
};
PostHogProductTours.prototype.dismissProductTour = function () {
var _a;
(_a = this._productTourManager) === null || _a === void 0 ? void 0 : _a.dismissTour('user_clicked_skip');
};
PostHogProductTours.prototype.nextStep = function () {
var _a;
(_a = this._productTourManager) === null || _a === void 0 ? void 0 : _a.nextStep();
};
PostHogProductTours.prototype.previousStep = function () {
var _a;
(_a = this._productTourManager) === null || _a === void 0 ? void 0 : _a.previousStep();
};
PostHogProductTours.prototype.clearCache = function () {
var _a;
this._cachedTours = null;
(_a = this._persistence) === null || _a === void 0 ? void 0 : _a.unregister(constants_1.PRODUCT_TOURS);
};
PostHogProductTours.prototype.resetTour = function (tourId) {
var _a;
(_a = this._productTourManager) === null || _a === void 0 ? void 0 : _a.resetTour(tourId);
};
PostHogProductTours.prototype.resetAllTours = function () {
var _a;
(_a = this._productTourManager) === null || _a === void 0 ? void 0 : _a.resetAllTours();
};
PostHogProductTours.prototype.cancelPendingTour = function (tourId) {
var _a;
(_a = this._productTourManager) === null || _a === void 0 ? void 0 : _a.cancelPendingTour(tourId);
};
return PostHogProductTours;
}());
exports.PostHogProductTours = PostHogProductTours;
//# sourceMappingURL=posthog-product-tours.js.map