/
search.go
159 lines (137 loc) · 4.15 KB
/
search.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package v16
import (
"context"
"github.com/justwatch/facebook-marketing-api-golang-sdk/fb"
)
// SearchService performs searches on the graph API.
type SearchService struct {
c *fb.Client
}
const (
getGeoLocationLimit = 5000
getDevicesResultsLimit = 5000
)
// GetAdGeoLocations returns all AdGeoLocations.
func (s *SearchService) GetAdGeoLocations(ctx context.Context) ([]AdGeoLocation, error) {
rb := fb.NewRoute(Version, "/search").
Type("adgeolocation").
LocationTypes("country", "city", "region").
Limit(getGeoLocationLimit)
res := []AdGeoLocation{}
err := s.c.GetList(ctx, rb.String(), &res)
if err != nil {
return nil, err
}
return res, nil
}
func (s *SearchService) GetRegions(ctx context.Context, country string) ([]AdGeoLocation, error) {
rb := fb.NewRoute(Version, "/search").
Type("adgeolocation").
LocationTypes("region").
Limit(getGeoLocationLimit)
if country != "" {
rb.Q(country)
}
res := []AdGeoLocation{}
err := s.c.GetList(ctx, rb.String(), &res)
if err != nil {
return nil, err
}
return res, nil
}
// AdGeoLocation contains different geolocation types to be used for adset targeting.
type AdGeoLocation struct {
Key string `json:"key"`
Name string `json:"name"`
Type string `json:"type"`
CountryCode string `json:"country_code"`
SupportsRegion bool `json:"supports_region"`
SupportsCity bool `json:"supports_city"`
}
// GetDevices returns all devices.
func (s *SearchService) GetDevices(ctx context.Context) ([]Device, error) {
rb := fb.NewRoute(Version, "/search").
Type("adTargetingCategory").
Class("user_device").
Limit(getDevicesResultsLimit)
res := []Device{}
err := s.c.GetList(ctx, rb.String(), &res)
if err != nil {
return nil, err
}
return res, nil
}
// Device contains device information to be used for adset targeting.
type Device struct {
Name string `json:"name"`
Description string `json:"description"`
Platform string `json:"platform"`
AudienceSize int `json:"audience_size"`
Type string `json:"type"`
}
// GetOperatingSystems returns all operating systems.
func (s *SearchService) GetOperatingSystems(ctx context.Context) ([]OperatingSystem, error) {
rb := fb.NewRoute(Version, "/search").
Type("adTargetingCategory").
Class("user_os").
Limit(getDevicesResultsLimit)
res := []OperatingSystem{}
err := s.c.GetList(ctx, rb.String(), &res)
if err != nil {
return nil, err
}
return res, nil
}
// OperatingSystem contains information about an user OS to be used for adset targeting.
type OperatingSystem struct {
Type string `json:"type"`
Name string `json:"name"`
Platform string `json:"platform"`
Description string `json:"description"`
}
// GetAdLocales returns all ad locales.
func (s *SearchService) GetAdLocales(ctx context.Context) ([]AdLocale, error) {
rb := fb.NewRoute(Version, "/search").
Type("adlocale").
Limit(1000)
res := []AdLocale{}
err := s.c.GetList(ctx, rb.String(), &res)
if err != nil {
return nil, err
}
return res, nil
}
// AdLocale can be used for locale adset targeting.
type AdLocale struct {
Key int `json:"key"`
Name string `json:"name"`
}
// TargetingOptionStatus is the json response of the ValidateInterest request.
type TargetingOptionStatus struct {
ID string `json:"id"`
CurrentStatus string `json:"current_status"`
FuturePlan []struct {
Key string `json:"key"`
Value string `json:"value"`
} `json:"future_plan"`
}
// ValidateInterests validates a list of interests and returns a list of valid and a list of invalid IDs.
func (s *SearchService) ValidateInterests(ctx context.Context, externalIDs []string) (validIDs []string, invalidIDs []string, err error) {
rb := fb.NewRoute(Version, "/search").
Type("targetingoptionstatus").
TargetingOptionList(externalIDs...).
Limit(1000)
var res []TargetingOptionStatus
err = s.c.GetList(ctx, rb.String(), &res)
if err != nil {
return nil, nil, err
}
for _, targetingOption := range res {
if targetingOption.CurrentStatus == "NORMAL" {
validIDs = append(validIDs, targetingOption.ID)
} else {
invalidIDs = append(invalidIDs, targetingOption.ID)
}
}
return validIDs, invalidIDs, nil
}