-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
603 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package mailchimp | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
) | ||
|
||
const ( | ||
reports_summary_endpoint string = "/3.0/reports" | ||
) | ||
|
||
var mailchimp_datacenter = regexp.MustCompile("[a-z]+[0-9]+$") | ||
|
||
type ChimpAPI struct { | ||
Transport http.RoundTripper | ||
endpoint string | ||
Debug bool | ||
} | ||
|
||
func NewChimp(apiKey string) *ChimpAPI { | ||
u := url.URL{} | ||
u.Scheme = "https" | ||
u.Host = fmt.Sprintf("%s.api.mailchimp.com", mailchimp_datacenter.FindString(apiKey)) | ||
u.Path = reports_summary_endpoint | ||
u.User = url.UserPassword("", apiKey) | ||
return &ChimpAPI{endpoint: u.String()} | ||
} | ||
|
||
type APIError struct { | ||
Status int `json:"status"` | ||
Type string `json:"type"` | ||
Title string `json:"title"` | ||
Detail string `json:"detail"` | ||
Instance string `json:"instance"` | ||
} | ||
|
||
func (e APIError) Error() string { | ||
return fmt.Sprintf("ERROR %v: %v. See %v", e.Status, e.Title, e.Type) | ||
} | ||
|
||
func chimpErrorCheck(body []byte) error { | ||
var e APIError | ||
json.Unmarshal(body, &e) | ||
if e.Title != "" || e.Status != 0 { | ||
return e | ||
} | ||
return nil | ||
} | ||
|
||
func (a *ChimpAPI) GetReports() (ReportsResponse, error) { | ||
var response ReportsResponse | ||
rawjson, err := runChimp(a) | ||
if err != nil { | ||
return response, err | ||
} | ||
|
||
err = json.Unmarshal(rawjson, &response) | ||
if err != nil { | ||
return response, err | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
func runChimp(api *ChimpAPI) ([]byte, error) { | ||
if api.Debug { | ||
log.Printf("Request URL: %s", api.endpoint) | ||
} | ||
client := &http.Client{Transport: api.Transport} | ||
resp, err := client.Get(api.endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if api.Debug { | ||
log.Printf("Response Body:%s", string(body)) | ||
} | ||
|
||
if err = chimpErrorCheck(body); err != nil { | ||
return nil, err | ||
} | ||
return body, nil | ||
} | ||
|
||
type ReportsResponse struct { | ||
Reports []Report `json:"reports"` | ||
TotalItems int `json:"total_items"` | ||
} | ||
|
||
type Report struct { | ||
ID string `json:"id"` | ||
CampaignTitle string `json:"campaign_title"` | ||
Type string `json:"type"` | ||
EmailsSent int `json:"emails_sent"` | ||
AbuseReports int `json:"abuse_reports"` | ||
Unsubscribed int `json:"unsubscribed"` | ||
SendTime string `json:"send_time"` | ||
|
||
TimeSeries []TimeSerie | ||
Bounces Bounces `json:"bounces"` | ||
Forwards Forwards `json:"forwards"` | ||
Opens Opens `json:"opens"` | ||
Clicks Clicks `json:"clicks"` | ||
FacebookLikes FacebookLikes `json:"facebook_likes"` | ||
IndustryStats IndustryStats `json:"industry_stats"` | ||
ListStats ListStats `json:"list_stats"` | ||
} | ||
|
||
type Bounces struct { | ||
HardBounces int `json:"hard_bounces"` | ||
SoftBounces int `json:"soft_bounces"` | ||
SyntaxErrors int `json:"syntax_errors"` | ||
} | ||
|
||
type Forwards struct { | ||
ForwardsCount int `json:"forwards_count"` | ||
ForwardsOpens int `json:"forwards_opens"` | ||
} | ||
|
||
type Opens struct { | ||
OpensTotal int `json:"opens_total"` | ||
UniqueOpens int `json:"unique_opens"` | ||
OpenRate float64 `json:"open_rate"` | ||
LastOpen string `json:"last_open"` | ||
} | ||
|
||
type Clicks struct { | ||
ClicksTotal int `json:"clicks_total"` | ||
UniqueClicks int `json:"unique_clicks"` | ||
UniqueSubscriberClicks int `json:"unique_subscriber_clicks"` | ||
ClickRate float64 `json:"click_rate"` | ||
LastClick string `json:"last_click"` | ||
} | ||
|
||
type FacebookLikes struct { | ||
RecipientLikes int `json:"recipient_likes"` | ||
UniqueLikes int `json:"unique_likes"` | ||
FacebookLikes int `json:"facebook_likes"` | ||
} | ||
|
||
type IndustryStats struct { | ||
Type string `json:"type"` | ||
OpenRate float64 `json:"open_rate"` | ||
ClickRate float64 `json:"click_rate"` | ||
BounceRate float64 `json:"bounce_rate"` | ||
UnopenRate float64 `json:"unopen_rate"` | ||
UnsubRate float64 `json:"unsub_rate"` | ||
AbuseRate float64 `json:"abuse_rate"` | ||
} | ||
|
||
type ListStats struct { | ||
SubRate float64 `json:"sub_rate"` | ||
UnsubRate float64 `json:"unsub_rate"` | ||
OpenRate float64 `json:"open_rate"` | ||
ClickRate float64 `json:"click_rate"` | ||
} | ||
|
||
type TimeSerie struct { | ||
TimeStamp string `json:"timestamp"` | ||
EmailsSent int `json:"emails_sent"` | ||
UniqueOpens int `json:"unique_opens"` | ||
RecipientsClick int `json:"recipients_click"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package mailchimp | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/influxdb/telegraf/plugins" | ||
) | ||
|
||
type MailChimp struct { | ||
api *ChimpAPI | ||
|
||
ApiKey string | ||
} | ||
|
||
var sampleConfig = ` | ||
# MailChimp API key | ||
api_key = "" | ||
` | ||
|
||
func (m *MailChimp) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
func (m *MailChimp) Description() string { | ||
return "Gathers metrics from the /reports MailChimp API" | ||
} | ||
|
||
func (m *MailChimp) Gather(acc plugins.Accumulator) error { | ||
if m.api == nil { | ||
m.api = NewChimp(m.ApiKey) | ||
} | ||
m.api.Debug = false | ||
|
||
reports, err := m.api.GetReports() | ||
if err != nil { | ||
return err | ||
} | ||
now := time.Now() | ||
|
||
for _, report := range reports.Reports { | ||
tags := make(map[string]string) | ||
tags["id"] = report.ID | ||
tags["campaign_title"] = report.CampaignTitle | ||
acc.Add("emails_sent", report.EmailsSent, tags, now) | ||
acc.Add("abuse_reports", report.AbuseReports, tags, now) | ||
acc.Add("unsubscribed", report.Unsubscribed, tags, now) | ||
acc.Add("hard_bounces", report.Bounces.HardBounces, tags, now) | ||
acc.Add("soft_bounces", report.Bounces.SoftBounces, tags, now) | ||
acc.Add("syntax_errors", report.Bounces.SyntaxErrors, tags, now) | ||
acc.Add("forwards_count", report.Forwards.ForwardsCount, tags, now) | ||
acc.Add("forwards_opens", report.Forwards.ForwardsOpens, tags, now) | ||
acc.Add("opens_total", report.Opens.OpensTotal, tags, now) | ||
acc.Add("unique_opens", report.Opens.UniqueOpens, tags, now) | ||
acc.Add("open_rate", report.Opens.OpenRate, tags, now) | ||
acc.Add("last_open", report.Opens.LastOpen, tags, now) | ||
acc.Add("clicks_total", report.Clicks.ClicksTotal, tags, now) | ||
acc.Add("unique_clicks", report.Clicks.UniqueClicks, tags, now) | ||
acc.Add("unique_subscriber_clicks", report.Clicks.UniqueSubscriberClicks, tags, now) | ||
acc.Add("click_rate", report.Clicks.ClickRate, tags, now) | ||
acc.Add("last_click", report.Clicks.LastClick, tags, now) | ||
acc.Add("facebook_recipient_likes", report.FacebookLikes.RecipientLikes, tags, now) | ||
acc.Add("facebook_unique_likes", report.FacebookLikes.UniqueLikes, tags, now) | ||
acc.Add("facebook_likes", report.FacebookLikes.FacebookLikes, tags, now) | ||
acc.Add("industry_type", report.IndustryStats.Type, tags, now) | ||
acc.Add("industry_open_rate", report.IndustryStats.OpenRate, tags, now) | ||
acc.Add("industry_click_rate", report.IndustryStats.ClickRate, tags, now) | ||
acc.Add("industry_bounce_rate", report.IndustryStats.BounceRate, tags, now) | ||
acc.Add("industry_unopen_rate", report.IndustryStats.UnopenRate, tags, now) | ||
acc.Add("industry_unsub_rate", report.IndustryStats.UnsubRate, tags, now) | ||
acc.Add("industry_abuse_rate", report.IndustryStats.AbuseRate, tags, now) | ||
acc.Add("list_stats_sub_rate", report.ListStats.SubRate, tags, now) | ||
acc.Add("list_stats_unsub_rate", report.ListStats.UnsubRate, tags, now) | ||
acc.Add("list_stats_open_rate", report.ListStats.OpenRate, tags, now) | ||
acc.Add("list_stats_click_rate", report.ListStats.ClickRate, tags, now) | ||
|
||
// TODO(cam) parse and add TimeSeries data here too | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
plugins.Add("mailchimp", func() plugins.Plugin { | ||
return &MailChimp{} | ||
}) | ||
} |
Oops, something went wrong.