Accessing Google Page Insights in PHP

Google Page Insights is a required tool to have when analyzing the speed and usability of your site. As you may know these metrics influence how google ranks your page in search results. If you frequently make changes to your web site designs than it becomes mandatory to check the metrics after each change to make sure that the design changes has not affected the score in any negative way. If you have many pages to test than manual testing can quickly become cumbersome.

Thankfully there are libraries that you can use to automate this process. Once such is given in this post which allows you to get Google Page Insight metrics using PHP.

Installation

Installation via composer is the best way to get the library.

composer require dsentker/phpinsights

Once that is done, get your api key from the Google developer console. This is required to access the api.

Usage

Usage is as simple as calling the api to get the speed score.

<?php
 
require "./vendor/autoload.php";
 
$uri = 'http://codediesel.com';
$caller = new \PhpInsights\InsightsCaller('your_google_api_key', 'en');
$response = $caller->getResponse($uri, \PhpInsights\InsightsCaller::STRATEGY_MOBILE);
$result = $response->getMappedResult();
 
echo "Your site speed score is : " . $result->getSpeedScore() . PHP_EOL;
echo "Your site usability score is : " . $result->getUsabilityScore();

You can set the insights for ‘desktop’ or ‘mobile’ by passing the appropriate constant to ‘getResponse’.

// Mobile Insights
$response = $caller->getResponse($uri, \PhpInsights\InsightsCaller::STRATEGY_MOBILE);
 
--OR--
 
// Desktop Insights
$response = $caller->getResponse($uri, \PhpInsights\InsightsCaller::STRATEGY_DESKTOP);

The api also returns a screenshot the page under test. You can download the same using the following.

if($result->hasScreenshot()) {
    print $result->screenshot->getImageHtml(); // html image element, OR
    print $result->screenshot->getData(); // base64 screenshot representation
}

The base64 data can be saved to a database for later use if wanted. This can be nice if you are changing the site design frequently and need to track the site score along with the design.

You can retrieve the full results using the following:

/** @var \PhpInsights\Result\InsightsResult $result */
foreach($result->getFormattedResults()->getRuleResults() as $rule => $ruleResult) {
 
    /*
     * If the rule impact is zero, it means that the website has passed the test.
     */
    if($ruleResult->getRuleImpact() > 0) {
 
        var_dump($rule); // AvoidLandingPageRedirects
        var_dump($ruleResult->getLocalizedRuleName()); // "Zielseiten-Weiterleitungen vermeiden"
 
        /*
         * The getDetails() method is a wrapper to get the `summary` field as well as `Urlblocks` data. You
         * can use $ruleResult->getUrlBlocks() and $ruleResult->getSummary() instead. 
         */
        foreach($ruleResult->getDetails() as $block) {
            var_dump($block->toString()); // "Auf Ihrer Seite sind keine Weiterleitungen vorhanden"
        }
 
    }
 
}

Errors while installation

You may probably encounter the following error on running the examples if a ssl certificate is not installed. Follow the steps given below to resolve the errors.

cURL error 60: SSL certificate problem: unable to get local issuer certificate

1. Download ‘cacert.pem’ from https://curl.haxx.se/docs/caextract.html.

2. Put the file in ‘C:\xampp\php\extras\ssl\cacert.pem’ or whatever your path maybe.

3. Add the following line to your php.ini. (change your path according to your system)
curl.cainfo = “C:\xampp\php\extras\ssl\cacert.pem”

4. Restart your webserver/apache server.

One thought on “Accessing Google Page Insights in PHP

Comments are closed.