-
Notifications
You must be signed in to change notification settings - Fork 22
/
ads.go
154 lines (131 loc) · 4.18 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
package v16
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/justwatch/facebook-marketing-api-golang-sdk/fb"
"golang.org/x/sync/errgroup"
)
// AdService works with Ads.
type AdService struct {
c *fb.Client
}
// Get returns a single ad.
func (as *AdService) Get(ctx context.Context, id string) (*Ad, error) {
res := &Ad{}
err := as.c.GetJSON(ctx, fb.NewRoute(Version, "/%s", id).Fields("id", "creative", "name", "account_id", "adset_id",
"adset{id,daily_budget,name,start_time,end_time,status,bid_strategy,targeting{age_min,age_max,publisher_platforms,geo_locations,genders,custom_audiences,excluded_custom_audiences,flexible_spec,exclusions}}",
"adcreatives{id,title,object_story_spec}").Limit(1000).String(), res)
if err != nil {
if fb.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return res, nil
}
// Create uploads a new ad, returns the fields and returns the created ad.
func (as *AdService) Create(ctx context.Context, a Ad) (string, error) {
if a.ID != "" {
return "", fmt.Errorf("cannot create ad that already exists: %s", a.ID)
} else if a.AccountID == "" {
return "", errors.New("cannot create ad without account id")
}
res := &fb.MinimalResponse{}
err := as.c.PostJSON(ctx, fb.NewRoute(Version, "/act_%s/ads", a.AccountID).String(), a, res)
if err != nil {
return "", err
} else if err = res.GetError(); err != nil {
return "", err
} else if res.ID == "" {
return "", fmt.Errorf("creating ad failed")
}
return res.ID, nil
}
// Update updates an ad.
func (as *AdService) Update(ctx context.Context, a Ad) error {
if a.ID == "" {
return errors.New("cannot update ad without id")
}
res := &fb.MinimalResponse{}
err := as.c.PostJSON(ctx, fb.NewRoute(Version, "/%s", a.ID).String(), a, res)
if err != nil {
return err
} else if err = res.GetError(); err != nil {
return err
} else if !res.Success && res.ID == "" {
return fmt.Errorf("updating the ad failed")
}
return nil
}
// List returns all ads of an account.
func (as *AdService) List(act string) *AdListCall {
return &AdListCall{
RouteBuilder: fb.NewRoute(Version, "/act_%s/ads", act).Fields("adset_id", "creative", "id", "name", "account_id", "adset{id}", "adcreatives{id}").Limit(1000),
c: as.c,
}
}
// ListOfAdset returns all ads of an adset.
func (as *AdService) ListOfAdset(adsetID string) *AdListCall {
return &AdListCall{
RouteBuilder: fb.NewRoute(Version, "/%s/ads", adsetID).Fields("id", "adset{id}", "adcreatives{id}").Limit(1000),
c: as.c,
}
}
// AdListCall is used for Listing ads.
type AdListCall struct {
*fb.RouteBuilder
c *fb.Client
}
// Do calls the graph API.
func (as *AdListCall) Do(ctx context.Context) ([]Ad, error) {
res := []Ad{}
err := as.c.GetList(ctx, as.RouteBuilder.String(), &res)
if err != nil {
return nil, err
}
return res, nil
}
// Do calls the graph API.
func (as *AdListCall) Read(ctx context.Context, c chan<- Ad) error {
jres := make(chan json.RawMessage)
wg := errgroup.Group{}
wg.Go(func() error {
defer close(jres)
return as.c.ReadList(ctx, as.RouteBuilder.String(), jres)
})
wg.Go(func() error {
for e := range jres {
v := Ad{}
err := json.Unmarshal(e, &v)
if err != nil {
return err
}
c <- v
}
return nil
})
return wg.Wait()
}
// Ad represents a Facebook Ad.
type Ad struct {
AccountID string `json:"account_id,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
AdsetID string `json:"adset_id,omitempty"`
Creative *AdCreative `json:"creative,omitempty"`
Adset *Adset `json:"adset,omitempty"`
TrackingSpecs []ConversionActionQuery `json:"tracking_specs,omitempty"`
Adcreatives *struct {
Data []AdCreative `json:"data,omitempty"`
} `json:"adcreatives,omitempty"`
}
// ConversionActionQuery contains tracking specs.
type ConversionActionQuery struct {
ActionType []string `json:"action.type,omitempty"`
FbPixel []string `json:"fb_pixel,omitempty"`
Page []string `json:"page,omitempty"`
Post []string `json:"post,omitempty"`
}