N
N
Noir2019-10-07 10:59:36
Google Apps Script
Noir, 2019-10-07 10:59:36

Where is the problem in the script?

Why, when loading this script in the table, not all sites work constantly; there are several with errors, while it is random?
https://gist.github.com/csarigoz/76777a4743bfa8bed491

spoiler
/**
 * Returns Mobile Speed & Optimization and Desktop Speed & Optimization values in six adjacent columns
 * by Cagri Sarigoz
 */
function checkAll(Url) {

    //CHANGE YOUR API KEY WITH YOUR_API_KEY BELOW
    var key = "YOUR_API_KEY";
    var serviceUrlMobile = "https://www.googleapis.com/pagespeedonline/v4/runPagespeed?url=" + Url + "&strategy=mobile&key=" + key;
    var serviceUrlDesktop = "https://www.googleapis.com/pagespeedonline/v4/runPagespeed?url=" + Url + "&strategy=desktop&key=" + key;
    //    var serviceUrlMobileFriendliness = "https://searchconsole.googleapis.com/v1/urlTestingTools/mobileFriendlyTest:run?fields=mobileFriendliness&key="+key;
    //    var formDataMobileFriendliness = {'url':Url};
    //    var optionsMobileFriendliness = {
    //     'method' : 'post',
    //     'payload' : formDataMobileFriendliness
    //    };
    var array = [];

    if (key == "YOUR_API_KEY")
        return "Please enter your API key to the script";


    var responseMobile = UrlFetchApp.fetch(serviceUrlMobile);

    if (responseMobile.getResponseCode() == 200) {
        var contentMobile = JSON.parse(responseMobile.getContentText());

        if ((contentMobile != null) && (contentMobile["ruleGroups"] != null)) {
            if (contentMobile["responseCode"] == 200) {
                var optimizationScoreMobile = contentMobile["ruleGroups"]["SPEED"]["score"];
                if ("metrics" in contentMobile["loadingExperience"]) {
                    var avgFCPMobile = ((contentMobile["loadingExperience"]["metrics"]["FIRST_CONTENTFUL_PAINT_MS"]["median"]) / 1000).toFixed(1).toString() + " (" + contentMobile["loadingExperience"]["metrics"]["FIRST_CONTENTFUL_PAINT_MS"]["category"] + ")";
                    var avgDCLMobile = ((contentMobile["loadingExperience"]["metrics"]["DOM_CONTENT_LOADED_EVENT_FIRED_MS"]["median"]) / 1000).toFixed(1).toString() + " (" + contentMobile["loadingExperience"]["metrics"]["DOM_CONTENT_LOADED_EVENT_FIRED_MS"]["category"] + ")";
                } else {
                    var avgFCPMobile = "Unavailable";
                    var avgDCLMobile = "Unavailable";
                }
            } else {
                var optimizationScoreMobile = "An error occured";
                var avgFCPMobile = "An error occured";
                var avgDCLMobile = "An error occured";

            }
        }
    }

    var responseDesktop = UrlFetchApp.fetch(serviceUrlDesktop);

    if (responseDesktop.getResponseCode() == 200) {
        var contentDesktop = JSON.parse(responseDesktop.getContentText());

        if ((contentDesktop != null) && (contentDesktop["ruleGroups"] != null)) {
            if (contentDesktop["responseCode"] == 200) {
                var optimizationScoreDesktop = contentDesktop["ruleGroups"]["SPEED"]["score"];
                if ("metrics" in contentDesktop["loadingExperience"]) {
                    var avgFCPDesktop = ((contentDesktop["loadingExperience"]["metrics"]["FIRST_CONTENTFUL_PAINT_MS"]["median"]) / 1000).toFixed(1).toString() + " (" + contentDesktop["loadingExperience"]["metrics"]["FIRST_CONTENTFUL_PAINT_MS"]["category"] + ")";
                    var avgDCLDesktop = ((contentDesktop["loadingExperience"]["metrics"]["DOM_CONTENT_LOADED_EVENT_FIRED_MS"]["median"]) / 1000).toFixed(1).toString() + " (" + contentDesktop["loadingExperience"]["metrics"]["DOM_CONTENT_LOADED_EVENT_FIRED_MS"]["category"] + ")";
                } else {
                    var avgFCPDesktop = "Unavailable";
                    var avgDCLDesktop = "Unavailable";
                }
            } else {
                var optimizationScoreDesktop = "An error occured";
                var avgFCPDesktop = "An error occured";
                var avgDCLDesktop = "An error occured";

            }
        }
    }

    //    var responseMobileFriendliness = UrlFetchApp.fetch(serviceUrlMobileFriendliness, optionsMobileFriendliness);
    //    var contentMobileFriendliness = JSON.parse(responseMobileFriendliness.getContentText());
    //      
    //      if ( (contentMobileFriendliness != null) && (contentMobileFriendliness["mobileFriendliness"] != null) )
    //      {
    //        var mobileFriendliness = contentMobileFriendliness["mobileFriendliness"];
    //      }
    //      else
    //        {
    //          var mobileFriendliness = "An error occured";
    //        }

    var currentDate = new Date().toJSON().slice(0, 10).replace(/-/g, '/');

    array.push([avgFCPMobile, avgDCLMobile, optimizationScoreMobile, avgFCPDesktop, avgDCLDesktop, optimizationScoreDesktop, currentDate]);
    Utilities.sleep(1000);
    return array;

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2019-10-08
@oshliaer

Because you choose the request quota for your key. View request statistics in the developer console.
This one is like a dead poultice. If you are running the script as a custom formula, then you need to make successive calls to the Principle Example in TableUtilities.sleep(1000);
5d9bad1a8c877535631892.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question