Giter VIP home page Giter VIP logo

nodejs-gtmetrix's Introduction

gtmetrix

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

npm Build Status Coverage Status

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 int 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 (err, data) or promise

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 int no Retry until completion, in ms
callback function no (err, data) or promise

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.

// 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 (err, data) or promise

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 (err, data) or promise

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 int yes Browser to look up
callback function no (err, data) or promise

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 description
[callback] function (err, data) or promise

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 | Buy me a coffee

nodejs-gtmetrix's People

Contributors

fvdm avatar greenkeeper[bot] avatar greenkeeperio-bot avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

nodejs-gtmetrix's Issues

Issue on response

Test Result is empty. Is my code has any issue?
My code

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

// Run test from London with Google Chrome
var test = {
    url: 'http://opteamize.in/',
    location: 2,
    browser: 3
};

gtmetrix.test.create (test, function(req,res){
    var testid = res.test_id;
    gtmetrix.test.get(testid,console.log);
});

My console.log
null { resources: {}, error: '', results: {}, state: 'queued' }

An in-range update of dotest is breaking the build 🚨

The devDependency dotest was updated from 2.6.1 to 2.6.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

dotest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).
  • βœ… coverage/coveralls: First build on greenkeeper/dotest-2.6.2 at 96.552% (Details).

Commits

The new version differs by 9 commits.

  • 0bb0b89 2.6.2
  • 18be6f3 Docs(changelog): Update changelog
  • adb9f92 Docs(readme): Added Promise example
  • 55f30c1 Test(config): Update Node versions on Travis CI
  • 2d70392 Chore(package): Update nyc to version 15.0.0 (#36)
  • 8a1c894 Chore(package): Update author url (#35)
  • ac3e4a6 Chore(deps): Update coveralls to v3.0.6 (#34)
  • 8305e24 Chore(security): Update ESLint to v6.2.2 (#33)
  • 9de4611 Chore(package): Update eslint to version 6.0.1 (#32)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Error while waiting for response

Hi,
I'm trying to do some tests with gtmetrix now but it looks like it has an error while waiting for the test results. Sometimes it works fine.
I checked the poll_state_url after the error and it looked like this:
{
"resources": {},
"error": "",
"results": {},
"state": "queued"
}

Please check here for the console error

if (err && !props.binary && !err.error.match (/Data not yet available/)) {
^
TypeError: err.error.match is not a function
at pollingCallback (/home/driant/Projects/GTMetrixTests/node_modules/gtmetrix/index.js:166:42)
at testGet (/home/driant/Projects/GTMetrixTests/node_modules/gtmetrix/index.js:267:24)
at /home/driant/Projects/GTMetrixTests/node_modules/gtmetrix/index.js:245:7
at apiResponse (/home/driant/Projects/GTMetrixTests/node_modules/gtmetrix/index.js:76:5)
at http.doRequest (/home/driant/Projects/GTMetrixTests/node_modules/gtmetrix/index.js:128:5)
at finalCallback (/home/driant/Projects/GTMetrixTests/node_modules/httpreq/lib/httpreq.js:98:7)
at ClientRequest. (/home/driant/Projects/GTMetrixTests/node_modules/httpreq/lib/httpreq.js:422:7)
at ClientRequest.g (events.js:292:16)
at emitNone (events.js:86:13)
at ClientRequest.emit (events.js:185:7)

Node version : v6.11.2
Npm version: 3.10.10

An in-range update of es6-promisify is breaking the build 🚨

The dependency es6-promisify was updated from 6.0.1 to 6.0.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

es6-promisify is a direct dependency of this project, and it is very likely causing it to break. If other packages depend on yours, this update is probably also breaking those in turn.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).
  • βœ… coverage/coveralls: First build on greenkeeper/es6-promisify-6.0.2 at 97.701% (Details).

Commits

The new version differs by 4 commits.

  • 4879a17 6.0.2
  • 07dd174 feat: Upgrade build pipeline to babel 7
  • b24ca46 fix: Revert references to npx for node 6,7
  • 2edb4c6 fix: Update dependencies

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Full rewrite

This code is so dated it needs a full rewrite.

  • Convert to a class
  • Real promises
  • Object style arguments
  • Remove callbacks
  • Restructure functions

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.