
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
viewability.js
Advanced tools
Viewability tracker used to measure whether a DOM elements is actually seen by users
Viewability.js is a lightweight, dependency-free JavaScript library that tracks the visibility and viewability of a DOM element within the viewport using the IntersectionObserver and MutationObserver API. It allows developers to measure how much of an element is viewable and for how long, making it useful for ad tracking, analytics, and user engagement monitoring.
By default, the library follows the IAB Standard for viewability measurement (50% visibility for at least 1 second), but these values can be fully customized to fit specific needs.
:star: Star me on GitHub — it motivates me a lot!
In addition to checking if an element is within the viewport, the library includes advanced visibility checks to determine whether an element is truly visible in the viewport.
When the isVisible option is enabled (by default), the library performs additional checks to ensure an element is not just in the viewport, but actually visible:
coverageThreshold (value greater than 0 and up to 1).
You can install the library via npm or simply include it in your project:
npm install viewability.js
<script> tag<script src="path/to/viewability.js"></script>
The library allows you to pass:
#)..class-name).If multiple elements match a given selector, only the first one found will be tracked.
import { Viewability } from "viewability.js";
// Pass an element reference
new Viewability(document.getElementById("target"));
// Pass an ID string (equivalent to document.getElementById)
new Viewability("target");
// Pass a CSS selector (selects the first matching element)
new Viewability(".target");
import { Viewability } from "viewability.js";
const tracker = new Viewability("target", {
onComplete: () => console.log("Element fully viewed!"),
onError: (err) => console.error(err.message),
});
// or
const tracker = new Viewability("target");
tracker.onComplete = () => console.log("Element fully viewed!");
tracker.onError = (err) => console.error(err.message);
| Option | Type | Default | Description |
|---|---|---|---|
autostart | Boolean | true | Automatically starts tracking on initialization. |
autostop | Boolean | true | Automatically stop tracking and disconnects the observer when viewability is completed. |
coverageThreshold | Number | 0.1 | When isVisible is true, this is the fraction of the element's area that must be covered in order to consider the element as covered (greater than 0 and up to 1). |
inViewThreshold | Number | 0.5 | Percentage of the element that must be visible (0 to 1). |
isVisible | Boolean | true | Whether to check if the element is truly visible in the viewport. |
timeInView | Number | 1000 | Time (in milliseconds) the element must remain in view to be considered fully viewed. |
onComplete | Function | undefined | Callback function executed when the element meets the viewability criteria. |
onError | Function | undefined | Callback function executed when something wrong. |
| Method | Description |
|---|---|
start() | Starts tracking if it hasn't already started. |
stop() | Stops tracking and disconnects the observer. |
const tracker = new Viewability("target", { autostart: false });
tracker.onComplete = () => console.log("Element fully viewed!");
tracker.onError = (err) => console.error(err.message);
tracker.start();
tracker.stop();
Viewability.js relies on the IntersectionObserver and MutationObserver API, which are supported in all modern browsers. For older browsers (e.g., Internet Explorer), a polyfill may be required.
I would greatly appreciate any contributions to make this project better. Please make sure to follow the below guidelines before getting your hands dirty.
Copyright Andrea Fassina, Licensed under MIT.
FAQs
Viewability tracker used to measure whether a DOM elements is actually seen by users
We found that viewability.js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.