Today we create a custom website page speed checker using Google PageSpeed API.
On this process, two steps to creating page speed checker,
- Creating an HTML text box with a button.
- Creating an onClick function on the above mentioned button.
- Creating a PageSpeed function that takes the URL from the text box.
Creating an HTML Text box with a Button
We create an HTML form with a text box and a button for getting particular website speed data.
1 2 3 4 5 6 7 8 9 10 11 |
<style type="text/css"> .isItLoaded{display: none;} .isItShow{display: block !mportant;} </style> <input type="text" name="" id="user_url"> <button type="button" onclick="run();">Click here to test</button> <div id="content" class=""> <div id="loading"></div> <div class="isItLoaded">Loading...</div> </div> |
Creating a JavaScript Function
Here is the complete source code of JavaScript onClick
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<a href="https://code.jquery.com/jquery-3.4.1.min.js">https://code.jquery.com/jquery-3.4.1.min.js</a> <script> function run() { $('.isItLoaded').removeClass('isItLoaded'); $('.isItLoaded').addClass('isItShow'); const url = setUpQuery(); fetch(url) .then(response => response.json()) .then(json => { // See https://developers.google.com/speed/docs/insights/v5/reference/pagespeedapi/runpagespeed#response // to learn more about each of the properties in the response object. showInitialContent(json.id); const cruxMetrics = { "First Contentful Paint": json.loadingExperience.metrics.FIRST_CONTENTFUL_PAINT_MS.category, "First Input Delay": json.loadingExperience.metrics.FIRST_INPUT_DELAY_MS.category }; showCruxContent(cruxMetrics); const lighthouse = json.lighthouseResult; const lighthouseMetrics = { 'First Contentful Paint': lighthouse.audits['first-contentful-paint'].displayValue, 'Speed Index': lighthouse.audits['speed-index'].displayValue, 'Time To Interactive': lighthouse.audits['interactive'].displayValue, 'First Meaningful Paint': lighthouse.audits['first-meaningful-paint'].displayValue, 'First CPU Idle': lighthouse.audits['first-cpu-idle'].displayValue, 'Estimated Input Latency': lighthouse.audits['estimated-input-latency'].displayValue }; $('.isItLoaded').removeClass('isItShow'); showLighthouseContent(lighthouseMetrics); }); } function setUpQuery() { var user_url = document.getElementById('user_url').value; const api = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed'; const parameters = { url: encodeURIComponent(user_url) }; let query = `${api}?`; for (key in parameters) { query += `${key}=${parameters[key]}`; } return query; } function showInitialContent(id) { document.body.innerHTML = ''; const title = document.createElement('h1'); title.textContent = 'PageSpeed Insights API Demo'; document.body.appendChild(title); const page = document.createElement('h3'); page.textContent = `Page tested: ${id}`; document.body.appendChild(page); } function showCruxContent(cruxMetrics) { const cruxHeader = document.createElement('h2'); cruxHeader.textContent = "Chrome User Experience Report Results"; document.body.appendChild(cruxHeader); for (key in cruxMetrics) { const p = document.createElement('p'); p.textContent = `${key}: ${cruxMetrics[key]}`; document.body.appendChild(p); } } function showLighthouseContent(lighthouseMetrics) { const lighthouseHeader = document.createElement('h2'); lighthouseHeader.textContent = "Lighthouse Results"; document.body.appendChild(lighthouseHeader); for (key in lighthouseMetrics) { const p = document.createElement('p'); p.textContent = `${key}: ${lighthouseMetrics[key]}`; document.body.appendChild(p); } } </script> |
Here we use Google page speed API.
Live Demo of PageSpeed Checker
See the Pen Page Speed Checker using Google PageSpeed API by Bikash Panda (@phpcodertech) on CodePen.
Also Check:
- Send Mail Attachments with PHP Mail()
- Selection in CSS | CSS ::selection
- How to Integrate Google Translate on website
Happy Coding..!
[…] Page Speed Checker using Google PageSpeed API […]
[…] Page Speed Checker using Google PageSpeed API […]