Phase 6 part 3: website lead forms into EspoCRM

Sites → Lead forms sets up the site's forms to create EspoCRM Leads
through its Lead Capture: the address saved and tested with a CORS
preflight, and a pull request adding a script that maps the form's fields
onto a Lead (campaign, page and form in the description). Plausible's
Lead goal is imported per signal as interested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 14:54:26 -07:00
co-authored by Claude Opus 5.5
parent fa4792e0fe
commit 2c42857419
12 changed files with 1003 additions and 9 deletions
+40 -6
View File
@@ -70,6 +70,13 @@ func TestImportPlausible(t *testing.T) {
var q plausible.Query
json.NewDecoder(r.Body).Decode(&q)
got = append(got, q)
if len(q.Filters) == 2 { // the lead forms (the Lead goal)
json.NewEncoder(w).Encode(map[string]any{"results": []any{
map[string]any{"dimensions": []string{"2026-11-21", "leaving-m365", "linkedin", "social"}, "metrics": []float64{1}},
map[string]any{"dimensions": []string{"2026-11-21", "old-campaign", "x", "social"}, "metrics": []float64{3}},
}})
return
}
json.NewEncoder(w).Encode(map[string]any{"results": []any{
map[string]any{"dimensions": []string{"2026-11-20", "leaving-m365", "email", "outreach", "outreach-3"}, "metrics": []float64{2}},
map[string]any{"dimensions": []string{"2026-11-21", "leaving-m365", "linkedin", "social", "(none)"}, "metrics": []float64{5}},
@@ -82,7 +89,7 @@ func TestImportPlausible(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if res.Added != 2 || res.Through != "2026-11-21" || !strings.Contains(res.Detail, "7 visitors") {
if res.Added != 2 || res.Through != "2026-11-21" || !strings.Contains(res.Detail, "7 visitors") || !strings.Contains(res.Detail, "1 form leads") {
t.Fatalf("result: %+v", res)
}
q := got[0]
@@ -90,17 +97,24 @@ func TestImportPlausible(t *testing.T) {
if q.SiteID != "federated.computer" || string(dr) != `["2026-10-23","2026-11-21"]` || len(q.Filters) != 1 {
t.Errorf("query: %+v %s", q, dr)
}
if lq, _ := json.Marshal(got[1].Filters); !strings.Contains(string(lq), `["is","event:goal",["Lead"]]`) {
t.Errorf("leads query: %s", lq)
}
evs, _ := signals.RecentEvents(d, m365, 10)
if len(evs) != 2 || evs[0].Stage != signals.StageVisited || evs[0].Channel != "social" || evs[0].Count != 5 ||
evs[1].Channel != "email" || !strings.Contains(evs[1].Note, "outreach-3") {
t.Errorf("events: %+v", evs)
var stages []string
for _, e := range evs {
stages = append(stages, e.Stage+"/"+e.Channel)
}
if len(evs) != 3 || strings.Join(stages, " ") != "interested/social visited/social visited/email" ||
!strings.Contains(evs[0].Note, "sent a lead form (linkedin / social)") || !strings.Contains(evs[2].Note, "outreach-3") {
t.Errorf("events: %v %+v", stages, evs)
}
// The next day reads only the new day; running again the same day reads nothing.
if _, err := ImportPlausible(context.Background(), d, c, []string{"federated.computer"}, now); err != nil || len(got) != 1 {
if _, err := ImportPlausible(context.Background(), d, c, []string{"federated.computer"}, now); err != nil || len(got) != 2 {
t.Errorf("same day: %d queries, %v", len(got), err)
}
res, _ = ImportPlausible(context.Background(), d, c, []string{"federated.computer"}, now.AddDate(0, 0, 1))
dr, _ = json.Marshal(got[1].DateRange)
dr, _ = json.Marshal(got[2].DateRange)
if string(dr) != `["2026-11-22","2026-11-22"]` || res.Added != 0 {
t.Errorf("next day: %s %+v", dr, res)
}
@@ -198,3 +212,23 @@ func TestSyncStripe(t *testing.T) {
t.Error("a bad key worked")
}
}
func TestImportLeadsWithoutTheGoal(t *testing.T) {
d := openDB(t)
signal(t, d, "Leaving M365", "leaving-m365")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var q plausible.Query
json.NewDecoder(r.Body).Decode(&q)
if len(q.Filters) == 2 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":"The goal ` + "`Lead`" + ` is not configured for this site."}`))
return
}
w.Write([]byte(`{"results":[]}`))
}))
defer srv.Close()
res, err := ImportPlausible(context.Background(), d, plausible.New(srv.URL, "k"), []string{"federated.computer"}, now)
if err != nil || !strings.Contains(res.Detail, `add a "Lead" goal`) || res.Through != "2026-11-21" {
t.Errorf("%+v %v", res, err)
}
}