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:
@@ -136,7 +136,7 @@ func ImportPlausible(ctx context.Context, d *db.DB, c *plausible.Client, sites [
|
||||
campaigns = append(campaigns, slug)
|
||||
}
|
||||
yesterday := now.Local().AddDate(0, 0, -1).Format(dayFormat)
|
||||
visits := 0
|
||||
visits, leads, leadNote := 0, 0, ""
|
||||
for _, site := range sites {
|
||||
var through string
|
||||
if _, err := d.GetJSON(plausibleStateKey(site), &through); err != nil {
|
||||
@@ -192,15 +192,73 @@ func ImportPlausible(ctx context.Context, d *db.DB, c *plausible.Client, sites [
|
||||
visits += n
|
||||
}
|
||||
}
|
||||
n, err := importLeads(ctx, d, c, site, from, yesterday, campaigns, tags)
|
||||
if err != nil {
|
||||
leadNote = fmt.Sprintf(" Form leads weren't read from %s: %v.", site, err)
|
||||
}
|
||||
leads += n
|
||||
if err := d.PutJSON(plausibleStateKey(site), yesterday); err != nil {
|
||||
return save(d, SourcePlausible, res, err)
|
||||
}
|
||||
}
|
||||
res.Through = yesterday
|
||||
res.Detail = fmt.Sprintf("%d visitors on links tagged with a signal.", visits)
|
||||
res.Detail = fmt.Sprintf("%d visitors on links tagged with a signal; %d form leads.", visits, leads) + leadNote
|
||||
return save(d, SourcePlausible, res, nil)
|
||||
}
|
||||
|
||||
// LeadGoal is the Plausible goal (a custom event) the sites' lead forms
|
||||
// send when they're submitted.
|
||||
const LeadGoal = "Lead"
|
||||
|
||||
// importLeads counts the lead forms sent on visits tagged with a signal
|
||||
// (Plausible's Lead goal, by the visit's UTM tags) as interested, on the
|
||||
// signal's funnel. Without the goal set up in Plausible, it reports why.
|
||||
func importLeads(ctx context.Context, d *db.DB, c *plausible.Client, site, from, to string, campaigns []any, tags map[string]int64) (int, error) {
|
||||
rows, err := c.Run(ctx, plausible.Query{
|
||||
SiteID: site,
|
||||
Metrics: []string{"events"},
|
||||
DateRange: []string{from, to},
|
||||
Dimensions: []string{"time:day", "visit:utm_campaign", "visit:utm_source", "visit:utm_medium"},
|
||||
Filters: [][]any{{"is", "event:goal", []any{LeadGoal}}, {"is", "visit:utm_campaign", campaigns}},
|
||||
Pagination: &plausible.Pagination{Limit: 10000},
|
||||
})
|
||||
if err != nil {
|
||||
if strings.Contains(strings.ToLower(err.Error()), "goal") {
|
||||
return 0, fmt.Errorf("add a %q goal (a custom event) in Plausible to count them", LeadGoal)
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
n := 0
|
||||
for _, r := range rows {
|
||||
if len(r.Dimensions) < 4 || len(r.Metrics) < 1 || r.Metrics[0] < 1 {
|
||||
continue
|
||||
}
|
||||
day, campaign, source, medium := r.Dimensions[0], r.Dimensions[1], r.Dimensions[2], r.Dimensions[3]
|
||||
sig, ok := tags[strings.ToLower(campaign)]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
at, err := time.ParseInLocation(dayFormat, day[:min(len(day), 10)], time.Local)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
count := int(r.Metrics[0])
|
||||
note := strings.Trim(none(source)+" / "+none(medium), " /")
|
||||
added, err := signals.Record(d, signals.Event{
|
||||
SignalID: sig, Stage: signals.StageInterested, Channel: Channel(none(source), none(medium)),
|
||||
Source: SourcePlausible, Count: count, Note: site + ": sent a lead form" + strings.TrimSuffix(" ("+note+")", " ()"),
|
||||
At: at.Add(12 * time.Hour), ExternalID: strings.Join([]string{"lead", site, day, campaign, source, medium}, "|"),
|
||||
})
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
if added {
|
||||
n += count
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// none is "" for Plausible's empty values.
|
||||
func none(v string) string {
|
||||
if v == "(none)" || v == "(not set)" {
|
||||
@@ -269,3 +327,32 @@ func PageVisitors(ctx context.Context, c *plausible.Client, site string, paths [
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// FormLead is a day's lead forms for a signal, as counted from Plausible.
|
||||
type FormLead struct {
|
||||
Signal string
|
||||
Count int
|
||||
Note string
|
||||
At time.Time
|
||||
}
|
||||
|
||||
// FormLeads are the latest lead forms counted, newest first.
|
||||
func FormLeads(d *db.DB, limit int) ([]FormLead, error) {
|
||||
rows, err := d.Query(`SELECT coalesce(s.name, ''), e.count, e.note, e.at FROM funnel_events e LEFT JOIN signals s ON s.id = e.signal_id
|
||||
WHERE e.source = ? AND e.external_id LIKE 'lead|%' ORDER BY e.at DESC, e.id DESC LIMIT ?`, SourcePlausible, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []FormLead
|
||||
for rows.Next() {
|
||||
var f FormLead
|
||||
var at string
|
||||
if err := rows.Scan(&f.Signal, &f.Count, &f.Note, &at); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.At, _ = time.Parse(time.RFC3339, at)
|
||||
out = append(out, f)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user