Skip to content

Commit

Permalink
Add visits and events metrics (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
francois2metz authored Sep 25, 2022
1 parent 2420971 commit c17a4a5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,24 @@ To know more about properties, see [Plausible Docs: Filtering](https://plausible
Metrics are aggregate information about the data. All queries have the option for you to choose the metrics you want to
see included in the results.

There are 4 metrics currently that you can ask the results for: number of visitors, number of page views, visit duration
and bounce rate. In this library, these metrics are represented by
There are 6 metrics currently that you can ask the results for: number of visitors, number of page views, visit duration, bounce rate, visits and events.
In this library, these metrics are represented by
the [Metric](https://pkg.go.dev/github.com/andrerfcsantos/go-plausible/plausible#Metric)
type. There are 4 constants of type `Metric`, each one representing one of the 4 metrics: `Visitors`,
`PageViews`, `BounceRate` and `VisitDuration`.
type. There are 6 constants of type `Metric`, each one representing one of the 6 metrics: `Visitors`,
`PageViews`, `BounceRate`, `VisitDuration`, `Events` and `Visits`

For instance, if for a query you only want information about the pageviews and number of visitors, you can pass this to
the query in the metrics parameter:

```go
metrics := plausible.Metrics {
plausible.Visitors,
plausible.PageViews,
plausible.Visitors,
plausible.PageViews,
},
```

For convenience, when you want to get information about all metrics, there's a function `AllMetrics()`
that returns all the 4 metrics. However, please note that not all queries support requests for all metrics. For that
that returns all the 6 metrics. However, please note that not all queries support requests for all metrics. For that
reason, use requests for all metrics with caution. If you try to use a metric in a query that does not support that
metric, you will get an error message saying which property was at fault.

Expand Down
42 changes: 35 additions & 7 deletions plausible/aggregate_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ type AggregateResult struct {
PageviewsChange int `json:"pageviews_change"`

// VisitDuration represents the visit duration result for the query.
// Only use this field if you included the Visit metric in your query
// Only use this field if you included the VisitDuration metric in your query
VisitDuration float64 `json:"visit_duration"`
// VisitDurationChange represents the visit duration change compared to the previous period.
// Only use this field if you included the Visit metric in your query and ComparePreviousPeriod was set to true.
// Only use this field if you included the VisitVisitDuration metric in your query and ComparePreviousPeriod was set to true.
VisitDurationChange float64 `json:"visit_duration_change"`

// Visitors represents the number of visitors result for the query.
Expand All @@ -77,6 +77,20 @@ type AggregateResult struct {
// VisitorsChange represents the change in the number of visitors compared to the previous period.
// Only use this field if you included the Visitors metric in your query and ComparePreviousPeriod was set to true.
VisitorsChange int `json:"visitors_change"`

// Visits represents the visits result for the query.
// Only use this field if you included the Visits metric in your query
Visits int `json:"visits"`
// VisitsChange represents the visits change compared to the previous period.
// Only use this field if you included the Visits metric in your query and ComparePreviousPeriod was set to true.
VisitsChange int `json:"visits_change"`

// Events represents the events result for the query.
// Only use this field if you included the Events metric in your query
Events int `json:"events"`
// EventsChange represents the events change compared to the previous period.
// Only use this field if you included the Events metric in your query and ComparePreviousPeriod was set to true.
EventsChange int `json:"events_change"`
}

type rawAggregateResult struct {
Expand All @@ -85,6 +99,10 @@ type rawAggregateResult struct {
Change float64 `json:"change"`
Value float64 `json:"value"`
} `json:"bounce_rate,omitempty"`
Events struct {
Change int `json:"change"`
Value int `json:"value"`
} `json:"events,omitempty"`
Pageviews struct {
Change int `json:"change"`
Value int `json:"value"`
Expand All @@ -97,6 +115,10 @@ type rawAggregateResult struct {
Change int `json:"change"`
Value int `json:"value"`
} `json:"visitors,omitempty"`
Visits struct {
Change int `json:"change"`
Value int `json:"value"`
} `json:"visits,omitempty"`
} `json:"results,omitempty"`
}

Expand All @@ -107,17 +129,23 @@ func (r *rawAggregateResult) toAggregateResult() AggregateResult {
return res
}

res.BounceRate = r.Result.BounceRate.Value
res.BounceRateChange = r.Result.BounceRate.Change

res.Events = r.Result.Events.Value
res.EventsChange = r.Result.Events.Change

res.Pageviews = r.Result.Pageviews.Value
res.PageviewsChange = r.Result.Pageviews.Change

res.VisitDuration = r.Result.VisitDuration.Value
res.VisitDurationChange = r.Result.VisitDuration.Change

res.Visitors = r.Result.Visitors.Value
res.VisitorsChange = r.Result.Visitors.Change

res.BounceRate = r.Result.BounceRate.Value
res.BounceRateChange = r.Result.BounceRate.Change

res.VisitDuration = r.Result.VisitDuration.Value
res.VisitDurationChange = r.Result.VisitDuration.Change
res.Visits = r.Result.Visits.Value
res.VisitsChange = r.Result.Visits.Change

return res
}
2 changes: 1 addition & 1 deletion plausible/aggregate_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestUnitToQueryArgsAggregateQuery(t *testing.T) {
expectedQueryArgs: QueryArgs{
QueryArg{Name: "period", Value: "custom"},
QueryArg{Name: "date", Value: "2021-01-01,2021-02-01"},
QueryArg{Name: "metrics", Value: "visitors,pageviews,bounce_rate,visit_duration"},
QueryArg{Name: "metrics", Value: "visitors,pageviews,bounce_rate,visit_duration,visits"},
QueryArg{Name: "filters", Value: "visit:os==Windows;event:page==/"},
QueryArg{Name: "compare", Value: "previous_period"},
},
Expand Down
2 changes: 1 addition & 1 deletion plausible/breakdown_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestUnitToQueryArgsBreakdownQuery(t *testing.T) {
expectedQueryArgs: QueryArgs{
QueryArg{Name: "property", Value: "visit:browser"},
QueryArg{Name: "period", Value: "day"},
QueryArg{Name: "metrics", Value: "visitors,pageviews,bounce_rate,visit_duration"},
QueryArg{Name: "metrics", Value: "visitors,pageviews,bounce_rate,visit_duration,visits"},
QueryArg{Name: "limit", Value: "1"},
QueryArg{Name: "page", Value: "1"},
QueryArg{Name: "filters", Value: "visit:os==Windows;event:page==/"},
Expand Down
6 changes: 5 additions & 1 deletion plausible/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ const (
BounceRate = Metric("bounce_rate")
// VisitDuration represents the visit duration metric
VisitDuration = Metric("visit_duration")
// Visits represents the number of visits/sessions metric
Visits = Metric("visits")
// Events represents the number of events (pageviews + custom events) metric
Events = Metric("events")
)

// AllMetrics is an utility function that returns all the metrics.
// This can be used to easily request information about all the metrics when building a query.
// However, please note that querying all metrics is not allowed in all type of queries.
func AllMetrics() Metrics {
return Metrics{
Visitors, PageViews, BounceRate, VisitDuration,
Visitors, PageViews, BounceRate, VisitDuration, Visits,
}
}

Expand Down
2 changes: 1 addition & 1 deletion plausible/timeseries_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestUnitToQueryArgsTimeseriesQuery(t *testing.T) {
expectedQueryArgs: QueryArgs{
QueryArg{Name: "period", Value: "day"},
QueryArg{Name: "filters", Value: "visit:os==Windows"},
QueryArg{Name: "metrics", Value: "visitors,pageviews,bounce_rate,visit_duration"},
QueryArg{Name: "metrics", Value: "visitors,pageviews,bounce_rate,visit_duration,visits"},
QueryArg{Name: "interval", Value: "date"},
},
isValid: true,
Expand Down

0 comments on commit c17a4a5

Please sign in to comment.