title: How can we detect when user closes browser?
body: <p>I mean, I want to track when a user leaves the app, closing browser or tabs.</p>
  
  <p>Components and Directives has a lifecycle hook called ngOnDestroy, which is called when the component is destroyed, but it can't catch when the user leaves the app</p>
  
  <pre><code>import { Component, OnInit } from '@angular/core';
  
  @Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html'
  })
  export class AppComponent implements OnDestroy {
    constructor() { }
  
    ngOnDestroy() {
        alert(`I'm leaving the app!`);
    }
  
  }
  </code></pre>
  
  <p>If the user closes the browser, the alert is not executed.</p>