Skip to content

Instantly share code, notes, and snippets.

@Kishorchandth
Kishorchandth / web vitals
Created July 25, 2021 04:39
Web-Vitals JS libraries
<script type="module">
// Load the web-vitals library from unpkg.com (or host locally):
import {getFCP, getLCP, getCLS, getTTFB, getFID} from 'https://unpkg.com/web-vitals?module';
function getSelector(node, maxLen = 100) {
let sel = '';
try {
while (node && node.nodeType !== 9) {
const part = node.id ? '#' + node.id : node.nodeName.toLowerCase() + (
(node.className && node.className.length) ?
@Kishorchandth
Kishorchandth / Web vitals
Last active July 25, 2021 04:55
Web Vitals with PHP through Code Snippet plugin
function web_vitals( ){
// The Data (LCP, CLS and FID) collected by web-vitals.js are not sent to Google Analytics but display on console tab.
//You will only see it if you're login user
if ( is_user_logged_in() ){
?>
<script type="module">
import {getCLS, getFID, getLCP} from 'https://unpkg.com/web-vitals?module';
getCLS(console.log);
getFID(console.log);
getLCP(console.log);