Phase 2 part 3: buying signals, the funnel and the scorecard
Schema 7 adds signals (hypothesis, how to spot it, angle, status, utm slug), funnel_events (stage, channel, subject, campaign, source, deduped by external ID) and outside_campaigns. Prospects -> Signals is the scorecard: each signal's funnel from found to paid, people once per stage, with revenue and outside spend, by period and channel. Each signal's page shows its funnel by channel, its campaigns and its latest steps. Prospects carry a signal (filter, change, "found" recorded) and outcomes can be recorded by hand. Settings -> Lead sources holds origami.chat and explee.com API keys. Every 6 hours (or Import now) Supermarket reads their campaigns, numbers and spend, contacted people, replies and hot leads, read-only: interested people and repliers become prospects, unsubscribes are suppressed, and each campaign is assigned to a signal. The explee.com client follows its OpenAPI schema; origami.chat's responses are undocumented, so its client reads fields loosely and its Test shows the real response. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
// Package explee reads outreach campaigns and their results from
|
||||
// explee.com's public API (https://api.explee.com/public/api/docs). It only
|
||||
// reads: Supermarket never starts, changes or sends anything there.
|
||||
package explee
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const apiBase = "https://api.explee.com/public/api/v1"
|
||||
|
||||
// ErrUnauthorized means the key was refused.
|
||||
var ErrUnauthorized = errors.New("explee.com refused the API key")
|
||||
|
||||
// Client calls the API with one key.
|
||||
type Client struct {
|
||||
key, base string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func New(key string) *Client {
|
||||
return &Client{key: key, base: apiBase, http: &http.Client{Timeout: 60 * time.Second}}
|
||||
}
|
||||
|
||||
// WithBase points the client at another server (for tests).
|
||||
func (c *Client) WithBase(u string) *Client {
|
||||
c.base = strings.TrimRight(u, "/")
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) get(ctx context.Context, path string, q url.Values, out any) error {
|
||||
u := c.base + path
|
||||
if len(q) > 0 {
|
||||
u += "?" + q.Encode()
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("X-API-Key", c.key)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(io.LimitReader(res.Body, 32<<20))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden {
|
||||
return ErrUnauthorized
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("explee.com %s: HTTP %d: %s", path, res.StatusCode, strings.TrimSpace(string(body[:min(len(body), 300)])))
|
||||
}
|
||||
return json.Unmarshal(body, out)
|
||||
}
|
||||
|
||||
// Me checks the key; it returns the key owner's email or name.
|
||||
func (c *Client) Me(ctx context.Context) (string, error) {
|
||||
var out map[string]any
|
||||
if err := c.get(ctx, "/me", nil, &out); err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, k := range []string{"email", "name", "organization_name"} {
|
||||
if v, ok := out[k].(string); ok && v != "" {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return "the key works", nil
|
||||
}
|
||||
|
||||
// Campaign is an outreach campaign.
|
||||
type Campaign struct {
|
||||
ID int64 `json:"id"`
|
||||
ProjectID int64 `json:"project_id"`
|
||||
Name string `json:"name"`
|
||||
TargetURL string `json:"target_url"` // the booking link
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// Campaigns lists every campaign, archived ones too.
|
||||
func (c *Client) Campaigns(ctx context.Context) ([]Campaign, error) {
|
||||
var out struct {
|
||||
Campaigns []Campaign `json:"campaigns"`
|
||||
}
|
||||
err := c.get(ctx, "/autogtm/campaigns", url.Values{"include_archived": {"true"}}, &out)
|
||||
return out.Campaigns, err
|
||||
}
|
||||
|
||||
// Analytics are a campaign's lifetime numbers.
|
||||
type Analytics struct {
|
||||
Status string `json:"status"`
|
||||
EmailsSent int `json:"emails_sent"`
|
||||
TotalReplies int `json:"total_replies"`
|
||||
ReplyRatePct float64 `json:"reply_rate_pct"`
|
||||
HotLeads int `json:"hot_leads"`
|
||||
SpendUSD float64 `json:"spend_usd"`
|
||||
CostPerLeadUSD float64 `json:"cost_per_lead_usd"`
|
||||
LeadsPoolUsed int `json:"leads_pool_used"`
|
||||
ManualStatus map[string]int `json:"manual_status_counts"`
|
||||
ColdLost int `json:"cold_lost"`
|
||||
}
|
||||
|
||||
func (c *Client) Analytics(ctx context.Context, campaignID int64) (*Analytics, error) {
|
||||
var out Analytics
|
||||
err := c.get(ctx, "/autogtm/campaigns/"+strconv.FormatInt(campaignID, 10)+"/analytics", url.Values{"period": {"all"}}, &out)
|
||||
return &out, err
|
||||
}
|
||||
|
||||
// Lead is a contacted lead.
|
||||
type Lead struct {
|
||||
PersonID string `json:"person_id"`
|
||||
CampaignID int64 `json:"campaign_id"`
|
||||
Erased bool `json:"erased"` // they asked to be removed: no personal data
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
JobTitle string `json:"job_title"`
|
||||
LinkedInURL string `json:"linkedin_url"`
|
||||
CompanyName string `json:"company_name"`
|
||||
CompanyDomain string `json:"company_domain"`
|
||||
FirstSentAt string `json:"first_sent_at"`
|
||||
Status string `json:"status"` // set by the team in the inbox, e.g. Hot, Won
|
||||
Replied bool `json:"replied"`
|
||||
Bounced bool `json:"bounced"`
|
||||
Unsubscribed bool `json:"unsubscribed"`
|
||||
LastReplyAt string `json:"last_reply_at"`
|
||||
}
|
||||
|
||||
// Leads lists every contacted lead of a campaign.
|
||||
func (c *Client) Leads(ctx context.Context, campaignID int64) ([]Lead, error) {
|
||||
var all []Lead
|
||||
offset := 0
|
||||
for page := 0; page < 200; page++ {
|
||||
var out struct {
|
||||
Leads []Lead `json:"leads"`
|
||||
HasMore bool `json:"has_more"`
|
||||
NextOffset *int `json:"next_offset"`
|
||||
}
|
||||
q := url.Values{"limit": {"1000"}, "offset": {strconv.Itoa(offset)}}
|
||||
if err := c.get(ctx, "/autogtm/campaigns/"+strconv.FormatInt(campaignID, 10)+"/leads", q, &out); err != nil {
|
||||
return all, err
|
||||
}
|
||||
all = append(all, out.Leads...)
|
||||
if !out.HasMore || out.NextOffset == nil {
|
||||
break
|
||||
}
|
||||
offset = *out.NextOffset
|
||||
}
|
||||
return all, nil
|
||||
}
|
||||
|
||||
// HotLead is a lead whose reply showed interest.
|
||||
type HotLead struct {
|
||||
PersonID string `json:"person_id"`
|
||||
CampaignID int64 `json:"campaign_id"`
|
||||
Erased bool `json:"erased"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
JobTitle string `json:"job_title"`
|
||||
CompanyName string `json:"company_name"`
|
||||
CompanyDomain string `json:"company_domain"`
|
||||
LinkedInURL string `json:"linkedin_url"`
|
||||
Phone string `json:"phone"`
|
||||
WhyHot string `json:"why_hot"`
|
||||
BecameHotAt string `json:"became_hot_at"`
|
||||
}
|
||||
|
||||
// HotLeads lists hot leads that became hot at or after since (zero: all).
|
||||
func (c *Client) HotLeads(ctx context.Context, since time.Time) ([]HotLead, error) {
|
||||
var all []HotLead
|
||||
offset := 0
|
||||
for page := 0; page < 200; page++ {
|
||||
var out struct {
|
||||
Leads []HotLead `json:"leads"`
|
||||
HasMore bool `json:"has_more"`
|
||||
NextOffset *int `json:"next_offset"`
|
||||
}
|
||||
q := url.Values{"limit": {"500"}, "offset": {strconv.Itoa(offset)}}
|
||||
if !since.IsZero() {
|
||||
q.Set("since", since.UTC().Format(time.RFC3339))
|
||||
}
|
||||
if err := c.get(ctx, "/autogtm/hot-leads", q, &out); err != nil {
|
||||
return all, err
|
||||
}
|
||||
all = append(all, out.Leads...)
|
||||
if !out.HasMore || out.NextOffset == nil {
|
||||
break
|
||||
}
|
||||
offset = *out.NextOffset
|
||||
}
|
||||
return all, nil
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
// Package origami reads outreach campaigns and their results from
|
||||
// origami.chat's v3 API (https://docs.origami.chat). It only reads:
|
||||
// Supermarket never starts, changes or sends anything there.
|
||||
//
|
||||
// The v3 spec documents requests but not response bodies, so responses
|
||||
// are read as loose JSON and fields are looked up by the names the docs
|
||||
// use. Check them against a real account with Sample.
|
||||
package origami
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const apiBase = "https://origami.chat/api/v3"
|
||||
|
||||
// ErrUnauthorized means the key was refused.
|
||||
var ErrUnauthorized = errors.New("origami.chat refused the API key")
|
||||
|
||||
// Object is one JSON object from the API.
|
||||
type Object map[string]any
|
||||
|
||||
// Client calls the API with one key, optionally scoped to a project.
|
||||
type Client struct {
|
||||
key, project, base string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func New(key, project string) *Client {
|
||||
return &Client{key: key, project: strings.TrimSpace(project), base: apiBase, http: &http.Client{Timeout: 60 * time.Second}}
|
||||
}
|
||||
|
||||
// WithBase points the client at another server (for tests).
|
||||
func (c *Client) WithBase(u string) *Client {
|
||||
c.base = strings.TrimRight(u, "/")
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) get(ctx context.Context, path string, q url.Values) (Object, error) {
|
||||
u := c.base + path
|
||||
if len(q) > 0 {
|
||||
u += "?" + q.Encode()
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+c.key)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if c.project != "" {
|
||||
req.Header.Set("x-origami-project", c.project)
|
||||
}
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(io.LimitReader(res.Body, 32<<20))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("origami.chat %s: HTTP %d: %s", path, res.StatusCode, strings.TrimSpace(string(body[:min(len(body), 300)])))
|
||||
}
|
||||
var out Object
|
||||
if err := json.Unmarshal(body, &out); err != nil {
|
||||
return nil, fmt.Errorf("origami.chat %s: %w", path, err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// list pages through a list endpoint ({items, next_cursor}).
|
||||
func (c *Client) list(ctx context.Context, path string, q url.Values) ([]Object, error) {
|
||||
if q == nil {
|
||||
q = url.Values{}
|
||||
}
|
||||
q.Set("limit", "100")
|
||||
var all []Object
|
||||
for page := 0; page < 500; page++ {
|
||||
out, err := c.get(ctx, path, q)
|
||||
if err != nil {
|
||||
return all, err
|
||||
}
|
||||
items, _ := out["items"].([]any)
|
||||
for _, it := range items {
|
||||
if o, ok := it.(map[string]any); ok {
|
||||
all = append(all, o)
|
||||
}
|
||||
}
|
||||
next := out.Str("next_cursor")
|
||||
if next == "" {
|
||||
break
|
||||
}
|
||||
q.Set("cursor", next)
|
||||
}
|
||||
return all, nil
|
||||
}
|
||||
|
||||
// Account checks the key; it returns the organization's name.
|
||||
func (c *Client) Account(ctx context.Context) (string, error) {
|
||||
out, err := c.get(ctx, "/account", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if n := out.Str("name", "org.name", "organization.name"); n != "" {
|
||||
return n, nil
|
||||
}
|
||||
return "the key works", nil
|
||||
}
|
||||
|
||||
// Campaigns lists every campaign.
|
||||
func (c *Client) Campaigns(ctx context.Context) ([]Object, error) {
|
||||
return c.list(ctx, "/send/campaigns", nil)
|
||||
}
|
||||
|
||||
// Stats are a campaign's counts and rates.
|
||||
func (c *Client) Stats(ctx context.Context, campaignID string) (Object, error) {
|
||||
return c.get(ctx, "/send/campaigns/"+url.PathEscape(campaignID)+"/stats", nil)
|
||||
}
|
||||
|
||||
// People lists a campaign's enrolled people.
|
||||
func (c *Client) People(ctx context.Context, campaignID string) ([]Object, error) {
|
||||
return c.list(ctx, "/send/campaigns/"+url.PathEscape(campaignID)+"/people", nil)
|
||||
}
|
||||
|
||||
// Replies lists replies from leads since a time (zero: all).
|
||||
func (c *Client) Replies(ctx context.Context, since time.Time) ([]Object, error) {
|
||||
q := url.Values{"from": {"lead"}}
|
||||
if !since.IsZero() {
|
||||
q.Set("since", since.UTC().Format(time.RFC3339))
|
||||
}
|
||||
return c.list(ctx, "/send/replies", q)
|
||||
}
|
||||
|
||||
// Sample returns the first campaign, its stats, one enrolled person and one
|
||||
// reply, as the API sent them, to check the field names against.
|
||||
func (c *Client) Sample(ctx context.Context) (map[string]any, error) {
|
||||
out := map[string]any{}
|
||||
camps, err := c.Campaigns(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out["campaigns"] = len(camps)
|
||||
if len(camps) > 0 {
|
||||
out["campaign"] = camps[0]
|
||||
id := camps[0].Str("id")
|
||||
if st, err := c.Stats(ctx, id); err == nil {
|
||||
out["stats"] = st
|
||||
} else {
|
||||
out["stats_error"] = err.Error()
|
||||
}
|
||||
if p, err := c.get(ctx, "/send/campaigns/"+url.PathEscape(id)+"/people", url.Values{"limit": {"1"}}); err == nil {
|
||||
out["people"] = p
|
||||
} else {
|
||||
out["people_error"] = err.Error()
|
||||
}
|
||||
}
|
||||
if r, err := c.get(ctx, "/send/replies", url.Values{"limit": {"1"}, "from": {"lead"}}); err == nil {
|
||||
out["replies"] = r
|
||||
} else {
|
||||
out["replies_error"] = err.Error()
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Str is the first non-empty string (or number) among these fields; a
|
||||
// dotted name looks inside nested objects.
|
||||
func (o Object) Str(names ...string) string {
|
||||
for _, n := range names {
|
||||
var v any = map[string]any(o)
|
||||
for _, part := range strings.Split(n, ".") {
|
||||
m, ok := v.(map[string]any)
|
||||
if !ok {
|
||||
v = nil
|
||||
break
|
||||
}
|
||||
v = m[part]
|
||||
}
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
if s := strings.TrimSpace(x); s != "" {
|
||||
return s
|
||||
}
|
||||
case float64:
|
||||
return strings.TrimSuffix(fmt.Sprintf("%f", x), ".000000")
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Num is the first numeric field among these names.
|
||||
func (o Object) Num(names ...string) (float64, bool) {
|
||||
for _, n := range names {
|
||||
var v any = map[string]any(o)
|
||||
for _, part := range strings.Split(n, ".") {
|
||||
m, ok := v.(map[string]any)
|
||||
if !ok {
|
||||
v = nil
|
||||
break
|
||||
}
|
||||
v = m[part]
|
||||
}
|
||||
if f, ok := v.(float64); ok {
|
||||
return f, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Time is the first field among these names that parses as a time.
|
||||
func (o Object) Time(names ...string) time.Time {
|
||||
for _, n := range names {
|
||||
if t := ParseTime(o.Str(n)); !t.IsZero() {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
// ParseTime reads the ISO 8601 forms these APIs send.
|
||||
func ParseTime(s string) time.Time {
|
||||
for _, f := range []string{time.RFC3339Nano, "2006-01-02T15:04:05", "2006-01-02 15:04:05", "2006-01-02"} {
|
||||
if t, err := time.Parse(f, s); err == nil {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
Reference in New Issue
Block a user