|
| 1 | +/* |
| 2 | + * Copyright@React-FullStory (https://github.com/cereallarceny/react-fullstory) |
| 3 | + */ |
| 4 | + |
| 5 | +import React from 'react'; |
| 6 | +import PropTypes from 'prop-types'; |
| 7 | + |
| 8 | +const canUseDOM = !!( |
| 9 | + typeof window !== 'undefined' && |
| 10 | + window.document && |
| 11 | + window.document.createElement |
| 12 | +); |
| 13 | + |
| 14 | +export const getWindowFullStory = () => window[window['_fs_namespace']]; |
| 15 | + |
| 16 | +class FullStory extends React.Component { |
| 17 | + constructor(props) { |
| 18 | + super(props); |
| 19 | + |
| 20 | + window['_fs_debug'] = false; |
| 21 | + window['_fs_host'] = 'fullstory.com'; |
| 22 | + window['_fs_org'] = props.org; |
| 23 | + window['_fs_namespace'] = 'FS'; |
| 24 | + (function(m,n,e,t,l,o,g,y) { |
| 25 | + if (e in m) { |
| 26 | + if(m.console && m.console.log) { |
| 27 | + m.console.log('FullStory namespace conflict. Please set window["_fs_namespace"].'); |
| 28 | + } |
| 29 | + |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + g = m[e]= function(a,b,s) { |
| 34 | + g.q ? g.q.push([a,b,s]) : g._api(a,b,s); |
| 35 | + }; |
| 36 | + g.q=[]; |
| 37 | + o = n.createElement(t); |
| 38 | + o.async = 1; |
| 39 | + o.src = `https://${window._fs_host}/s/fs.js`; |
| 40 | + y = n.getElementsByTagName(t)[0]; |
| 41 | + y.parentNode.insertBefore(o,y); |
| 42 | + g.identify = function(i,v,s) { |
| 43 | + g(l,{ uid:i },s); |
| 44 | + |
| 45 | + if (v) { |
| 46 | + g(l,v,s); |
| 47 | + } |
| 48 | + }; |
| 49 | + g.setUserVars = function(v,s) { |
| 50 | + g(l,v,s); |
| 51 | + }; |
| 52 | + g.event = function(i,v,s) { |
| 53 | + g('event',{ n:i,p:v },s); |
| 54 | + }; |
| 55 | + g.shutdown = function() { |
| 56 | + g("rec",!1); |
| 57 | + }; |
| 58 | + g.restart = function() { |
| 59 | + g("rec",!0); |
| 60 | + }; |
| 61 | + g.consent = function(a) { |
| 62 | + g("consent",!arguments.length||a); |
| 63 | + }; |
| 64 | + g.identifyAccount = function(i,v) { |
| 65 | + o = 'account'; |
| 66 | + v = v||{}; |
| 67 | + v.acctId = i; |
| 68 | + g(o,v); |
| 69 | + }; |
| 70 | + g.clearUserCookie = function() {}; |
| 71 | + })(window, document, window['_fs_namespace'], 'script', 'user'); |
| 72 | + } |
| 73 | + |
| 74 | + shouldComponentUpdate() { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + componentWillUnmount() { |
| 79 | + if (!canUseDOM || !getWindowFullStory()) return false; |
| 80 | + |
| 81 | + getWindowFullStory().shutdown(); |
| 82 | + |
| 83 | + delete getWindowFullStory(); |
| 84 | + } |
| 85 | + |
| 86 | + render() { |
| 87 | + return false; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +FullStory.propTypes = { |
| 92 | + org: PropTypes.string.isRequired, |
| 93 | +}; |
| 94 | + |
| 95 | +export default FullStory; |
0 commit comments