fvdm/nodejs-gtmetrix

Issue on response

rmshkmr opened this issue · 9 comments

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

var gtmetrix = require ('gtmetrix') ({
    email: 'ramesh.kumar@opteamize.in',
    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' }

fvdm commented

No it's not empty. It does report state: 'queued' and you need to try again later. There are no results because the test did not complete yet. Short example without error handling:

// Create test
gtmetrix.test.create (test, function (err, res) {
  var testid = res.test_id;

  // Keep checking the status after each 10 seconds
  var retry = setInterval (function () {

    // Get test results
    gtmetrix.test.get (testid, function (tErr, tData) {
      if (tData.state === 'completed' || tData.state === 'error') {
        // It's done
        clearInterval (retry);
        console.log (tData);
      }
    });
  }, 10000);
});

I will implement a feature that takes care of this for you.

fvdm commented

Alright, I just released v1.2.0 on npm. Here is how to use it:

gtmetrix.test.create (test, function (err, data) {
  if (err) { return console.log (err) }

  // check every 5 seconds for completion
  gtmetrix.test.get (data.test_id, 5000, console.log);
});

In my tests they usually complete after around 20 seconds.

@fvdm It gives me the error
`/home/ramesh/Documents/Learning/gt/node_modules/gtmetrix/index.js:58
callback (error, data);
^

TypeError: callback is not a function
at /home/ramesh/Documents/Learning/gt/node_modules/gtmetrix/index.js:58:5
at finalCallback (/home/ramesh/Documents/Learning/gt/node_modules/httpreq/lib/httpreq.js:133:4)
at IncomingMessage. (/home/ramesh/Documents/Learning/gt/node_modules/httpreq/lib/httpreq.js:393:5)
at emitNone (events.js:72:20)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at nextTickCallbackWith2Args (node.js:441:9)
at process._tickCallback (node.js:355:17)

Process finished with exit code 1`

fvdm commented

Could you please provide a code sample?

`var gtmetrix = require ('gtmetrix') ({
email: 'ramesh.kumar@opteamize.in',
apikey: 'UsedMyAPI',
timeout: 10000
});

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

// Create test
gtmetrix.test.create (test,function(err,data){
if (err) { return console.log (err) }
// check every 5 seconds for completion
gtmetrix.test.get (data.test_id,5000,console.log);
});

`

fvdm commented

@rmshkmr Can you confirm you are using the latest version?

npm ls gtmetrix

npm_gtmetrix_version

@fvdm Its working like charm. Where can i get full documentation for query full report like pagespeed,yslow,pdf?

fvdm commented

@rmshkmr Good the hear!

I just released v1.2.3 with improved resources support. See the documentation.

https://github.com/fvdm/nodejs-gtmetrix#retrieve-screenshot

gtmetrix.test.get (data.test_id,'yslow',5000,console.log);

giving this error
selection_032

gtmetrix version 1.2.3