npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gtmetrix

v1.3.0

Published

Run and access GTmetrix tests to measure website performance (unofficial)

Downloads

660

Readme

gtmetrix

Node.js module for the GTmetrix API to run and access tests.

npm Build Status Coverage Status Dependencies Code Quality Greenkeeper

Example

const gtmetrix = require ('gtmetrix') ({
  email: '[email protected]',
  apikey: 'abc123'
});

// Run test from London with Google Chrome
const testDetails = {
  url: 'http://example.net/',
  location: 2,
  browser: 3
};

// Poll test every 5 seconds for completion, then log the result
gtmetrix.test.create (testDetails).then (data =>
  gtmetrix.test.get (data.test_id, 5000).then (console.log));

(For readability I left out the error handling)

Installation

npm i gtmetrix

You need an account at GTmetrix to get an API key. The API key can be found here when you are logged in.

Configuration

The setup function takes an object with these settings.

name | type | required | default | description :-------|:--------|:---------|:--------|:-------------------- email | string | yes | | Your account email apikey | string | yes | | Your account API key timeout | integer | no | 5000 | Wait timeout in ms

Example

const gtmetrix = require ('gtmetrix') ({
  email: '[email protected]',
  apikey: 'abc123',
  timeout: 10000
});

Methods

All methods return promises, but you can also provide a callback function instead which gets the standard err and data arguments.

In the examples below I use a mix of callbacks and promises, but each method can do both. I also left out the error handling for better readability.

Errors

message | description | properties :----------------|:-----------------------|:---------------------------------- request failed | Request cannot be made | error invalid response | Can't process response | error statusCode contentType API error | API returned an error | error statusCode contentType

test.create

( params, callback )

Run a test.

argument | type | required | description :--------|:---------|:---------|:----------------- params | object | yes | Test settings callback | function | no | Callback function

API documentation

// Run test from London with Google Chrome
const test = {
  url: 'http://example.net/',
  location: 2,
  browser: 3
};

gtmetrix.test.create (test, console.log);
{ credits_left: 68,
  test_id: 'Ao0AYQbz',
  poll_state_url: 'https://gtmetrix.com/api/0.1/test/Ao0AYQbz' }

test.get

( testId, [resource], [polling], callback )

Get details about a test or one of its resources.

When you specify a binary resource, i.e. screenshot, the callback data will be a Buffer instance, so you can post-process the binary data however you like. See example below.

argument | type | required | description :--------|:---------|:---------|:----------------------------- testId | string | yes | Test id to look up resource | string | no | Retrieve a test resource polling | number | no | Retry until completion, in ms callback | function | no | Callback function

API documentation

Test details

Get what is currently available, without waiting for completion.

gtmetrix.test.get ('Ao0AYQbz', console.log);
{ resources: {}, error: '', results: {}, state: 'started' }

Wait for completion and then get the details.

// Get test result when it is complete, retry every 5 seconds (5000 ms)
gtmetrix.test.get ('Ao0AYQbz', 5000, console.log);
{ resources:
   { report_pdf: 'https://gtmetrix.com/api/0.1/test/Ao0AYQbz/report-pdf',
     pagespeed: 'https://gtmetrix.com/api/0.1/test/Ao0AYQbz/pagespeed',
     har: 'https://gtmetrix.com/api/0.1/test/Ao0AYQbz/har',
     pagespeed_files: 'https://gtmetrix.com/api/0.1/test/Ao0AYQbz/pagespeed-files',
     report_pdf_full: 'https://gtmetrix.com/api/0.1/test/Ao0AYQbz/report-pdf?full=1',
     yslow: 'https://gtmetrix.com/api/0.1/test/Ao0AYQbz/yslow',
     screenshot: 'https://gtmetrix.com/api/0.1/test/Ao0AYQbz/screenshot' },
  error: '',
  results:
   { onload_time: 185,
     first_contentful_paint_time: 221,
     page_elements: 2,
     report_url: 'https://gtmetrix.com/reports/example.net/Ao0AYQbz',
     redirect_duration: 0,
     first_paint_time: 221,
     dom_content_loaded_duration: null,
     dom_content_loaded_time: 184,
     dom_interactive_time: 183,
     page_bytes: 1911,
     page_load_time: 185,
     html_bytes: 277,
     fully_loaded_time: 307,
     html_load_time: 145,
     rum_speed_index: 221,
     yslow_score: 99,
     pagespeed_score: 99,
     backend_duration: 68,
     onload_duration: 0,
     connect_duration: 77 },
  state: 'completed' }

Retrieve screenshot

Retry every 5000 ms until it's ready.

const fs = require ('fs');

gtmetrix.test.get ('Ao0AYQbz', 'screenshot', 5000).then (data =>
  fs.writeFile (__dirname + '/screenshot.jpg', data, console.log));
Resources

resource | binary | content | description :---------------|:-------|:----------|:--------------------------------------- filmstrip | yes | JPEG | Page loading filmstrip (requires video) har | no | JS object | HTTP Archive pagespeed | no | JS object | Pagespeed report pagespeed-files | yes | ZIP | Pagespeed optimized files report-pdf | yes | PDF | Test summary report-pdf-full | yes | PDF | Full test report screenshot | yes | JPEG | Screenshot image video | yes | MP4 | Page loading video yslow | no | JS object | YSlow report

locations.list

( callback )

Get a list of available test locations.

argument | type | required | description :--------|:---------|:---------|:----------------- callback | function | no | Callback function

API documentation

gtmetrix.locations.list (console.log);
[ { name: 'Vancouver, Canada',
    default: true,
    id: '1',
    browsers: [ 1, 3 ] } ]

browsers.list

( callback )

Get a list of available test browsers.

argument | type | required | description :--------|:---------|:---------|:----------------- callback | function | no | Callback function

API documentation

gtmetrix.browsers.list (console.log);
[ { features:
     { dns: true,
       cookies: true,
       adblock: true,
       http_auth: true,
       video: true,
       user_agent: true,
       throttle: true,
       filtering: true,
       resolution: true },
    browser: 'firefox',
    name: 'Firefox (Desktop)',
    platform: 'desktop',
    id: 1,
    device: '' } ]

browsers.get

( browserId, callback )

Get details about a test browser.

argument | type | required | description :---------|:---------|:---------|:----------------------- browserId | number | yes | Browser id to look up callback | function | no | Callback function

API documentation

gtmetrix.browsers.get (3, console.log);
{ features:
   { dns: true,
     cookies: true,
     adblock: true,
     http_auth: true,
     video: true,
     user_agent: true,
     throttle: true,
     filtering: true,
     resolution: true },
  browser: 'chrome',
  name: 'Chrome (Desktop)',
  platform: 'desktop',
  id: 3,
  device: '' }

account.status

( callback )

Information about your account.

argument | type | required | description :--------|:---------|:---------|:----------------- callback | function | no | Callback function

API documentation

gtmetrix.account.status (console.log);
{ api_refill: 1234567890, api_credits: 68 }

Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org

Author

Franklin van de Meent

Buy me a coffee