/
reports.go
93 lines (78 loc) · 2.33 KB
/
reports.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package trending_products_service
import (
"context"
"fmt"
analyticsdata "google.golang.org/api/analyticsdata/v1beta"
"google.golang.org/api/option"
)
// Google Analytics Property
type property struct {
id string
key string
}
// TODO
// API Ready Response
// functions currently printf
// formatting is pending
type formattedResponse struct {
response string
}
// Instance returns an Analytics Data service
func (p property) Instance() *analyticsdata.Service {
//Analytics Data client
ctx := context.Background()
client, err := analyticsdata.NewService(ctx, option.WithAPIKey(p.key))
if err != nil {
panic(err)
}
return client
}
func (p property) getPopularProductsByPurchaseAmount() *formattedResponse {
// Run the report for top items purchased by item name.
topItemsPurchasedReportRequest := &analyticsdata.RunReportRequest{
Property: "properties/PROPERTY_ID",
DateRanges: []*analyticsdata.DateRange{
{StartDate: "2023-09-27", EndDate: "2023-09-27"},
},
Dimensions: []*analyticsdata.Dimension{
{Name: "item_name"},
},
Metrics: []*analyticsdata.Metric{
{Name: "total_item_revenue"},
},
Limit: 10,
}
topItemsPurchasedReportResponse, err := p.Instance().Properties.RunReport(p.id, topItemsPurchasedReportRequest).Do()
if err != nil {
panic(err)
}
fmt.Println("Top items purchased by item name:")
for _, row := range topItemsPurchasedReportResponse.Rows {
fmt.Printf("%s: %f\n", row.DimensionValues[0].Value, row.MetricValues[0].Value)
}
}
func (p property) getPopularProductsByViews() *formattedResponse {
// Run the report for top views by page title and screen class.
topViewsReportRequest := &analyticsdata.RunReportRequest{
Property: "properties/YOUR_PROPERTY_ID",
DateRanges: []*analyticsdata.DateRange{
{StartDate: "2023-09-27", EndDate: "2023-09-27"},
},
Dimensions: []*analyticsdata.Dimension{
{Name: "page_title"},
{Name: "screen_class"},
},
Metrics: []*analyticsdata.Metric{
{Name: "total_views"},
},
Limit: 10,
}
topViewsReportResponse, err := p.Instance().Properties.RunReport(p.id, topViewsReportRequest).Do()
if err != nil {
panic(err)
}
fmt.Println("Top views by page title and screen class:")
for _, row := range topViewsReportResponse.Rows {
fmt.Printf("%s (%s): %d\n", row.DimensionValues[0].Value, row.DimensionValues[1].Value, row.MetricValues[0].Value)
}
}