function fullStoryPlugin(pluginConfig = {}) {
  const source = 'analytics'
  return {
    name: 'fullstory',
    config: {
      ...defaultConfig,
      ...pluginConfig
    },
    initialize: ({ config }) => {
      if (!config.org) {
        throw new Error('No org name supplied for fullstory')
      }
      if (!scriptAlreadyLoaded()) {
        // Create script & append to DOM
        let script = document.createElement('script')
        script.type = 'text/javascript'
        script.async = true
        script.crossOrigin = 'anonymous'
        script.src = `https://edge.fullstory.com/s/fs.js`

        /* script.addEventListener('load', () => {
          isLoaded = true
        }) */
        // Catch any errors while loading the script
        script.addEventListener('error', () => {
          throw new Error(`Full story failed to load.`)
        })
        // On next tick, inject the script
        setTimeout(() => {
          let firstScript = document.getElementsByTagName('script')[0]
          firstScript.parentNode.insertBefore(script, firstScript)
        }, 0)
      }

      window._fs_debug = config.debug
      window._fs_host = 'fullstory.com';
      window._fs_script = 'edge.fullstory.com/s/fs.js';
      window._fs_org = config.org;
      window._fs_namespace = 'FS';

      /* eslint-disable */
      ;(function(m,n,e,t,l,o,g,y){
        if (e in m) {if(m.console && m.console.log) { m.console.log('FullStory namespace conflict. Please set window["_fs_namespace"].');} return;}
        g=m[e]=function(a,b,s){g.q?g.q.push([a,b,s]):g._api(a,b,s);};g.q=[];
        o=n.createElement(t);o.async=1;o.crossOrigin='anonymous';o.src='https://'+_fs_script;
        y=n.getElementsByTagName(t)[0];y.parentNode.insertBefore(o,y);
        g.identify=function(i,v,s){g(l,{uid:i},s);if(v)g(l,v,s)};g.setUserVars=function(v,s){g(l,v,s)};g.event=function(i,v,s){g('event',{n:i,p:v},s)};
        g.anonymize=function(){g.identify(!!0)};
        g.shutdown=function(){g("rec",!1)};g.restart=function(){g("rec",!0)};
        g.log = function(a,b){g("log",[a,b])};
        g.consent=function(a){g("consent",!arguments.length||a)};
        g.identifyAccount=function(i,v){o='account';v=v||{};v.acctId=i;g(o,v)};
        g.clearUserCookie=function(){};
        g.setVars=function(n, p){g('setVars',[n,p]);};
        g._w={};y='XMLHttpRequest';g._w[y]=m[y];y='fetch';g._w[y]=m[y];
        if(m[y])m[y]=function(){return g._w[y].apply(this,arguments)};
        g._v="1.3.0";
      })(window,document,window['_fs_namespace'],'script','user');
      /* eslint-enable */
    },
    // https://help.fullstory.com/hc/en-us/articles/360020828113-FS-identify-Identifying-users
    // https://help.fullstory.com/hc/en-us/articles/360020623294-FS-setUserVars-Recording-custom-user-data
    identify: ({ payload, config }) => {
      const { userId, anonymousId, traits } = payload
      if (typeof FS === 'undefined') return false
      const userTraits = formatPayload(traits, true)
      if (userId) {
        FS.identify(userId, userTraits, source)
      } else {
        userTraits.analyticsAnonymousId_str = anonymousId
        FS.setUserVars(userTraits, source)
      }
    },
    /* https://help.fullstory.com/hc/en-us/articles/360020623234 */
    track: ({ payload, options, config }) => {
      if (typeof FS === 'undefined') return false
      const eventData = formatPayload(payload.properties)
      FS.event(payload.event, eventData, source)
    },
    loaded: () => {
      return !!window.FS
    },
  }
}
FullStory Analytics plugin
@link https://getanalytics.io/plugins/fullstory/
@link https://help.fullstory.com/hc/en-us/sections/360003732794-JavaScript-API
@param {object} pluginConfig - Plugin settings
@param {string} pluginConfig.org - FullStory account's `org` ID. The `_fs_org` value in settings.
@return {object} Analytics plugin
@example

fullStoryPlugin({
  org: 'your-org-name'
})