Search for the Article

Class/Element Visibility tracker - Google tag manager

  


function(){
var found = false;
var intervalId;

function checkElementOnPage() {
  var elements = document.getElementsByClassName("GetConsultationForm_successMessageSection__c2EeU");
    
  if (elements.length > 0 && !found) {
    console.log("Success message found on page");
    window.dataLayer.push({'event': 'formSubmissionSuccess'});
    found = true;
    clearInterval(intervalId);
  }
}

  var intervalId = setInterval(checkElementOnPage, 30000);
  return undefined;
}



function checkForSuccessMessage() {
  var found = false;
  var targetNode = document.body;
  var config = { childList: true, subtree: true };
  
  var callback = function(mutationsList, observer) {
    mutationsList.forEach(function(mutation) {
      if (mutation.type === 'childList') {
        var elements = document.getElementsByClassName("sent");
        if (elements.length > 0 && !found) {
          console.log("Success message found on page");
          window.dataLayer.push({'event': 'deepak'});
          found = true;
          observer.disconnect();
        }
      }
    });
  }
  
  var observer = new MutationObserver(callback);
  observer.observe(targetNode, config);
  return found;
}


Class/Element Visibility tracker - Google tag manager