// We are using jQuery here as this plugin requires jQuery as a dependency (function ($) { $('body').on('click', '.wp-polls .poll-result-btn .poll-button', function () { if (window.dataLayer) { const pollQuestion = $(this).parents('.wp-polls').find('.polls-heading').text(); const isCheckBox = $(this).parents('.wp-polls').find("input[type='checkbox']").length > 0; let selectedAnswer = ''; let pollAnswer = ''; if (isCheckBox) { selectedAnswer = $(this).parents('.wp-polls').find("input[type='checkbox'][class='poll-answer']:checked").parents('.poll-options').find('label').text(); pollAnswer = $(this).parents('.wp-polls').find('input[type=checkbox]:checked').map(function(_, el) { return $(el).parents('.poll-options').find('label').text(); }).get(); } else { selectedAnswer = $(this).parents('.wp-polls').find("input[type='radio'][class='poll-answer']:checked").parents('.poll-options').find('label').text(); pollAnswer = selectedAnswer; } if (selectedAnswer) { window.dataLayer.push({ event: 'pollVoteClicked', pollCTA: { Category: "Vote Button Click", "Poll Question": pollQuestion, "Poll Answer": pollAnswer, } }); } } }); })(jQuery);