@mobileaction/ui-modules
Version:
Mobile Action common modules for Vue projects
241 lines (225 loc) • 7.73 kB
JavaScript
import { injectPlugin, validateVueInstall } from '../PluginUtils.js';
/** from: https://stackoverflow.com/a/7616484
*
* @param s string to hash
* @returns {number} hash value of string
*/
function strHash(s) {
if (typeof s !== 'string') {
return 0;
}
let hash = 0;
for (let i = 0; i < s.length; i += 1) {
// bit or converts to 32bit integer
// eslint-disable-next-line no-bitwise
hash = (((hash << 5) - hash) + s.charCodeAt(i)) | 0;
}
return hash;
}
class MaFullStoryCls {
constructor(Vue, { fsParams, domains }) {
if (!domains || !domains.length) {
throw Error('MaFullStory: domains must be an array of at least size 1');
}
if (!fsParams || !fsParams.org) {
throw Error('MaFullStory: fsParams.org must be given');
}
this.Vue = Vue;
Object.defineProperties(this, {
domains: {
value: domains,
writable: false,
enumerable: false,
},
fsParams: {
value: Object.assign({
debug: false,
host: 'fullstory.com',
script: 'edge.fullstory.com/s/fs.js',
org: '',
namespace: 'FS',
}, fsParams),
writable: false,
enumerable: false,
},
loaded: {
value: false,
enumerable: false,
},
});
}
initialize(user, prefix = '') {
// just in case compare hashes for test email
if (!user || strHash(user.username) === -643103943) {
return;
}
if (!this.domains.includes(window.origin)) {
return;
}
Object.keys(this.fsParams).forEach((k) => {
window[`_fs_${ k }`] = this.fsParams[k];
});
const self = this;
// eslint-disable-next-line no-unused-vars
function initFSOld(l) {
const t = 'script';
const ns = window._fs_namespace;
if (ns in window) {
self.Vue.$log.error('FullStory namespace conflict. Please set window["_fs_namespace"].');
return;
}
const g = window[ns] = (a, b) => {
if (g.q) {
g.q.push([a, b]);
} else {
g._api(a, b);
}
};
g.q = [];
const o = document.createElement(t);
o.async = 1;
o.src = `https://${ window._fs_host }/s/fs.js`;
o.onload = () => {
this.loaded = true;
};
const y = document.getElementsByTagName(t)[0];
y.parentNode.insertBefore(o, y);
g.identify = function (i, v) {
g(l, { uid: i });
if (v) {
g(l, v);
}
};
g.setUserVars = function (v) {
g(l, v);
};
g.identifyAccount = function (i, v) {
const o = 'account';
const _v = v || {};
_v.acctId = i;
g(o, _v);
};
g.clearUserCookie = function (cookie) { // noinspection JSCheckFunctionSignatures
if (!cookie || document.cookie.match('fs_uid=[`;`]*`[`;`]*`[`;`]*`')) {
let d = document.domain;
while (1) { // eslint-disable-line no-constant-condition
document.cookie = `fs_uid=;domain=${ d };path=/;expires=${ new Date(0).toUTCString() }`;
const i = d.indexOf('.');
if (i < 0) {
break;
}
d = d.slice(i + 1);
}
}
};
}
window['_fs_debug'] = false;
window['_fs_host'] = 'fullstory.com';
window['_fs_script'] = 'edge.fullstory.com/s/fs.js';
window['_fs_org'] = '3TA9V';
window['_fs_namespace'] = 'FS';
// NOTE: if you update the script please do not forget to set self.loaded = true on script onload
function initFS(ns) {
let t = 'script';
let l = 'user';
if (ns in window) {
if (window.console && window.console.log) {
self.Vue.$log.error('FullStory namespace conflict. Please set window["_fs_namespace"].');
}
return;
}
let g = window[ns] = function (a, b, s) {
g.q ? g.q.push([a, b, s]) : g._api(a, b, s);
};
g.q = [];
let o = document.createElement(t);
o.async = 1;
o.crossOrigin = 'anonymous';
o.onload = () => {
this.loaded = true;
};
o.src = 'https://' + window._fs_script;
let y = document.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';
// eslint-disable-next-line no-param-reassign
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] = window[y];
y = 'fetch';
g._w[y] = window[y];
if (window[y]) {
window[y] = function () {
return g._w[y].apply(this, arguments);
};
}
g._v = '1.3.0';
}
initFS(window['_fs_namespace']);
const fsData = {
displayName: `${ prefix }${ user.username }`,
email: `${ prefix }${ user.username }`,
numberOfApps_int: user.usage.apps,
keywords_int: user.usage.keywords,
pricingPlan_str: user.plan,
createdAt_date: new Date(+user.createdAt * 1000),
};
// you might want to prefix the user depending on their role etc.
const userId = `${ prefix }${ user.userId }`;
window.FS.identify(userId, fsData);
}
fs() {
if (!this.loaded) {
MaFullStory.$log.warn('MaFullStory: FS is not loaded');
}
return window.FS;
}
}
export function MaFullStory(app, opts) {
if (!validateVueInstall(app, MaFullStory, 'MaFullStory')) {
return;
}
const _opts = Object.assign({
domains: [],
fsParams: {},
}, opts);
injectPlugin(app, new MaFullStoryCls(app, _opts), '$maFullStory');
}
export default MaFullStory;