Yes, It is possible. We can achieve this by using the performance timing API to first calculate the time it takes for the website to fully load by adding a custom listener that will fire on All Pages Trigger:
<script>
window.addEventListener('load', function() {
var loadTime = window.performance.timing.loadEventEnd - window.performance.timing.navigationStart;
dataLayer.push({'event': 'pageFullyLoaded', 'loadTime': loadTime});
});
</script>
Next, we can use events like beforeunload to track the user abandonment by creating another listener:
<script>
window.addEventListener('beforeunload', function() {
if (!window.pageFullyLoaded) {
dataLayer.push({'event': 'pageAbandoned', 'reason': 'beforeLoad'});
}
});
window.addEventListener('load', function() {
window.pageFullyLoaded = true;
});
</script>
Now that we have the listeners in place, we can use dataLayer variables to capture the loadTime and the reason parameters from the custom Listener dataLayer push using Google Tag Manager (GTM) and send this data to Google Analytics 4 (GA4) or another tools of your likings.