ebidel / appmetrics.js

A small (< 1kb) library for measuring things in your web app and reporting the results to Google Analytics.
Apache License 2.0
1.37k stars 67 forks source link

Easier way to log custom timed metrics #11

Open mdvorscak opened 6 years ago

mdvorscak commented 6 years ago

In the first paint example

/**
 * Returns the browser's first paint metric (if available).
 * @return {number} The first paint time in ms.
 */
function getFirstPaintIfSupported() {/* ... */}

// Take measurement after page load.
window.addEventListener('load', function() {
  const fp = getFirstPaintIfSupported();
  if (fp) {
    const metric = new Metric('firstpaint');
    //If I call log here
    metric.log(); // I get firstpaint -1 ms
    // No need to call start()/end(). Can send a value, directly.
    metric.sendToAnalytics('load', metric.name, fp);
  }
});

Of course I can manually generate the log, but it could be nice if the log could take an overridable duration parameter (like sendToAnalytics):

  log(duration = this.duration) {
    console.info(this.name, this.duration, 'ms');
    return this;
  }

Or maybe a way to set the duration itself for these types of events?

set duration(duration){
  this.customDuration = duration;
  return this;
}

What do you think?

ebidel commented 6 years ago

Not sure I follow completely. .log() is just meant as a convenient helper for writing the console.log statement yourself. It should really throw if start and end were never called. If you want to log a custom metric, maybe just console.log what you want :) ?

mdvorscak commented 6 years ago

Yeah, of course I can write the log statement myself. I just thought it would be nicer if I could use metric.log to remain consistent. Rather than switching between metric.log and console.log, if I want to keep my log messages the same it means that I must know the internals of how metric is doing it's logging (and format my console.log the same).

Maybe metric.log really should throw an error if there is no duration associated to it?

ebidel commented 6 years ago

Maybe metric.log really should throw an error if there is no duration associated to it?

Yea exactly :) That ways it's not ambiguous and you don't need to understand the internal details. I'm happy to take a PR for that btw.