File size: 2,789 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import { getCurrentUser } from '@automattic/calypso-analytics';
import config from '@automattic/calypso-config';
import debug from 'debug';
import isJetpackCloud from 'calypso/lib/jetpack/is-jetpack-cloud';
import { TRACKING_IDS } from './ad-tracking/constants';
import { mayWeTrackByTracker } from './tracker-buckets';
const logRocketDebug = debug( 'calypso:analytics:logrocket' );
let logRocketScriptLoaded = false;
export function mayWeLoadLogRocketScript() {
return config.isEnabled( 'logrocket' ) && mayWeTrackByTracker( 'logrocket' );
}
export function maybeAddLogRocketScript() {
if ( logRocketScriptLoaded ) {
logRocketDebug( 'LogRocket script already loaded' );
return;
}
if ( ! mayWeLoadLogRocketScript() ) {
logRocketDebug( 'Not loading LogRocket script' );
return;
}
if ( ! isJetpackCloud() ) {
logRocketDebug( 'Not loading LogRocket script: not Jetpack Cloud' );
return;
}
const script = document.createElement( 'script' );
script.src = 'https://cdn.logrocket.io/LogRocket.min.js';
script.crossOrigin = 'anonymous';
script.async = true;
script.onload = () => {
logRocketDebug( 'LogRocket script loaded' );
if ( window.LogRocket ) {
window.LogRocket.init( TRACKING_IDS.logRocket, {
dom: {
// None of the input elements will be recorded or sent to LogRocket
// @see https://docs.logrocket.com/reference/dom#sanitize-all-user-input-fields
inputSanitizer: true,
},
// @see https://docs.logrocket.com/v1.0/reference/network
network: {
requestSanitizer: ( request ) => {
// Remove the Authorization header from the request if it exists
if ( request.headers && request.headers.Authorization ) {
request.headers.Authorization = null;
}
// Remove the body from the request if it exists
if ( request.body ) {
delete request.body;
}
return request;
},
responseSanitizer: ( response ) => {
// Remove the body from the response if it exists
if ( response.body ) {
delete response.body;
}
return response;
},
},
} );
maybeIdentifyUser();
logRocketScriptLoaded = true;
}
};
script.onerror = () => {
logRocketDebug( 'Error loading LogRocket script' );
};
document.head.appendChild( script );
}
function maybeIdentifyUser() {
if ( ! window.LogRocket ) {
return;
}
const currentUser = getCurrentUser();
if ( currentUser ) {
logRocketDebug( 'maybeIdentifyUser:', currentUser );
window.LogRocket.identify( currentUser.hashedPii.ID );
}
}
export function recordLogRocketEvent( name, props ) {
maybeAddLogRocketScript();
if ( ! window.LogRocket || ! name ) {
return;
}
logRocketDebug( 'recordLogRocketEvent:', { name, props } );
window.LogRocket.track( name, props );
}
|