Skip to content

Commit bf947b2

Browse files
authoredNov 13, 2019
updating fullstory module and adding rollup-based build (#1)
* updating fullstory module and adding rollup-based bulid * exporting FullStory so it can be initialized
1 parent 9e144f4 commit bf947b2

9 files changed

+524
-110
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
dist

‎package.json

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
"name": "@sentry/fullstory",
33
"version": "0.0.1",
44
"description": "The Sentry-FullStory integration creates a link from the Sentry Error to the FullStory replay. It also creates a link from the FullStory event to the Sentry error.",
5-
"main": "src/index.js",
5+
"main": "./dist/index.js",
6+
"module": "./dist/index.js",
67
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "echo \"Error: no test specified\" && exit 1",
9+
"clean": "rimraf dist",
10+
"build": "npm run clean && rollup -c"
811
},
12+
"files": [
13+
"dist",
14+
"src"
15+
],
916
"repository": {
1017
"type": "git",
1118
"url": "git+https://github.com/getsentry/sentry-fullstory.git"
@@ -18,5 +25,12 @@
1825
"homepage": "https://github.com/getsentry/sentry-fullstory#readme",
1926
"peerDependencies": {
2027
"@sentry/browser": ">=4.0.0"
28+
},
29+
"dependencies": {},
30+
"devDependencies": {
31+
"@babel/core": "^7.7.2",
32+
"rimraf": "^3.0.0",
33+
"rollup": "^1.26.3",
34+
"rollup-plugin-babel": "^4.3.3"
2135
}
2236
}

‎rollup.config.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import babel from 'rollup-plugin-babel';
2+
import pkg from './package.json';
3+
4+
export default [
5+
{
6+
input: 'src/index.js',
7+
output: [{ file: pkg.main, format: 'esm' }],
8+
plugins: [
9+
babel({
10+
exclude: ['node_modules/**']
11+
})
12+
]
13+
}
14+
];

‎src/FullStoryWrapper.js

-63
This file was deleted.

‎src/SentryFullStory.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Sentry from '@sentry/browser';
22

3-
import * as FullStory from './FullStoryWrapper';
3+
// NOTE: this will be a package hosted on NPM soon
4+
import FullStory from './fullstory';
45

56
/**
67
* This integration creates a link from the Sentry Error to the FullStory replay.

‎src/fullstory.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import snippet from './snippet';
2+
3+
const fs = () => window[window._fs_namespace];
4+
5+
const ensureSnippetLoaded = () => {
6+
const snippetLoaded = !!fs();
7+
if (!snippetLoaded) {
8+
throw Error('FullStory is not loaded, please ensure the init function is invoked before calling FullStory API functions');
9+
}
10+
};
11+
12+
const hasFullStoryWithFunction = (...testNames) => {
13+
ensureSnippetLoaded();
14+
return testNames.every(current => fs()[current]);
15+
};
16+
17+
const wrapFunction = name => (...args) => {
18+
if (hasFullStoryWithFunction(name)) {
19+
return fs()[name](...args);
20+
}
21+
console.warn(`FS.${name} not ready`); // eslint-disable-line no-console
22+
return null;
23+
};
24+
25+
const wrappedFS = ['event', 'log', 'getCurrentSessionURL', 'identify', 'setUserVars', 'consent', 'shutdown', 'restart'].reduce((acc, current) => {
26+
acc[current] = wrapFunction(current);
27+
return acc;
28+
}, {});
29+
30+
const init = (options) => {
31+
if (fs()) {
32+
// eslint-disable-next-line no-console
33+
console.warn('The FullStory snippet has already been defined elsewhere (likely in the <head> element)');
34+
return;
35+
}
36+
37+
snippet(options);
38+
};
39+
40+
const initOnce = (fn, message) => (...args) => {
41+
if (window._fs_initialized) {
42+
// eslint-disable-next-line no-console
43+
if (message) console.warn(message);
44+
return;
45+
}
46+
fn(...args);
47+
window._fs_initialized = true;
48+
};
49+
50+
wrappedFS.init = initOnce(init, 'FullStory init has already been called once, additional invocations are ignored');
51+
wrappedFS.anonymize = () => wrappedFS.identify(false);
52+
53+
export default wrappedFS;

‎src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import SentryFullStory from './SentryFullStory';
2+
import FullStory from './fullstory';
23

34
export default SentryFullStory;
5+
export { FullStory };

‎src/snippet.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* eslint-disable */
2+
const snippet = ({
3+
orgId,
4+
namespace = 'FS',
5+
debug = false,
6+
host = 'fullstory.com',
7+
script = 'edge.fullstory.com/s/fs.js'
8+
}) => {
9+
if (!orgId) {
10+
throw new Error('FullStory orgId is a required parameter');
11+
}
12+
/* begin FullStory snippet */
13+
window._fs_debug = debug;
14+
window._fs_host = host;
15+
window._fs_script = script;
16+
window._fs_org = orgId;
17+
window._fs_namespace = namespace;
18+
(function(m, n, e, t, l, o, g, y) {
19+
if (e in m) {
20+
if (m.console && m.console.log) {
21+
m.console.log(
22+
'FullStory namespace conflict. Please set window["_fs_namespace"].'
23+
);
24+
}
25+
return;
26+
}
27+
g = m[e] = function(a, b, s) {
28+
g.q ? g.q.push([a, b, s]) : g._api(a, b, s);
29+
};
30+
g.q = [];
31+
o = n.createElement(t);
32+
o.async = 1;
33+
o.crossOrigin = 'anonymous';
34+
o.src = 'https://' + m._fs_script;
35+
y = n.getElementsByTagName(t)[0];
36+
y.parentNode.insertBefore(o, y);
37+
g.identify = function(i, v, s) {
38+
g(l, { uid: i }, s);
39+
if (v) g(l, v, s);
40+
};
41+
g.setUserVars = function(v, s) {
42+
g(l, v, s);
43+
};
44+
g.event = function(i, v, s) {
45+
g('event', { n: i, p: v }, s);
46+
};
47+
g.shutdown = function() {
48+
g('rec', !1);
49+
};
50+
g.restart = function() {
51+
g('rec', !0);
52+
};
53+
g.log = function(a, b) {
54+
g('log', [a, b]);
55+
};
56+
g.consent = function(a) {
57+
g('consent', !arguments.length || a);
58+
};
59+
g.identifyAccount = function(i, v) {
60+
o = 'account';
61+
v = v || {};
62+
v.acctId = i;
63+
g(o, v);
64+
};
65+
g.clearUserCookie = function() {};
66+
})(window, document, window['_fs_namespace'], 'script', 'user');
67+
/* end FullStory snippet */
68+
};
69+
70+
export default snippet;

0 commit comments

Comments
 (0)
Please sign in to comment.