-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagespeed.go
62 lines (51 loc) · 1.45 KB
/
pagespeed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package analyze
import (
"context"
"time"
"google.golang.org/api/option"
"google.golang.org/api/pagespeedonline/v5"
)
const (
DESKTOP = "DESKTOP"
MOBILE = "MOBILE"
)
// Result is a struct that contains the result of the analysis
type Result struct {
URL string `json:"url"`
Strategy string `json:"strategy"`
Score float64 `json:"score"`
Date time.Time `json:"date"`
}
// PerfAnalyzer is a struct that contains the pagespeedonline service
type PerfAnalyzer struct {
pagespeedonlineService *pagespeedonline.Service
apiKey string
}
// NewPerfAnalyzer creates a new PerfAnalyzer
func NewPerfAnalyzer(apiKey string) *PerfAnalyzer {
pagespeedonlineService, err := pagespeedonline.NewService(context.Background(), option.WithAPIKey(apiKey))
if err != nil {
panic(err)
}
return &PerfAnalyzer{
pagespeedonlineService: pagespeedonlineService,
apiKey: apiKey,
}
}
// Analyze returns the score of a given url and strategy
func (a *PerfAnalyzer) Analyze(url string, strategy string) (Result, error) {
call := a.pagespeedonlineService.Pagespeedapi.Runpagespeed(url)
call.Category("PERFORMANCE")
call.Strategy(strategy)
pagespeedApiPagespeedResponseV5, err := call.Do()
if err != nil {
panic(err)
}
score, _ := pagespeedApiPagespeedResponseV5.LighthouseResult.Categories.Performance.Score.(float64)
return Result{
URL: url,
Strategy: strategy,
Score: score,
Date: time.Now(),
}, nil
}