Pen Settings

HTML

CSS

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

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

Packages

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.

Behavior

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.

HTML

              
                <header>
  <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>
</header>

<main>
  <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">
  </form>

  <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>

</main>

<hr>

<footer>
  <h2>Live Demo</h2>
  <p>
    Kick the tires with a <a href="https://streetphotography.blog/simple-form/">live demo</a>.
  </p>
</footer>
              
            
!

CSS

              
                
              
            
!

JS

              
                // 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
    });
  */

  return;
};

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

nameInput.addEventListener("input", (event) => {
  if (event.target.value !== "") {
    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.';
    alert(msg);
    console.log(msg);
    // sendBeachon can go here e.g., navigator.sendBeacon('/log', analyticsData);
    // Call to gtag can also go here.
  }
});
              
            
!
999px

Console