Meet your perfect mattress.
Every mattress is designed by the sleep experts at Sleap Authority Labs to optimize your sleep. Find your perfect match with a few easy questions.
Will take just 60 seconds
' ); const quizSelector = '.quiz-step[data-step="' + index + '"]'; $(quizSelector).append( '
' + question.title + "
" ); $(quizSelector).append( '
' + question.description ? question.description : "" + "
" ); $(quizSelector).append('
'); question.answers.map((answer, answerIndex) => { const inputId = "q-" + index + "-a-" + answerIndex; const variantId = "answer" + index + answerIndex; $(quizSelector + " > .quiz__variants").append( '' ); $(quizSelector + " > .quiz__variants").append( '' ); if (answer.description) { $(quizSelector + " > .quiz__variants > #" + variantId).append( '' + answer.description + "" ); } if (answer.icon) { $(quizSelector + " > .quiz__variants > #" + variantId).append( '' ); } $(quizSelector + " > .quiz__variants > #" + variantId).append( "" + answer.title + "" ); }); }); } function updateProgress() { $(".progerss-bar-inner").text( (curStep + 1).toString() + " of " + data.questions.length ); const widthStep = 100 / data.questions.length; const newWidth = widthStep * (curStep + 1); $(".progerss-bar-inner").css("width", newWidth + "%"); } function showNextStep() { const question = data.questions[curStep]; $('.quiz-step[data-step="' + (curStep - 1).toString() + '"]').removeClass( "quiz-step_active" ); $('.quiz-step[data-step="' + (curStep + 1).toString() + '"]').removeClass( "quiz-step_active" ); $('.quiz-step[data-step="' + curStep + '"]').addClass("quiz-step_active"); updateProgress(); // Question impression window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: "dynamic_yield_quiz", type: "quiz_impression", title: question ? question.title : "Queston is undefined", sub_title: curStep, }); } // Function will show blured results & email dialog function finalizeQuiz() { $(".js-back").hide(); $(".progerss-bar").hide(); $( '.quiz-step[data-step="' + (data.questions.length - 1).toString() + '"]' ).removeClass("quiz-step_active"); $(".js-quiz-submit").hide(); $(".loading").show(); setTimeout(function () { $(".loading").hide(); $('.quiz-step[data-step="email"]').addClass("quiz-step_active"); // Email input impression window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: "dynamic_yield_quiz", type: "quiz_impression", title, sub_title: "email_impression", }); }, 3000); const answerKey = results .map((r, index) => (data.questions[index].skip ? "*" : r)) .join("_"); finalResult = data.results.find((r) => r.answers.includes(answerKey)); $(".result__name").text(finalResult.title); $(".result__media img").attr("src", finalResult.image); $(".result__controls a").attr("href", finalResult.link); $(".result__controls a").text(finalResult.buttonText); $(".result__descriptions").text("“" + finalResult.review.description + "”"); $(".result__verified span").text(finalResult.review.author); $(".result__review-title").text(finalResult.review.title); } // Checks whether submit button should be disabled function btnDisabled() { const result = $('input[name="q-' + curStep + '"]:checked').val(); if (Boolean(result)) { $(".js-quiz-submit").prop("disabled", false); } else { $(".js-quiz-submit").prop("disabled", true); } } setDataDom(); // Initial impression window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: "dynamic_yield_quiz", type: "impression", title, sub_title: "mainpage_impression", }); $(".js-intro").click(function (e) { e.preventDefault(); // Procceed to first question window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: "dynamic_yield_quiz", type: "click", title, sub_title: btnStartText, }); quizStarted = true; $(".intro").hide(); $(".quiz").css("display", "flex"); $(".progerss-bar").show(); $(".btn__back").show(); $(".js-back").hide(); showNextStep(); btnDisabled(); }); $('input[type="checkbox"], input[type="radio"]').change(function () { btnDisabled(); }); $(".js-back").click(function (e) { e.preventDefault(); curStep = curStep - 1; showNextStep(); if (curStep === 0) { $(".js-back").hide(); } const answerIndex = results.pop(); const inputId = "q-" + curStep + "-a-" + answerIndex; $('input[id="' + inputId + '"]').prop("checked", true); btnDisabled(); }); $(".js-quiz-submit").click(function () { // Save answer const result = $('input[name="q-' + curStep + '"]:checked').val(); const answer = data.questions[curStep]; results.push(result); // Reset answer for the next question $('input[name="q"]').prop("checked", false); curStep = curStep + 1; // Show back button after the first question if (curStep > 0) { $(".js-back").show(); } btnDisabled(); if (curStep < data.questions.length) { // Capture Quiz Answer const question = data.questions[curStep - 1]; window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: "dynamic_yield_quiz", type: "quiz_click", title: question ? question.title : "Question is not defined", sub_title: question?.answers[result]?.title, }); showNextStep(); } else { finalizeQuiz(); } }); $(".js-capture").click(function (e) { e.preventDefault(); var email = $(this).siblings('input[type="email"]').val().trim(); if (email.length > 0) { var email1 = CryptoJS.SHA1(email.toString().toLowerCase()); var email5 = CryptoJS.MD5(email.toString().toLowerCase()); const utms = window.location.search ? window.location.search + `&dg=${email1}` : `?dg=${email1}`; $(".result__controls a").attr("href", finalResult.link + utms); window.dataLayer.push({event: "finished_quiz"}); window.dataLayer.push({ event: "product_impression", products: [{ product_name: finalResult.title, upsell_button_text: "Shop Nectar Now", }], }); window.dataLayer.push({ event: "created_lead", email, hashedEmailMD5: email5.toString(), hashedEmailSHA1: email1.toString(), signUpType: "lead", widget: "quiz_email_submitted", }); $(".quiz-step").removeClass("quiz-step_active"); $(".footer").hide(); $('.quiz-step[data-step="result"]').addClass("quiz-step_active"); setTimeout(() => { // Product result impression window.dataLayer.push({ event: "dynamic_yield_quiz", type: "quiz_impression", title: finalResult.title, sub_title: "product_impression", href: finalResult.link + utms, image_source: finalResult.image, tags: { email }, }); }, 500) } }); $("#shop_CTA").click(function () { window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: "dynamic_yield_quiz", type: "quiz_click", title, sub_title: finalResult.buttonText, }); }); $("#cEmail").keyup(function () { var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if ($(this).val().length > 0 && filter.test($(this).val().trim())) { $(".js-capture").prop("disabled", false); } else { $(".js-capture").prop("disabled", true); } }); $(".quiz-close-btn").click(function () { const question = data.questions[curStep - 1]; window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: "dynamic_yield_quiz", type: "close", title, sub_title: question ? question.title : btnStartText, }); // FIXME: CLOSE OVERLAY HERE or REMOVE COMPLETELY });});