forked from himidori/golang-vk-api
/
ads.go
278 lines (220 loc) · 6.81 KB
/
ads.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package vkapi
import (
"encoding/json"
"log"
"net/url"
"strconv"
)
type Ads struct {
client *VKClient
}
func NewAds(client *VKClient) *Ads {
return &Ads{
client: client,
}
}
type CreateTargetGroupResponse struct {
ID int `json:"id"`
}
// id (integer) — идентификатор аудитории;
// name (string) — название аудитории ретаргетинга;
// last_updated (integer) — дата и время последнего пополнения аудитории в формате Unixtime;
// is_audience (integer, 1) — 1, если группа является аудиторией (т.е.может быть пополнена при помощи пикселя);
// is_shared (integer, 1) — 1, если группа является копией (см. метод ads.shareTargetGroup);
// audience_count (integer) — приблизительное количество пользователей, входящих в аудиторию;
// lifetime (integer) — количество дней, через которое пользователи, добавляемые в аудиторию ретаргетинга, будут автоматически исключены из неё;
// file_source (integer, 1) — признак пополнения аудитории через файл;
// api_source (integer, 1) — признак пополнения аудитории через метод ads.importTargetContacts;
// lookalike_source (integer, 1) — признак аудитории, полученной при помощи Look-a-Like.
//
// pixel (string) — код для размещения на сайте рекламодателя. Возвращается, если параметр extended = 1 (только для старых групп).
// domain (string) — домен сайта, где размещен код учета пользователей (только для старых групп).
type TargetGroupResponse struct {
ID int `json:"id"`
Name string `json:"name"`
LastUpdated int `json:"last_updated"`
IsAudience bool `json:"is_audience"`
IsShared bool `json:"is_shared"`
AudienceCount int `json:"audience_count"`
Lifetime int `json:"lifetime"`
FileSource bool `json:"file_source"`
ApiSource bool `json:"api_source"`
LookalikeSource bool `json:"lookalike_source"`
Pixel string `json:"pixel"`
Domain string `json:"domain"`
}
type GetClientResponse []struct {
ID int `json:"id"`
Name string `json:"name"`
DayLimit string `json:"day_limit"`
AllLimit string `json:"all_limit"`
}
type GetAccountResponse struct {
ID int `json:"account_id"`
Type string `json:"account_type"`
Status int `json:"account_status"`
Role string `json:"access_role"`
}
//
func (a *Ads) CreateTargetGroup(AccountID int, Lifetime string, Name string, ClientIDs ...int) ([]CreateTargetGroupResponse, error) {
v := url.Values{}
v.Add("account_id", strconv.Itoa(AccountID))
v.Add("lifetime", Lifetime)
v.Add("name", Name)
create := func() (CreateTargetGroupResponse, error) {
d := CreateTargetGroupResponse{}
resp, err := a.client.MakeRequest("ads.createTargetGroup", v)
if err != nil {
return d, err
}
err = json.Unmarshal(resp.Response, &d)
if err != nil {
log.Println(err, string(resp.Response))
return d, err
}
return d, nil
}
if len(ClientIDs) == 0 {
d, err := create()
if err != nil {
return nil, err
}
return []CreateTargetGroupResponse{d}, nil
}
ds := make([]CreateTargetGroupResponse, len(ClientIDs))
for i, clientID := range ClientIDs {
v.Set("client_id", strconv.Itoa(clientID))
d, err := create()
if err != nil {
return nil, err
}
ds[i] = d
}
return ds, nil
}
func (a *Ads) GetTargetGroup(AccountID int, ClientIDs ...int) ([]TargetGroupResponse, error) {
d := []TargetGroupResponse{}
v := url.Values{}
v.Add("account_id", strconv.Itoa(AccountID))
get := func() error {
resp, err := a.client.MakeRequest("ads.getTargetGroups", v)
if err != nil {
return err
}
ret := []TargetGroupResponse{}
err = json.Unmarshal(resp.Response, &ret)
if err != nil {
log.Println(err, string(resp.Response))
return err
}
d = append(d, ret...)
return nil
}
if len(ClientIDs) == 0 {
if err := get(); err != nil {
return d, err
}
return d, nil
}
for _, ClientID := range ClientIDs {
v.Set("client_id", strconv.Itoa(ClientID))
if err := get(); err != nil {
return d, err
}
}
return d, nil
}
func (a *Ads) UpdateTargetGroup(AccountID string, TargetGroupID int) (int, error) {
d := 0
return d, nil
}
//
func (a *Ads) DeleteTargetGroup(AccountID int, TargetGroupID int, ClientIDs ...int) ([]int, error) {
v := url.Values{}
v.Add("account_id", strconv.Itoa(AccountID))
v.Add("target_group_id", strconv.Itoa(TargetGroupID))
delete := func() (int, error) {
resp, err := a.client.MakeRequest("ads.deleteTargetGroup", v)
if err != nil {
return 0, err
}
var d int
err = json.Unmarshal(resp.Response, &d)
if err != nil {
return 0, err
}
return d, nil
}
if len(ClientIDs) == 0 {
d, err := delete()
if err != nil {
return nil, err
}
return []int{d}, nil
}
ds := make([]int, len(ClientIDs))
for i, clientID := range ClientIDs {
v.Set("client_id", strconv.Itoa(clientID))
d, err := delete()
if err != nil {
return nil, err
}
ds[i] = d
}
return ds, nil
}
// Возвращает количество обработанных контактов.
type ImportTargetContactsResponse struct {
Response int `json:"response"`
}
// Examples:
// ImportTargetContacts
//
func (a *Ads) ImportTargetContacts(
accountID int,
targetGroupID int,
contacts string) (ImportTargetContactsResponse, error) {
d := ImportTargetContactsResponse{}
v := url.Values{}
v.Add("account_id", strconv.Itoa(accountID))
v.Add("target_group_id", strconv.Itoa(targetGroupID))
v.Add("contacts", contacts)
resp, err := a.client.MakeRequest("ads.importTargetContacts", v)
if err != nil {
return d, err
}
err = json.Unmarshal(resp.Response, &d.Response)
if err != nil {
return d, err
}
return d, nil
}
// Examples:
// GetClients
//
func (a *Ads) GetClients(AccountID int) (GetClientResponse, error) {
d := GetClientResponse{}
v := url.Values{}
v.Add("account_id", strconv.Itoa(AccountID))
resp, err := a.client.MakeRequest("ads.getClients", v)
if err != nil {
return d, err
}
err = json.Unmarshal(resp.Response, &d)
if err != nil {
return d, err
}
return d, nil
}
func (a *Ads) GetAccounts() ([]GetAccountResponse, error) {
resp, err := a.client.MakeRequest("ads.getAccounts", nil)
if err != nil {
return nil, err
}
ret := make([]GetAccountResponse, 0)
err = json.Unmarshal(resp.Response, &ret)
if err != nil {
return nil, err
}
return ret, nil
}