forked from justwatch/facebook-marketing-api-golang-sdk
/
event.go
47 lines (39 loc) · 1.08 KB
/
event.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
package v16
import (
"context"
"github.com/JoshuaDietz/facebook-marketing-api-golang-sdk-v19-test/fb"
)
// EventService contains all methods for working on events.
type EventService struct {
c *fb.Client
}
// SimpleList returns event names for a given pixel id.
func (es *EventService) SimpleList(ctx context.Context, pixelID string) ([]string, error) {
type events struct {
EventsByHour []struct {
EventName string `json:"value"`
} `json:"data"`
}
res := []events{}
route := fb.NewRoute(Version, "/%s/stats", pixelID).
Limit(250).
Fields("data{value}").
Aggregation("event")
err := es.c.GetList(ctx, route.String(), &res)
if err != nil {
return nil, err
}
uniqueEventNamesMap := make(map[string]struct{})
for _, event := range res {
for _, eventByHour := range event.EventsByHour {
if eventByHour.EventName != "" {
uniqueEventNamesMap[eventByHour.EventName] = struct{}{}
}
}
}
uniqueEventNames := []string{}
for uniqueEventName := range uniqueEventNamesMap {
uniqueEventNames = append(uniqueEventNames, uniqueEventName)
}
return uniqueEventNames, nil
}