UNPKG

@screeb/sdk-browser

Version:

Screeb's browser sdk.

539 lines (530 loc) 16.1 kB
'use strict'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; var SCREEB_TAG_ENDPOINT = "https://t.screeb.app/tag.js"; var CONSTANTS = { version: "0.0.0-dev" }; var _window = typeof window === "undefined" ? undefined : window; var callScreebCommand = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } if (_window === null || _window === void 0 ? void 0 : _window.$screeb) { return _window.$screeb.apply(_window.$screeb, args); } return Promise.reject("[Screeb] Screeb.load() must be called before any other function."); }; /** * Appends Screeb tag into your dom. * * @param options Screeb module options. * @param options.window If you're running Screeb tag in an iframe, please set the inner window here. * @param options.screebEndpoint Please don't do this. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.load(); * ``` */ var load = function (options) { if (options === void 0) { options = {}; } return new Promise(function (resolve, reject) { var _a, _b, _c, _d, _e; _window = (_a = options.window) !== null && _a !== void 0 ? _a : window; var scriptElement = document.createElement("script"); scriptElement.async = true; scriptElement.src = (_b = options.screebEndpoint) !== null && _b !== void 0 ? _b : SCREEB_TAG_ENDPOINT; scriptElement.addEventListener("load", function () { return resolve(undefined); }); scriptElement.addEventListener("error", reject); if (options.platform) { _window["ScreebConfig"] = { platform: options.platform }; } _window.$screeb = (_c = _window.$screeb) !== null && _c !== void 0 ? _c : function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return new Promise(function (a, b) { var _a; if (_window === null || _window === void 0 ? void 0 : _window.$screeb) { return (_window.$screeb.q = (_a = _window.$screeb.q) !== null && _a !== void 0 ? _a : []).push({ args: args, ko: b, ok: a, v: 1, }); } return 0; }); }; _window.document.head.appendChild(scriptElement); var context = { secondary_sdk_name: (_d = options.sdkName) !== null && _d !== void 0 ? _d : "sdk-browser", secondary_sdk_version: (_e = options.sdkVersion) !== null && _e !== void 0 ? _e : CONSTANTS.version, }; callScreebCommand("client.internal.web", context); }); }; /** * Initializes Screeb tag. * * @param websiteId Your website/channel id. * @param userId The unique identifier of your user. * @param userProperties The properties of your user. * ```text Requirements: * - Property names must be limited to 128 characters * - No more than 1000 attributes * - Supported types for values: string, number, boolean and Date * ``` * @param hooks Hooks to be called when SDK is ready or a survey is showed, started, completed, hidden * or when a question is replied. * * @param language Force a specific language for the tag. eg: 'en'. default: browser language. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.init( * "<your-website-id>", * "<your-user-id>", * { * firstname: '<user-firstname>', * lastname: '<user-lastname>', * plan: '<user-plan>', * last_seen_at: new Date(), * authenticated: true * }, * { * version: "1.0.0", * onReady: (payload) => console.log("Screeb SDK is ready!", payload), * }, * "en" * ); * ``` */ var init = function (websiteId, userId, userProperties, hooks, language) { var identityObject; if (userId || userProperties) { identityObject = { hooks: hooks, identity: { id: userId, properties: userProperties, }, }; } if (language) { identityObject = __assign(__assign({}, identityObject), { language: language }); } return callScreebCommand("init", websiteId, identityObject); }; /** * Checks if Screeb tag has been loaded. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * console.log(Screeb.isLoaded()); // false * Screeb.load(); * console.log(Screeb.isLoaded()); // true * ``` */ var isLoaded = function () { return (_window === null || _window === void 0 ? void 0 : _window.$screeb) && typeof _window.$screeb === "function"; }; /** * Shutdowns current Screeb session. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.close(); * ``` */ var close = function () { return callScreebCommand("close"); }; /** * Prints the actual state information of Screeb tag. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.debug(); * // ******************* SCREEB SESSION DEBUG ********************* * // Screeb channel id: <UUID> * // Screeb channel type: widget * // Screeb respondent id: <UUID> * // Screeb survey id: none * // Screeb response id: none * // * // Screeb current session start: Thu May 04 2023 16:53:49 GMT+0200 (Central European Summer Time) * // Screeb current session last activity: Thu May 04 2023 17:41:30 GMT+0200 (Central European Summer Time) * // * // Screeb targeting engine status: disabled * // Screeb targeting engine: 3 surveys * // * // Detected platform: desktop * // Detected locale: en-GB * // Detected timezone: -120 * // ************************************************************** * ``` */ var debug = function () { return callScreebCommand("debug"); }; /** * Tracks a user event. * * @param eventName The event name. * @param eventProperties The properties of your event. * ```text Requirements: * - Property names must be limited to 128 characters * - No more than 1000 attributes * - Supported types for values: string, number, boolean and Date. * ``` * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.eventTrack( * "Product added to cart", * { * product_name: 'Red bike 2021', * category: 'sport', * color: 'red', * price: 299, * count: 1, * reference: '2CF093TG1', * delivery_method: 'UPS', * user_logged: false, * added_at: new Date(), * } * ); * ``` */ var eventTrack = function (eventName, eventProperties) { return callScreebCommand("event.track", eventName, eventProperties); }; /** * Change the current user identity. * Warning: Running surveys will be closed. * * @param userId The unique identifier of your user. * @param userProperties The properties of your user. * ```text Requirements: * - Property names must be limited to 128 characters * - No more than 1000 attributes * - Supported types for values: string, number, boolean and Date. * ``` * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.identity( * "<your-user-id>", * { * firstname: '<user-firstname>', * lastname: '<user-lastname>', * plan: '<user-plan>', * last_seen_at: new Date(), * authenticated: true * } * ); * ``` */ var identity = function (userId, userProperties) { return callScreebCommand("identity", userId, userProperties); }; /** * Retrieves the current user identity. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * console.log(await Screeb.identityGet()); * // { * // anonymous_id: "<UUID>", * // user_id: "<UUID>", * // session_id: "<UUID>", * // session_start: "2023-05-04T16:30:15.882Z", * // session_end: "2023-05-04T17:02:09.087Z", * // channel_id: "<UUID>", * // is_ready: true, * // } * ``` */ var identityGet = function () { return callScreebCommand("identity.get"); }; /** * Assigns the current user to a group. * * @param groupName * @param groupType * @param groupProperties The properties of your user group. * ```text Requirements: * - Property names must be limited to 128 characters * - No more than 1000 attributes * - Supported types for values: string, number, boolean and Date. * ``` * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.identityGroupAssign( * 'company', * 'Apple', * { * address_line_1: 'Apple Campus', * address_line_2: '1 Infinite Loop', * city: 'Cupertino', * zipcode: 95014, * state: 'California', * country: 'United states', * } * ); * ``` */ var identityGroupAssign = function (groupName, groupType, groupProperties) { return callScreebCommand("identity.group.assign", groupType, groupName, groupProperties); }; /** * Unassigns the current user to a group. * * @param groupName The name of your user group. * @param groupType The type of your user group. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.identityGroupUnassign('company', 'Apple'); * ``` */ var identityGroupUnassign = function (groupName, groupType) { return callScreebCommand("identity.group.unassign", groupType, groupName); }; /** * Adds properties to the current user identity. * * @param userProperties The properties of your user. * ```text Requirements: * - Property names must be limited to 128 characters * - No more than 1000 attributes * - Supported types for values: string, number, boolean and Date. * ``` * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * // Set user properties * Screeb.identityProperties( * { * firstname: '<user-firstname>', * lastname: '<user-lastname>', * plan: '<user-plan>', * last_seen_at: new Date(), * authenticated: true * } * ); * * // Delete user property : set values to null * Screeb.identityProperties( * { * age: null, * company: null, * logged: true, * } * ); * ``` */ var identityProperties = function (userProperties) { return callScreebCommand("identity.properties", userProperties); }; /** * Resets the current user identity. * Warning: This command must be called only once, since it creates a new identity on Screeb side. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.identityReset(); * ``` */ var identityReset = function () { return callScreebCommand("identity.reset"); }; /** * Interrupts a running survey. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.surveyClose(); * ``` */ var surveyClose = function () { return callScreebCommand("survey.close"); }; /** * Starts a survey by its ID. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.surveyStart( * '<UUID>', * '<UUID>', * false, * { * color: "green", * article_id: 42 * }, * { * version: "1.0.0", * onSurveyShowed: (payload) => console.log("Survey showed", payload), * }, * "en" * ); * ``` */ var surveyStart = function (surveyId, distributionId, allowMultipleResponses, hiddenFields, hooks, language) { if (allowMultipleResponses === void 0) { allowMultipleResponses = true; } if (hiddenFields === void 0) { hiddenFields = {}; } return callScreebCommand("survey.start", surveyId, { distribution_id: distributionId, allow_multiple_responses: allowMultipleResponses, language: language, hidden_fields: hiddenFields, hooks: hooks, }); }; /** * Interrupts a running message. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.messageClose(); * ``` */ var messageClose = function () { return callScreebCommand("message.close"); }; /** * Starts a message by its ID. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.messageStart( * '<UUID>', * false, * { * color: "green", * article_id: 42 * }, * { * version: "1.0.0", * onMessageShowed: (payload) => console.log("Message showed", payload), * }, * "en" * ); * ``` */ var messageStart = function (messageId, allowMultipleResponses, hiddenFields, hooks, language) { if (allowMultipleResponses === void 0) { allowMultipleResponses = true; } if (hiddenFields === void 0) { hiddenFields = {}; } return callScreebCommand("message.start", messageId, { allow_multiple_responses: allowMultipleResponses, language: language, hidden_fields: hiddenFields, hooks: hooks, }); }; /** * Forces a targeting check. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * Screeb.targetingCheck(); * ``` */ var targetingCheck = function () { return callScreebCommand("targeting.check"); }; /** * Prints the current state of the targeting engine. * * @example * ```ts * import * as Screeb from "@screeb/sdk-browser"; * * console.log(await Screeb.targetingDebug()); * // targeting ************ SCREEB TARGETING RULES DEBUG ************** * // Disabled surveys are not listed here. * // * // Screeb channel id: <UUID> * // Screeb respondent id: <UUID> * // * // Survey <UUID>: * // https://admin.screeb.app/org/last/survey/<UUID>/share * // * // - Rule of type "Device type (desktop/mobile/tablet)": true 🟢 * // - Rule of type "Multiple display": true 🟢 * // - Rule of type "Capping per time between survey display on current respondent": true 🟢 * // - Rule of type "User event count": false 🔴 * // - Rule of type "Capping per respondent display count": false 🔴 * ``` */ var targetingDebug = function () { return callScreebCommand("targeting.debug"); }; exports.close = close; exports.debug = debug; exports.eventTrack = eventTrack; exports.identity = identity; exports.identityGet = identityGet; exports.identityGroupAssign = identityGroupAssign; exports.identityGroupUnassign = identityGroupUnassign; exports.identityProperties = identityProperties; exports.identityReset = identityReset; exports.init = init; exports.isLoaded = isLoaded; exports.load = load; exports.messageClose = messageClose; exports.messageStart = messageStart; exports.surveyClose = surveyClose; exports.surveyStart = surveyStart; exports.targetingCheck = targetingCheck; exports.targetingDebug = targetingDebug; CONSTANTS.version = '0.3.0'