From 44348bb1c1a2c19553bd1589c9358f65643e4836 Mon Sep 17 00:00:00 2001 From: hirokisan Date: Thu, 9 Mar 2023 10:07:39 +0900 Subject: [PATCH] feat: add method to retrieve value --- run_report.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/run_report.go b/run_report.go index 8a65fd2..9de6f48 100644 --- a/run_report.go +++ b/run_report.go @@ -2,6 +2,7 @@ package ga4data import ( "context" + "fmt" analyticsdata "google.golang.org/api/analyticsdata/v1beta" ) @@ -47,6 +48,46 @@ func (r *RunReportResponse) MergeResponse(resp *analyticsdata.RunReportResponse) } } +// DimensionValue : +func (r *RunReportResponse) DimensionValue(row *analyticsdata.Row, column string) analyticsdata.DimensionValue { + return *row.DimensionValues[r.DimensionIndex(column)] +} + +// Dimension : +func (r *RunReportResponse) Dimension(row *analyticsdata.Row, column string) string { + return r.DimensionValue(row, column).Value +} + +// DimensionIndex : +func (r *RunReportResponse) DimensionIndex(column string) int { + for i, header := range r.DimensionHeaders { + if column == header.Name { + return i + } + } + panic(fmt.Sprintf("column %s is not found", column)) +} + +// MetricValue : +func (r *RunReportResponse) MetricValue(row *analyticsdata.Row, column string) analyticsdata.MetricValue { + return *row.MetricValues[r.MetricIndex(column)] +} + +// Metric : +func (r *RunReportResponse) Metric(row *analyticsdata.Row, column string) string { + return r.MetricValue(row, column).Value +} + +// MetricIndex : +func (r *RunReportResponse) MetricIndex(column string) int { + for i, header := range r.MetricHeaders { + if column == header.Name { + return i + } + } + panic(fmt.Sprintf("column %s is not found", column)) +} + // RunReport : func RunReport( ctx context.Context,