forked from segmentio/gads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
254 lines (233 loc) · 7.51 KB
/
data.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package gads
import (
"encoding/xml"
)
type DataService struct {
Auth
}
func NewDataService(auth *Auth) *DataService {
return &DataService{Auth: *auth}
}
type LandscapePoint struct {
Bid *int64 `xml:"bid>microAmount,omitempty"`
Clicks *int64 `xml:"clicks,omitempty"`
Cost *int64 `xml:"cost>microAmount,omitempty"`
Impressions *int64 `xml:"impressions,omitempty"`
PromotedImpressions *int64 `xml:"promotedImpressions,omitempty"`
}
type BidLandscape struct {
CampaignId *int64 `xml:"campaignId,omitempty"`
AdGroupId *int64 `xml:"adGroupId,omitempty"`
StartDate *string `xml:"startDate,omitempty"`
EndDate *string `xml:"endDate,omitempty"`
LandscapePoints []LandscapePoint `xml:"landscapePoints"`
}
type AdGroupBidLandscape struct {
BidLandscape
Type string `xml:"type"`
LandscapeCurrent bool `xml:"landscapeCurrent"`
Errors []error `xml:"-"`
}
type CriterionBidLandscape struct {
BidLandscape
CriterionId *int64 `xml:"criterionId,omitempty"`
}
// GetAdGroupBidLandscape returns an array of AdGroupBidLandscape objects
// the selector.
//
// Example
//
// adGroupBidLandscape, totalCount, err := dataService.GetAdGroupBidLandscape(
// gads.Selector{
// Fields: []string{
// "AdGroupId",
// "Bid",
// "CampaignId",
// "LocalClicks",
// "LocalCost",
// "LocalImpressions",
// },
// Predicates: []gads.Predicate{
// {"AdGroupId", "EQUALS", []string{adGroupId}},
// },
// },
// )
//
// Selectable fields are
// "AdGroupId", "Bid", "CampaignId", "EndDate", "LandscapeCurrent", "LandscapeType", "LocalClicks",
// "LocalCost", "LocalImpressions", "PromotedImpressions", "StartDate"
//
// filterable fields are
// "AdGroupId", "Bid", "CampaignId", "LandscapeCurrent", "LandscapeType", "LocalClicks",
// "LocalCost", "LocalImpressions", "PromotedImpressions"
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/DataService#getadgroupbidlandscape
// https://developers.google.com/adwords/api/docs/appendix/selectorfields#v201809-DataService
//
func (s *DataService) GetAdGroupBidLandscape(selector Selector) (adGroupBidLandscapes []AdGroupBidLandscape, totalCount int64, err error) {
// The default namespace, "", will break in 1.5 with the addition of
// custom namespace support. Hence, we have to ensure that the baseUrl is
// set again as the proper namespace for the service/serviceSelector element
selector.XMLName = xml.Name{baseUrl, "serviceSelector"}
respBody, err := s.Auth.request(
dataServiceUrl,
"getAdGroupBidLandscape",
struct {
XMLName xml.Name
Sel Selector
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "getAdGroupBidLandscape",
},
Sel: selector,
},
nil,
)
if err != nil {
return adGroupBidLandscapes, totalCount, err
}
getResp := struct {
Size int64 `xml:"rval>totalNumEntries"`
AdGroupBidLandscapes []AdGroupBidLandscape `xml:"rval>entries"`
}{}
err = xml.Unmarshal([]byte(respBody), &getResp)
if err != nil {
return adGroupBidLandscapes, totalCount, err
}
return getResp.AdGroupBidLandscapes, getResp.Size, err
}
// GetCriterionBidLandscape returns an array of CriterionBidLandscape objects
// the selector.
//
// Example
//
// criterionBidLandscape, totalCount, err := dataService.GetCriterionBidLandscape(
// gads.Selector{
// Fields: []string{
// "AdGroupId",
// "CriterionId",
// "Bid",
// "CampaignId",
// "LocalClicks",
// "LocalCost",
// "LocalImpressions",
// },
// Predicates: []gads.Predicate{
// {"CriterionId", "EQUALS", []string{criterionId}},
// },
// },
// )
//
// Selectable fields are
// "AdGroupId", "Bid", "CampaignId", "CriterionId", "EndDate", "LandscapeCurrent", "LandscapeType", "LocalClicks",
// "LocalCost", "LocalImpressions", "PromotedImpressions", "StartDate"
//
// filterable fields are
// "AdGroupId", "Bid", "CampaignId", "CriterionId", "LandscapeCurrent", "LandscapeType", "LocalClicks",
// "LocalCost", "LocalImpressions", "PromotedImpressions"
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/DataService#getcriterionbidlandscape
// https://developers.google.com/adwords/api/docs/appendix/selectorfields#v201809-DataService
//
func (s *DataService) GetCriterionBidLandscape(selector Selector) (criterionBidLandscapes []CriterionBidLandscape, totalCount int64, err error) {
// The default namespace, "", will break in 1.5 with the addition of
// custom namespace support. Hence, we have to ensure that the baseUrl is
// set again as the proper namespace for the service/serviceSelector element
selector.XMLName = xml.Name{baseUrl, "serviceSelector"}
respBody, err := s.Auth.request(
dataServiceUrl,
"getCriterionBidLandscape",
struct {
XMLName xml.Name
Sel Selector
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "getCriterionBidLandscape",
},
Sel: selector,
},
nil,
)
if err != nil {
return criterionBidLandscapes, totalCount, err
}
getResp := struct {
Size int64 `xml:"rval>totalNumEntries"`
CriterionBidLandscapes []CriterionBidLandscape `xml:"rval>entries"`
}{}
err = xml.Unmarshal([]byte(respBody), &getResp)
if err != nil {
return criterionBidLandscapes, totalCount, err
}
return getResp.CriterionBidLandscapes, getResp.Size, err
}
// QueryAdGroupBidLandscape returns a slice of AdGroupBidLandscapes based on passed in AWQL query
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/DataService#queryadgroupbidlandscape
//
func (s *DataService) QueryAdGroupBidLandscape(query string) (adGroupBidLandscapes []AdGroupBidLandscape, totalCount int64, err error) {
respBody, err := s.Auth.request(
dataServiceUrl,
"queryAdGroupBidLandscape",
AWQLQuery{
XMLName: xml.Name{
Space: baseUrl,
Local: "queryAdGroupBidLandscape",
},
Query: query,
},
nil,
)
if err != nil {
return adGroupBidLandscapes, totalCount, err
}
getResp := struct {
Size int64 `xml:"rval>totalNumEntries"`
AdGroupBidLandscapes []AdGroupBidLandscape `xml:"rval>entries"`
}{}
err = xml.Unmarshal([]byte(respBody), &getResp)
if err != nil {
return adGroupBidLandscapes, totalCount, err
}
return getResp.AdGroupBidLandscapes, getResp.Size, err
}
// QueryCriterionBidLandscape returns a slice of CriterionBidLandscapes based on passed in AWQL query
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/DataService#querycriterionbidlandscape
//
func (s *DataService) QueryCriterionBidLandscape(query string) (criterionBidLandscapes []CriterionBidLandscape, totalCount int64, err error) {
respBody, err := s.Auth.request(
dataServiceUrl,
"queryCriterionBidLandscape",
AWQLQuery{
XMLName: xml.Name{
Space: baseUrl,
Local: "queryCriterionBidLandscape",
},
Query: query,
},
nil,
)
if err != nil {
return criterionBidLandscapes, totalCount, err
}
getResp := struct {
Size int64 `xml:"rval>totalNumEntries"`
CriterionBidLandscapes []CriterionBidLandscape `xml:"rval>entries"`
}{}
err = xml.Unmarshal([]byte(respBody), &getResp)
if err != nil {
return criterionBidLandscapes, totalCount, err
}
return getResp.CriterionBidLandscapes, getResp.Size, err
}