marshmallow/gtmetrix

Get GT Metrix information in Laravel Nova for you website pages. This package can also be used stand-alone.

v1.1.2 2021-04-07 11:25 UTC

README

alt text

GT Metrix for Laravel Nova

Get GT Metrix information in Laravel Nova for you website pages. This package can also be used stand-alone.

How does it work?

You create an account on GT Metrix. Once you have an API key you are good to go. You can add this library on every resource you want to, as long as it has a public url connected to it so we can get the GT Metrix score for that url.

Index view

On the index page we show an avarage score of the `Pagespeed` score and the `Yslow` score.

68747470733a2f2f6769746c61622e636f6d2f6d617273686d616c6c6f772d7061636b616765732f6e6f76612f67746d65747269782f2d2f7261772f6d61737465722f7265736f75726365732f73637265656e73686f74732f696e6465782d766965772e706e67

Detail view

On the detail view we will show the `Pagespeed` score and the `Yslow` score. You can click on them to go to the full report on the GTmetrix.com website.

68747470733a2f2f6769746c61622e636f6d2f6d617273686d616c6c6f772d7061636b616765732f6e6f76612f67746d65747269782f2d2f7261772f6d61737465722f7265736f75726365732f73637265656e73686f74732f64657461696c2d766965772e706e67

Action

On the detail view you will be able to start an action. This action is queued so make sure you have a worker running. This is needed because the changes of a timeout are big if its busy at GTmetrix.

68747470733a2f2f6769746c61622e636f6d2f6d617273686d616c6c6f772d7061636b616765732f6e6f76612f67746d65747269782f2d2f7261772f6d61737465722f7265736f75726365732f73637265656e73686f74732f616374696f6e2d766965772e706e67

Once you've selected the action to get a new score you will get a popup telling you how much credits you have left and if you are sure you want to spend another credit.

68747470733a2f2f6769746c61622e636f6d2f6d617273686d616c6c6f772d7061636b616765732f6e6f76612f67746d65747269782f2d2f7261772f6d61737465722f7265736f75726365732f73637265656e73686f74732f616374696f6e2d706f7075702e706e67

Installation

Pull in the library using composer.

composer require marshmallow/gtmetrix

Update your .env file and add the to env properties listed below.

GTMETRIX_EMAIL_ADDRESS=...
GTMETRIX_API_KEY=...

Prepare your models

  1. Add use Actionable; and use GTMetrix; to your model.
namespace App;

use Laravel\Nova\Actions\Actionable;
use Marshmallow\GTMetrix\Traits\GTMetrix;

class Post extends Model
{
    use GTMetrix;
    use Actionable;
    // ...
  1. Add the public method getFullPublicPath() to your models. When getting the status from GT Metrix, we will use the result of this method as the url you want to check.
class Post extends Model
{
    // ...

    public function getFullPublicPath()
    {
        return 'https://marshmallow.dev/' . $this->slug;
    }
}

Prepare your nova resources

  1. Add GTMetrixField::make('GT Metrix'), as a field.
use Marshmallow\GTMetrix\GTMetrixField;

public function fields(Request $request)
{
    return [
        ID::make(__('ID'), 'id')->sortable(),
        GTMetrixField::make('GT Metrix'),
    ];
}
  1. Add new CheckGTMetrixScore, as an action.
use Marshmallow\GTMetrix\Actions\CheckGTMetrixScore;

public function actions(Request $request)
{
    return [
        new CheckGTMetrixScore,
    ];
}