Socket
Socket
Sign inDemoInstall

gtmetrix

Package Overview
Dependencies
2
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    gtmetrix

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


Version published
Weekly downloads
151
decreased by-7.93%
Maintainers
1
Install size
157 kB
Created
Weekly downloads
 

Changelog

Source

1.3.0 (2018-03-06)

Documentation Changes
  • readme:
    • Add output examples (a6f65647)
    • Callback arg is no longer required (b20aa309)
    • Minor edits (0a1af5f0)
    • Better parsable syntax (3ace947d)
    • Removed result from example (6e858c13)
    • Full code example (545b7021)
New Features
  • interface: Promisified all methods (54a74b59)
Refactors
  • interface: Switched to es6-promisify (4dfae869)
  • package: Minimum node version 8.0.0 (ad64bb9a)
Code Style Changes
  • syntax: Only set doRequest of httpreq (f24606f6)

Readme

Source

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: 'your@mail.tld',
  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.

nametyperequireddefaultdescription
emailstringyesYour account email
apikeystringyesYour account API key
timeoutintegerno5000Wait timeout in ms
Example
const gtmetrix = require ('gtmetrix') ({
  email: 'your@mail.tld',
  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
messagedescriptionproperties
request failedRequest cannot be madeerror
invalid responseCan't process responseerror statusCode contentType
API errorAPI returned an errorerror statusCode contentType

test.create

( params, callback )

Run a test.

argumenttyperequireddescription
paramsobjectyesTest settings
callbackfunctionnoCallback 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.

argumenttyperequireddescription
testIdstringyesTest id to look up
resourcestringnoRetrieve a test resource
pollingnumbernoRetry until completion, in ms
callbackfunctionnoCallback 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
resourcebinarycontentdescription
filmstripyesJPEGPage loading filmstrip (requires video)
harnoJS objectHTTP Archive
pagespeednoJS objectPagespeed report
pagespeed-filesyesZIPPagespeed optimized files
report-pdfyesPDFTest summary
report-pdf-fullyesPDFFull test report
screenshotyesJPEGScreenshot image
videoyesMP4Page loading video
yslownoJS objectYSlow report

locations.list

( callback )

Get a list of available test locations.

argumenttyperequireddescription
callbackfunctionnoCallback 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.

argumenttyperequireddescription
callbackfunctionnoCallback 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.

argumenttyperequireddescription
browserIdnumberyesBrowser id to look up
callbackfunctionnoCallback 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.

argumenttyperequireddescription
callbackfunctionnoCallback 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

Keywords

FAQs

Last updated on 06 Mar 2018

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc