window.addEventListener("beforeunload", function(e) { var dataLayer = window.dataLayer = window.dataLayer || []; dataLayer.push ({ 'event' : 'beforeunload', 'timeOnPage' : new Date().getTime() - {{Page Load Start Time}} }); });