October 25, 2024
Chicago 12, Melborne City, USA
HTML

Data Dog Rum Session Recording isnt stopping when user goes to new url


On the login page when i click the hyperlink to go to signup page and try to go to a new url like google.com then the data dog session recording isnt stopping despite the code below in signup page which executes a beforeunload event handler when it detects user going to new url & handles it by calling stopSession() which a DataDog SDK helper function to stop recording in data dog.

window.addEventListener(‘beforeunload’,(event) => externalNavSessionRecordStopper(event));

function externalNavSessionRecordStopper(event) {
    if(!isUserNavigation()) {
        console.log('externalNavSessionRecordStopper() was triggered by generated event:', event.type);  // Log the event type

        if (event.type === 'beforeunload' || event.type === 'unload') {
            event.preventDefault();
            event.returnValue="";
        }

        try {
            if (window.DD_RUM) {
                console.log('mysignuppage-externalNav- Attempting to stop session recording');
                window.DD_RUM.stopSessionReplayRecording();
                console.log('mysignuppage-externalNav- Successfully stopped session recording');    
                console.log('mysignuppage-externalNav- Attempting to stop session');

                //sync calling stopSession()
                window.DD_RUM.stopSession(); 

                //sync calling stopSession() with null check
                
                //window.DD_RUM && window.DD_RUM.stopSession()
                
                
                // async calling stopSession() using onReady
                /*
                window.DD_RUM.onReady(function() {
                    DD_RUM.stopSession()
                })
                */

                console.log('mysignuppage-externalNav- Successfully stopped session');

                // Force small delay to ensure commands complete
                const startTime = Date.now();
                while(Date.now() - startTime < 200) {}
            }
            else {
            console.warn('mysignuppage-externalNav- Failed to stop session recording (DD_RUM not avail)');
            }
        } catch (sessionRecordingStopError) {
            console.error('mysignuppage-externalNav- Failed to stop recording:', sessionRecordingStopError);
        }
    }
}



// Stop session recording should autosave the replay of the recording (accessible on DD dashboard)
function stopSessionRecording() {
    try {
        if (window.DD_RUM) {
            console.log('mysignuppage-createBtn- Attempting to stop session recording');
            window.DD_RUM.stopSessionReplayRecording();
            console.log('mysignuppage-createBtn- Successfully stopped session recording');  
            console.log('mysignuppage-createBtn- Attempting to stop session');
            window.DD_RUM.stopSession(); //async
            console.log('mysignuppage-createBtn- Successfully stopped session');
        }
        else {
            console.warn('mysignuppage-createBtn- Failed to stop session recording (DD_RUM not avail)');
            }
    } catch (sessionRecordingStopError) {
        console.error('mysignuppage-createBtn- Failed to stop session recording:', sessionRecordingStopError);
    }
}

I read the Data Dog API Docs to do sync and async call to stopSession() helper function.

I also debugged the js code and walkthrough it on the html page interaction.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video