Source: track-account-register-page.js

import { SignupAttemptListener, SignupSuccessListener, SignupFailureListener, SignupSuccessMessageListener } from "./listeners/sign-up";

/**
 * Sets up and tears down event listeners for the /account/register page.
 * Session storage is used to track attempts across page loads.
 * @function trackAccountRegisterPage
 * @param {Object} options - an object of parameters
 * @param {string} options.customerID - the currently logged in customer if there is one
 * @returns {void}
 */
export default function trackAccountRegisterPage({ customerID }) {
    const isAccountRegisterPage = window.location.pathname === "/account/register";
    if (isAccountRegisterPage) {
        const attempt = new SignupAttemptListener();
        attempt.onTrigger = storeSignupAttempt;
        attempt.attach();

        new SignupFailureListener().attach();
        new SignupSuccessMessageListener().attach();
    }

    // The success event may trigger on a different page.
    new SignupSuccessListener(customerID).attach();

    cleanupSignupAttempt();
}

/**
 * Set the signup_attempt flag in session storage.
 * This allows us to track attempts across page loads.
 * @private
 */
function storeSignupAttempt() {
    sessionStorage.setItem('signup_attempt', true);
}

/**
 * Clean up the attempt tracking flag.
 * Session is allowed to persist across the captcha page in case the user is required to solve a captcha.
 * @private
 */
function cleanupSignupAttempt() {
    const isCaptchaPage = window.location.pathname === '/challenge';
    if (!isCaptchaPage) {
        sessionStorage.removeItem('signup_attempt');
    }
}