forked from justwatch/facebook-marketing-api-golang-sdk
/
custom_conversion.go
109 lines (97 loc) · 3.42 KB
/
custom_conversion.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
package v16
import (
"context"
"errors"
"fmt"
"github.com/flycan/facebook-marketing-api-golang-sdk/fb"
)
// CustomConversionService contains all methods for working on custom conversions.
type CustomConversionService struct {
c *fb.Client
}
// Create uploads a new custom conversion and returns the id of the custom conversion.
func (ccs *CustomConversionService) Create(ctx context.Context, businessID string, cc CustomConversion) (string, error) {
if cc.ID != "" {
return "", fmt.Errorf("cannot create custom conversion that already exists: %s", cc.ID)
} else if businessID == "" {
return "", errors.New("cannot create custom conversion without business id")
}
res := &fb.MinimalResponse{}
err := ccs.c.PostJSON(ctx, fb.NewRoute(Version, "/%s/customconversions", businessID).String(), cc, res)
if err != nil {
return "", err
} else if err = res.GetError(); err != nil {
return "", err
} else if res.ID == "" {
return "", fmt.Errorf("creating custom conversion failed")
}
return res.ID, nil
}
// List returns all custom conversions for the specified account.
func (ccs *CustomConversionService) List(ctx context.Context, act string) ([]CustomConversion, error) {
customConversions := []CustomConversion{}
route := fb.NewRoute(Version, "/act_%s/customconversions", act).
Limit(250).
Fields(
"id",
"account_id",
"aggregation_rule",
"business",
"name",
"rule",
"custom_event_type",
"creation_time",
"data_sources",
"description",
"default_conversion_value",
"last_fired_time",
"is_unavailable",
"is_archived",
"first_fired_time",
// "pixel",
"offline_conversion_data_set",
"event_source_type",
"retention_days",
)
err := ccs.c.GetList(ctx, route.String(), &customConversions)
if err != nil {
return nil, err
}
return customConversions, nil
}
// CustomConversion https://developers.facebook.com/docs/marketing-api/reference/custom-conversion/
type CustomConversion struct {
ID string `json:"id"`
AccountID string `json:"account_id"`
AggregationRule string `json:"aggregation_rule"`
Business Business `json:"business"`
Name string `json:"name"`
Rule string `json:"rule"`
CustomEventType string `json:"custom_event_type"`
CreationTime fb.Time `json:"creation_time"`
DataSources []DataSource `json:"data_sources"`
Description string `json:"description"`
DefaultConversionValue int `json:"default_conversion_value"`
LastFiredTime fb.Time `json:"last_fired_time"`
IsUnavailable bool `json:"is_unavailable"`
IsArchived bool `json:"is_archived"`
FirstFiredTime fb.Time `json:"first_fired_time"`
Pixel Pixel `json:"pixel"`
OfflineConversionDataSet interface{} `json:"offline_conversion_data_set"`
EventSourceType string `json:"event_source_type"`
RetentionDays int `json:"retention_days"`
}
// DataSource is part of a CustomConversion.
type DataSource struct {
ID string `json:"id"`
SourceType string `json:"source_type"`
}
// Pixel is part of a CustomConversion.
type Pixel struct {
ID string `json:"id"`
}
// Business is part of a CustomConversion.
type Business struct {
ID string `json:"id"`
Name string `json:"name"`
}