Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="l-box">
  <h1>Easy event tracking with Universal Analytics</h1>
  
  <p>Pure JavaScript function to track download links with Google Analytics.</p>
</div>

<div class="l-box">
  <h2>PDF</h2>
  
  <ul class="m-downloads">
    <li><a href="doc1.pdf" class="js-analytics-track" data-cat="PDF" data-label="Download">Download 1</a></li>
    <li><a href="doc2.pdf" class="js-analytics-track" data-cat="PDF" data-label="Download">Download 2</a></li>
    <li><a href="doc3.pdf" class="js-analytics-track" data-cat="PDF" data-label="Download">Download 3</a></li>
  </ul>
</div>

<div class="l-box">
  <h2>Test result</h2>
  
  <div id="m-result"></div>
</div>
              
            
!

CSS

              
                
body {
  background-color: #f5f5f5;
  padding: 20px;
  font-family: Helvetica Neue, Helvetica, Arial, Sans-Serif;
  
  @media screen and (min-width: 600px) {
    display: flex;
    flex-direction: row;    
  }
}

.l-box {
  background-color: #fff;
  padding: 20px;
  border: 1px solid #ddd;
  margin: 10px;
  
  h1 {
    margin: 0 0 .5em;
    font-weight: normal;
    font-size: 24px;
  }
  
  h2 {
    margin: 0 0 .5em;
    font-weight: normal;
    font-size: 20px;
  }
  
  @media screen and (min-width: 600px) {
    flex: 1;
  }  
  
  @media screen and (min-width: 800px) {
    h1 {
      font-size: 30px;
    }

    h2 {
      font-size: 25px;
    }
  }  
}

.m-downloads {
  list-style: none;
  margin: 2em 0;
  padding: 0;
  
  li {
    margin: 0 0 .8em;
    padding: 0;
  }
  
  a {
    color: blue;
    text-decoration: underline; 
  }
}
              
            
!

JS

              
                (function(){
  "use strict";
  /* global ga */
  
  var demo = true;
  
  var trackingLinks = document.getElementsByClassName('js-analytics-track'), i;

  function trackOnClick(event) {
    var config = {
          category: this.dataset.cat || 'PDF',
          action: this.dataset.action || 'Download',
          file: this.getAttribute('href')
        };
    
    // Just delete this block in production environment
    if(demo === true) {
      event.preventDefault();
      
      var result = document.getElementById('m-result');
      
      result.innerHTML = [
        'Google Analytics will track an event in the category',
        config.category,
        'action',
        config.action,
        'for the file',
        config.file,
        '.'
      ].join(' ');
    }
    
    if(typeof ga !== 'undefined') {
      ga('send', 'event', category, action, file);
    }
  }

  for(i = 0; i < trackingLinks.length; i++) {
    trackingLinks[i].addEventListener('click', trackOnClick);
  }
})();
              
            
!
999px

Console