UNPKG

@fullstory/browser

Version:
166 lines (158 loc) 5.86 kB
import { initFS } from '@fullstory/snippet'; /****************************************************************************** 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 */ 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); }; var getFullStory = function () { if (window._fs_namespace) { return window[window._fs_namespace]; } return undefined; }; var ensureSnippetLoaded = function () { var fs = getFullStory(); if (!fs) { throw Error('FullStory is not loaded, please ensure the init function is invoked before calling FullStory API functions'); } return fs; }; var _init = function (inputOptions, readyCallback) { // Make a copy so we can modify `options` if desired. var options = __assign({}, inputOptions); if (getFullStory()) { console.warn('The FullStory snippet has already been defined elsewhere (likely in the <head> element)'); return; } // see README for details on the recordCrossDomainIFrames option if (options.recordCrossDomainIFrames) { window._fs_run_in_iframe = true; } if (options.appHost) { window._fs_app_host = options.appHost; } if (options.assetMapId) { window._fs_asset_map_id = options.assetMapId; } if (options.startCaptureManually) { window._fs_capture_on_startup = false; } // record the contents of this iFrame when embedded in a parent site if (options.recordOnlyThisIFrame) { window._fs_is_outer_script = true; } // Set cookie domain if it was specified. if (options.cookieDomain) { window._fs_cookie_domain = options.cookieDomain; } if (options.debug === true) { if (!options.script) { options.script = 'edge.fullstory.com/s/fs-debug.js'; } else { console.warn('Ignoring `debug = true` because `script` is set'); } } initFS(options); var fs = getFullStory(); if (!fs) { console.warn('Failed to initialize FS snippet'); return; } if (readyCallback) { fs('observe', { type: 'start', callback: readyCallback }); } if (options.devMode === true) { var message = 'FullStory was initialized in devMode and will stop recording'; fs('trackEvent', { name: 'FullStory Dev Mode', properties: { message: message, } }); fs('shutdown'); window._fs_dev_mode = true; console.warn(message); } }; var initOnce = function (message) { return function (inputOptions, readyCallback) { if (window._fs_initialized) { if (message) console.warn(message); return; } _init(inputOptions, readyCallback); window._fs_initialized = true; }; }; var init = initOnce('FullStory init has already been called once, additional invocations are ignored'); // normalize undefined into boolean var isInitialized = function () { return !!window._fs_initialized; }; var hasFullStoryWithFunction = function () { var testNames = []; for (var _i = 0; _i < arguments.length; _i++) { testNames[_i] = arguments[_i]; } var fs = ensureSnippetLoaded(); return testNames.every(function (current) { return fs[current]; }); }; var guard = function (name) { return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } if (window._fs_dev_mode) { var message = "FullStory is in dev mode and is not capturing: ".concat(name, " method not executed"); console.warn(message); return message; } var fs = getFullStory(); if (hasFullStoryWithFunction(name) && fs) { return fs[name].apply(fs, args); } console.warn("FS.".concat(name, " not ready")); return null; }; }; var buildFullStoryShim = function () { var FS = function (operation, options, source) { var fs = ensureSnippetLoaded(); if (window._fs_dev_mode) { var message = "FullStory is in dev mode and is not capturing: ".concat(operation, " not executed"); console.warn(message); return undefined; } return fs(operation, options, source); }; FS.anonymize = guard('anonymize'); FS.consent = guard('consent'); FS.disableConsole = guard('disableConsole'); FS.enableConsole = guard('enableConsole'); FS.event = guard('event'); FS.getCurrentSessionURL = guard('getCurrentSessionURL'); FS.identify = guard('identify'); FS.log = guard('log'); FS.restart = guard('restart'); FS.setUserVars = guard('setUserVars'); FS.setVars = guard('setVars'); FS.shutdown = guard('shutdown'); return FS; }; var FullStory = buildFullStoryShim(); export { FullStory, init, isInitialized };