代码之家  ›  专栏  ›  技术社区  ›  mert dökümcü

如何使用PageSpeedInsights API和谷歌应用程序脚本向Slack发送谷歌灯塔报告

  •  0
  • mert dökümcü  · 技术社区  · 3 年前

    我想通过Slack定期更新我的网站速度。 如何使用谷歌应用程序脚本实现这一点?

    1 回复  |  直到 3 年前
        1
  •  4
  •   mert dökümcü    3 年前

    首先也是最重要的是,得到。。。

    一旦你有了这些,去 https://script.google.com/home 并创建一个新脚本。
    下面的代码应该可以做到这一点:

    var mobileData = fetchDataFromPSI('mobile');
    var desktopData = fetchDataFromPSI('desktop');
    
    function pageSpeedApiEndpointUrl(strategy) {
      const apiBaseUrl = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed';
      const websiteHomepageUrl = ''; // Your website
      const apikey = ''; // Your API key
      
      const apiEndpointUrl = apiBaseUrl + '?url=' + websiteHomepageUrl + '&key=' + apikey + '&strategy=' + strategy;
      return apiEndpointUrl;
    }
    function fetchDataFromPSI(strategy) {
      const pageSpeedEndpointUrl = pageSpeedApiEndpointUrl(strategy);
      const response = UrlFetchApp.fetch(pageSpeedEndpointUrl);
      const json = response.getContentText();
      const parsedJson = JSON.parse(json);
      
      const lighthouse = parsedJson['lighthouseResult']
      const originLoadingExperience = parsedJson['originLoadingExperience']
    
      const result = {
        'overall_performance': originLoadingExperience['overall_category'],
        'score': lighthouse['categories']['performance']['score']*100,
        'firstContentfulPaint': lighthouse['audits']['first-contentful-paint']['displayValue'],
        'speedIndex': lighthouse['audits']['speed-index']['displayValue'],
        'timeToInteractive': lighthouse['audits']['interactive']['displayValue'],
        'firstMeaningfulPaint': lighthouse['audits']['first-meaningful-paint']['displayValue'],
      }
      return result; 
    }
    function composeMessageForSlack() {
      const message = {
        'blocks': [
         {
          'type': 'section',
          'text': {
            'type': 'mrkdwn',
            'text': '*Website performance on Prod:*'
           }
         }
        ],
         'attachments': [
          {
            'blocks': [
              {
                'type': 'section',
                'text': {
                  'type': 'mrkdwn',
                  'text': 'Performance on Mobile :point_down:'
                 }
              },
              {
              'type': 'section',
              'text': {
                  'type': 'mrkdwn',
                  'text': 'Score = ' + mobileData['score'] + 
                          '\n\nFirst Contentful Paint = ' + mobileData['firstContentfulPaint'] + 
                          '\n\nSpeed Index = ' + mobileData['speedIndex'] + 
                          '\n\nTime To Interactive = ' + mobileData['timeToInteractive'] +
                          '\n\nFirst Meaningful Paint = ' + mobileData['firstMeaningfulPaint'] + 
                          '\n\nOverall Performance is = ' + mobileData['overall_performance'] + '\n\n'
              }
            },
            {
              'type': 'divider'
            },
            {
              'type': 'section',
              'text': {
                'type': 'mrkdwn',
                'text': 'Performance on Desktop :point_down:'
               }
             },
             {
              'type': 'section',
              'text': {
                  'type': 'mrkdwn',
                  'text': 'Score = ' + desktopData['score'] + 
                          '\n\nFirst Contentful Paint = ' + desktopData['firstContentfulPaint'] + 
                          '\n\nSpeed Index = ' + desktopData['speedIndex'] + 
                          '\n\nTime To Interactive = ' + desktopData['timeToInteractive'] +
                          '\n\nFirst Meaningful Paint = ' + desktopData['firstMeaningfulPaint'] +
                          '\n\nOverall Performance is = ' + desktopData['overall_performance'] + '\n\n'
              }
             }
           ]
          }
         ]
      };
      
      return message;
    }
    function postDataToSlack() {
      const slackWebhookUrl = ''; //Your Slack webhook url
    
      const payload = composeMessageForSlack();
      
      const options = {
        'method' : 'post',
        'contentType' : 'application/json',
        'payload' : JSON.stringify(payload)
      };
      
      return UrlFetchApp.fetch(slackWebhookUrl, options);
    }
    function doGet() {
      postDataToSlack()
      return ContentService.createTextOutput('Website Performance retrieval successfull!');
    }
    
    

    测试代码并确保其正常工作后,确定报告的频率 using triggers .

    推荐文章