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

              
                <div class='prodRecSection'>Recommended products section</div>
<div class='answer'>Are the recommended products visible? No</div>
              
            
!

CSS

              
                body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
	margin: 0;
	color: #555;
}
.prodRecSection {
  margin: 150vh 50px;
	height: 100px;
	outline: dashed 4px salmon;
	color: salmon;
}
.answer {
	position: fixed;
	top: 35px;
	left: 0;
	right: 0;
}
.prodRecSection, .answer {
	display: flex;
	justify-content: center;
	align-items: center;
	font-size: 2em;
	font-weight: 600;
}
              
            
!

JS

              
                // --- this is a test ometria object for this script here

var testOmetria = {
  // test function
  trackRecommendedProductsImpression: function() {
    alert('tracking happened');
  }
}

// this assignment is for the test here - not for live code.
var ometria = testOmetria;

// test prodRecInstance
var prodRecInstance = '5588dc36c00xxb7v57be2daxx991a90cd';

// ---- end.

function areRecommendedProductsInView (el) {
	const scroll = window.scrollY || window.pageYOffset
	const boundsTop = el.getBoundingClientRect().top + scroll
	
	const viewport = {
		top: scroll,
		bottom: scroll + window.innerHeight,
	}
	
    const bounds = {
		top: boundsTop,
		bottom: boundsTop + el.clientHeight,
	}
	
	return (bounds.bottom >= viewport.top && bounds.bottom <= viewport.bottom ) 
		|| ( bounds.top <= viewport.bottom && bounds.top >= viewport.top );
}

document.addEventListener('DOMContentLoaded', function () {
	var prodRecSection = document.querySelector('.prodRecSection');
	var answer = document.querySelector('.answer');
  var timestracked = 0;
	
	function handler() { 
    return raf(function () {
      var areProductsInView = areRecommendedProductsInView(prodRecSection);
      // tracking function call is happening here
      if (areProductsInView && timestracked === 0 && typeof ometria !== 'undefined' && typeof ometria.trackRecommendedProductsImpression === 'function') {
        timestracked += 1;
        ometria.trackRecommendedProductsImpression(prodRecInstance); 
      }
		  answer.innerHTML = 'Are the recommended products visible? ' + (               areProductsInView ? 'Yes' : 'No' )
	    })
  }
	
	handler();
	window.addEventListener('scroll', handler);
});

// requestAnimationFrame
var raf = 
    window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    function( callback ) {
        window.setTimeout( callback, 1000 / 60 )
    };
              
            
!
999px

Console