forked from segmentio/gads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraffic_estimator.go
144 lines (127 loc) · 3.53 KB
/
traffic_estimator.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
package gads
import "encoding/xml"
type TrafficEstimatorService struct {
Auth
}
func NewTrafficEstimatorService(auth *Auth) *TrafficEstimatorService {
return &TrafficEstimatorService{Auth: *auth}
}
type KeywordEstimateRequest struct {
Keyword KeywordCriterion `xml:"keyword"`
}
type AdGroupEstimateRequest struct {
KeywordEstimateRequests []KeywordEstimateRequest `xml:"keywordEstimateRequests"`
MaxCpc int64 `xml:"https://adwords.google.com/api/adwords/cm/v201809 maxCpc>microAmount"`
}
type CampaignEstimateRequest struct {
AdGroupEstimateRequests []AdGroupEstimateRequest `xml:"adGroupEstimateRequests"`
}
type TrafficEstimatorSelector struct {
CampaignEstimateRequests []CampaignEstimateRequest `xml:"campaignEstimateRequests"`
}
type StatsEstimate struct {
AverageCpc int64 `xml:"averageCpc>microAmount"`
AveragePosition float64 `xml:"averagePosition"`
ClickThroughRate float64 `xml:"clickThroughRate"`
ClicksPerDay float64 `xml:"clicksPerDay"`
ImpressionsPerDay float64 `xml:"impressionsPerDay"`
TotalCost int64 `xml:"totalCost>microAmount"`
}
type KeywordEstimate struct {
Min StatsEstimate `xml:"min"`
Max StatsEstimate `xml:"max"`
}
type AdGroupEstimate struct {
KeywordEstimates []KeywordEstimate `xml:"keywordEstimates"`
}
type CampaignEstimate struct {
AdGroupEstimates []AdGroupEstimate `xml:"adGroupEstimates"`
}
// Get returns an array of CampaignEstimates, holding AdGroupEstimates which
// hold KeywordEstimates, which hold the minimum and maximum values based on
// the requested keywords.
//
// Example
//
// keywordEstimateRequests := []KeywordEstimateRequest{
// KeywordEstimateRequest{
// KeywordCriterion{
// Text: "mars cruise",
// MatchType: "BROAD",
// },
// },
// KeywordEstimateRequest{
// KeywordCriterion{
// Text: "cheap cruise",
// MatchType: "EXACT",
// },
// },
// KeywordEstimateRequest{
// KeywordCriterion{
// Text: "cruise",
// MatchType: "EXACT",
// },
// },
// }
//
// adGroupEstimateRequests := []AdGroupEstimateRequest{
// AdGroupEstimateRequest{
// KeywordEstimateRequests: keywordEstimateRequests,
// MaxCpc: 1000000,
// },
// }
//
// campaignEstimateRequests := []CampaignEstimateRequest{
// CampaignEstimateRequest{
// AdGroupEstimateRequests: adGroupEstimateRequests,
// },
// }
//
// estimates, err := trafficEstimatorService.Get(TrafficEstimatorSelector{
// CampaignEstimateRequests: campaignEstimateRequests,
// })
//
// if err != nil {
// panic(err)
// }
//
// for _, estimate := range estimates {
// for _, adGroupEstimate := range estimate.AdGroupEstimates {
// for _, keywordEstimate := range adGroupEstimate.KeywordEstimates {
// fmt.Printf("Avg cpc: %d", keywordEstimate.Min.AverageCpc)
// }
// }
// }
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/TrafficEstimatorService#get
//
func (s *TrafficEstimatorService) Get(selector TrafficEstimatorSelector) (res []CampaignEstimate, err error) {
respBody, err := s.Auth.request(
trafficEstimatorServiceUrl,
"get",
struct {
XMLName xml.Name
Sel TrafficEstimatorSelector `xml:"selector"`
}{
XMLName: xml.Name{
Space: baseTrafficUrl,
Local: "get",
},
Sel: selector,
},
nil,
)
if err != nil {
return res, err
}
getResp := struct {
CampaignEstimates []CampaignEstimate `xml:"rval>campaignEstimates"`
}{}
err = xml.Unmarshal([]byte(respBody), &getResp)
if err != nil {
return res, err
}
return getResp.CampaignEstimates, err
}