Phase 2 part 7: the end of the funnel

Posts can be written for a buying signal: the prompt gets its hypothesis
and angle, and links carry its campaign tag. Plausible (Stats API v2) is
read once a day for visitors on tagged links, recorded per signal as
Visited. Stripe (restricted key) is read every hour: $0 checkouts are
Signed up, paid invoices and one-time payments are Paid with their value,
tied to a signal by the checkout's utm_campaign metadata or the payer's
email. The Core's Cal.com has no API, so booking emails from Cal.com in
the outreach mailbox count as Booked. New Settings → Website & sales;
the signal page lists its posts and tagged links. Schema 11.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 06:42:03 -07:00
co-authored by Claude Opus 5.5
parent 90d73cf907
commit d23d5e2a87
32 changed files with 1775 additions and 26 deletions
+31
View File
@@ -14,6 +14,7 @@ import (
"supermarket/internal/inventory"
"supermarket/internal/models"
"supermarket/internal/profile"
"supermarket/internal/signals"
"supermarket/internal/utm"
)
@@ -335,3 +336,33 @@ func TestScheduleAndDue(t *testing.T) {
t.Errorf("unscheduled: %+v", got)
}
}
func TestDraftsForSignal(t *testing.T) {
d := openDB(t)
sig := &signals.Signal{Name: "Leaving M365", Slug: "leaving-m365", Angle: "A flat price"}
if err := signals.Save(d, sig); err != nil {
t.Fatal(err)
}
if _, err := CreateDrafts(d, []Draft{{InventoryName: "Rival", Kind: "competitor", Channel: ChannelX, Content: "Hi", SignalID: sig.ID},
{InventoryName: "Rival", Kind: "competitor", Channel: ChannelLinkedIn, Content: "Plain"}}); err != nil {
t.Fatal(err)
}
list, err := ForSignal(d, sig.ID, 10)
if err != nil || len(list) != 1 || list[0].SignalSlug != "leaving-m365" {
t.Fatalf("for signal: %+v %v", list, err)
}
if p := list[0].UTM(); p.Campaign != "leaving-m365" || p.Medium != "social" {
t.Errorf("utm: %+v", p)
}
all, _ := ListDrafts(d, 10, StatusDraft)
for _, dr := range all {
if dr.SignalID == 0 && dr.UTM().Campaign != "competitor" {
t.Errorf("plain draft's campaign: %+v", dr.UTM())
}
}
prompt := userPrompt(Request{Profile: &profile.Profile{Name: "Us"}, Entry: &inventory.Entry{Name: "Rival", Relationship: "competitor"},
Channels: []string{ChannelX}, Signal: sig}, "")
if !strings.Contains(prompt, "Leaving M365") || !strings.Contains(prompt, "A flat price") {
t.Errorf("prompt lacks the signal: %s", prompt)
}
}
+24 -5
View File
@@ -78,6 +78,8 @@ type Draft struct {
UpdatedAt time.Time
PublishedAt time.Time
PublishAt time.Time // when a scheduled draft goes out
SignalID int64 // the buying signal it was written for; 0: none
SignalSlug string // that signal's campaign tag
}
// Length is the draft's length as its channel counts it, including the
@@ -167,9 +169,13 @@ func CreateDrafts(d *db.DB, drafts []Draft) (string, error) {
return "", fmt.Errorf("unknown channel %q", dr.Channel)
}
ids, _ := json.Marshal(nonNil(dr.ItemIDs))
if _, err := tx.Exec(`INSERT INTO drafts (batch, inventory_name, kind, channel, title, target, content, source_url, item_ids)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, batch, dr.InventoryName, dr.Kind, dr.Channel,
strings.TrimSpace(dr.Title), dr.Target, strings.TrimSpace(dr.Content), dr.SourceURL, string(ids)); err != nil {
var sig any
if dr.SignalID != 0 {
sig = dr.SignalID
}
if _, err := tx.Exec(`INSERT INTO drafts (batch, inventory_name, kind, channel, title, target, content, source_url, item_ids, signal_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, batch, dr.InventoryName, dr.Kind, dr.Channel,
strings.TrimSpace(dr.Title), dr.Target, strings.TrimSpace(dr.Content), dr.SourceURL, string(ids), sig); err != nil {
return "", err
}
for _, id := range dr.ItemIDs {
@@ -193,7 +199,8 @@ func nonNil(s []string) []string {
}
const draftColumns = `id, batch, inventory_name, kind, channel, content, source_url, item_ids, status, error,
post_url, created_at, updated_at, COALESCE(published_at, ''), COALESCE(publish_at, ''), title, target`
post_url, created_at, updated_at, COALESCE(published_at, ''), COALESCE(publish_at, ''), title, target,
COALESCE(signal_id, 0), COALESCE((SELECT slug FROM signals WHERE signals.id = drafts.signal_id), '')`
func scanDrafts(rows *sql.Rows) ([]Draft, error) {
defer rows.Close()
@@ -202,7 +209,8 @@ func scanDrafts(rows *sql.Rows) ([]Draft, error) {
var dr Draft
var ids, created, updated, published, publishAt string
if err := rows.Scan(&dr.ID, &dr.Batch, &dr.InventoryName, &dr.Kind, &dr.Channel, &dr.Content,
&dr.SourceURL, &ids, &dr.Status, &dr.Error, &dr.PostURL, &created, &updated, &published, &publishAt, &dr.Title, &dr.Target); err != nil {
&dr.SourceURL, &ids, &dr.Status, &dr.Error, &dr.PostURL, &created, &updated, &published, &publishAt, &dr.Title, &dr.Target,
&dr.SignalID, &dr.SignalSlug); err != nil {
return nil, err
}
_ = json.Unmarshal([]byte(ids), &dr.ItemIDs)
@@ -301,6 +309,17 @@ func RecoverInterrupted(d *db.DB) (int, error) {
return int(n), nil
}
// ForSignal lists the posts written for a signal, newest first: the
// published ones, then the rest that aren't discarded.
func ForSignal(d *db.DB, signalID int64, limit int) ([]Draft, error) {
rows, err := d.Query(`SELECT `+draftColumns+` FROM drafts WHERE signal_id = ? AND status != ?
ORDER BY published_at IS NULL, published_at DESC, id DESC LIMIT ?`, signalID, StatusDiscarded, limit)
if err != nil {
return nil, err
}
return scanDrafts(rows)
}
// CountDrafts is how many drafts wait for review.
func CountDrafts(d *db.DB) (int, error) {
var n int
+23 -1
View File
@@ -12,6 +12,7 @@ import (
"supermarket/internal/api/twitter"
"supermarket/internal/inventory"
"supermarket/internal/profile"
"supermarket/internal/signals"
)
// Request is what a set of drafts is written from.
@@ -24,6 +25,15 @@ type Request struct {
// if its rules couldn't be read).
Subreddit string
SubredditInfo *reddit.Subreddit
// Signal, if set, is the buying signal the posts are written for.
Signal *signals.Signal
}
func (r *Request) signalID() int64 {
if r.Signal == nil {
return 0
}
return r.Signal.ID
}
// SourceURL is the link the drafts carry: the first item's.
@@ -122,7 +132,7 @@ func Generate(ctx context.Context, client ai.Client, req Request) ([]Draft, erro
if r := []rune(title); len(r) > RedditTitleLimit {
title = string(r[:RedditTitleLimit-1]) + "…"
}
drafts = append(drafts, Draft{InventoryName: name, Kind: req.Entry.Relationship, Channel: ch,
drafts = append(drafts, Draft{InventoryName: name, Kind: req.Entry.Relationship, Channel: ch, SignalID: req.signalID(),
Title: title, Target: reddit.CleanName(req.Subreddit), Content: strings.TrimSpace(rp.Body),
SourceURL: src, ItemIDs: ids, Status: StatusDraft})
continue
@@ -144,6 +154,7 @@ func Generate(ctx context.Context, client ai.Client, req Request) ([]Draft, erro
InventoryName: name,
Kind: req.Entry.Relationship,
Channel: ch,
SignalID: req.signalID(),
Content: text,
SourceURL: src,
ItemIDs: ids,
@@ -264,6 +275,17 @@ func userPrompt(req Request, src string) string {
fmt.Fprintf(&b, "%s\n</item>\n", strings.TrimSpace(it.Body))
}
if sig := req.Signal; sig != nil {
fmt.Fprintf(&b, "\nWrite it for readers in this situation (a buying signal we're testing): %s.\n", sig.Name)
if sig.Hypothesis != "" {
fmt.Fprintf(&b, "Why they may buy now: %s\n", oneLine(sig.Hypothesis))
}
if sig.Angle != "" {
fmt.Fprintf(&b, "The angle (their pain, and what we promise): %s\n", oneLine(sig.Angle))
}
b.WriteString("Connect the source material to that situation where it honestly fits; don't force it.\n")
}
b.WriteString("\nWrite one post for each channel below. Reply with JSON whose keys are the channel IDs.\n")
for _, ch := range req.Channels {
limit := limitFor(ch, src)
+6 -1
View File
@@ -86,7 +86,12 @@ func (d *Draft) UTM() utm.Params {
if slug := utm.Slug(d.InventoryName); slug != "" {
content = slug + "-" + fmt.Sprint(d.ID)
}
return utm.Params{Source: d.Channel, Medium: "social", Campaign: d.Kind, Content: content}
campaign := d.Kind
if d.SignalSlug != "" {
// Written for a buying signal: its visits count for the signal.
campaign = d.SignalSlug
}
return utm.Params{Source: d.Channel, Medium: "social", Campaign: campaign, Content: content}
}
// MarkPosted records a draft the user posted by hand (on a network