Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


  <h1>Send an event to GA When Exiting a Page</h1>

  <p>There are 2 methods demo'd in this pen. View the browser console for the results.</p>

  <h2>1) Form Interaction with a Before Unload Listener</h2>

  <p>If there's anything in the input field before page unload, an alert will popup.</p>

  <p>A simple test is to enter something in the name field, then click the browser's back button.</>

  <form id="test-form">
    <input id="name" type="text" placeholder="Name">

  <h2>2) Navigation from Page with a Visibility Change Listener</h2>

  <p>Test this by opening up a new tab or navigating to another tab.</p>



  <h2>Live Demo</h2>
    Kick the tires with a <a href="">live demo</a>.




                // Old way. Not reliable. Each browser handles this differently.
// E.g., some browsers will not fire if there's no interaction
// with the page.
const beforeUnloadListener = (event) => {
  event.preventDefault(); // Just for testing this pen.

  console.log('beforeUnloadListener: Can send stats to GA here.');
  // GA code goes here.
  // Below is an example using gtag.js API.
      gtag('event', 'page-exit', {
      'event_category': 'input-field',
      'event_action' : 'exit-intent',
      'event_label': 'input-name',
      'value': 1


const nameInput = document.querySelector("#name");

nameInput.addEventListener("input", (event) => {
  if ( !== "") {
    addEventListener("beforeunload", beforeUnloadListener, {capture: true});
  } else {
    removeEventListener("beforeunload", beforeUnloadListener, {capture: true});

// More reliable. Switch tabs as a simple test.
document.addEventListener('visibilitychange', function logData() {
  if (document.visibilityState === 'hidden') {
    const msg = 'visibilitychange: Can send stats to GA here too.';
    // sendBeachon can go here e.g., navigator.sendBeacon('/log', analyticsData);
    // Call to gtag can also go here.