title: How to know if page was closed / refreshed / 'go back' was pressed in angular 2+
body: <p>
  I'm trying to use the function <code>window.addEventListener('beforeunload', function (event) {...}</code>
  In the function, I want to know which action triggered the listener.</p>
  
  <p>
  I tried to use this method with no success:</p>
  
  <pre><code>window.addEventListener('beforeunload', function (event) {
       if (performance.navigation.type == 1) {
          document.write('Window was refreshed!');
       } 
       else {
          document.write('Window was closed!');
       }
  }); 
  </code></pre>
  
  <p>Any action (refresh/close/'go back') triggers the <code>else</code> part... what am I doing wrong?</p>
  
  <p>Alternative method would be appreciated as well.
  </p>