Phase 7 part 1: sign-ups by page
Settings → Website & sales says which clicked links count as a sign-up: Supermarket suggests the sign-up, trial and checkout links visitors clicked, and the user ticks, unticks or adds their own. The Plausible import reads, per page a visit began on, the visitors, sign-ups and form leads (a new page_days table); What ranks, the audit and Sites show visits and sign-ups next to each page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,9 @@ package web
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -78,6 +80,39 @@ func (s *Server) renderTracking(w http.ResponseWriter, r *http.Request, errMsg s
|
||||
lastS, _ := tracking.Last(s.DB, tracking.SourceStripe)
|
||||
customers, _ := tracking.Customers(s.DB, 20)
|
||||
set, _ := s.outreachSettings()
|
||||
// The links visitors clicked, to say which are sign-ups.
|
||||
var links []tracking.OutboundLink
|
||||
if c := s.plausible(); c != nil {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
|
||||
for _, site := range s.sites() {
|
||||
l, err := tracking.OutboundLinks(ctx, c, site)
|
||||
if err == nil {
|
||||
links = append(links, l...)
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
var urls []string
|
||||
for _, l := range links {
|
||||
urls = append(urls, l.URL)
|
||||
}
|
||||
signups := tracking.LoadSignups(s.DB, urls)
|
||||
var own []string // patterns that aren't one of the links, to edit as text
|
||||
for i := range links {
|
||||
links[i].Signup = signups.Matches(links[i].URL)
|
||||
}
|
||||
for _, p := range signups.Patterns {
|
||||
listed := false
|
||||
for _, l := range links {
|
||||
if strings.EqualFold(patternOf(l.URL), p) {
|
||||
listed = true
|
||||
}
|
||||
}
|
||||
if !listed {
|
||||
own = append(own, p)
|
||||
}
|
||||
}
|
||||
lastPages, _ := tracking.Last(s.DB, "pages")
|
||||
s.render(w, r, "tracking", page{Title: "Website & sales", Nav: "settings/tracking", Error: errMsg, Data: map[string]any{
|
||||
"PlausibleURL": s.plausibleURL(),
|
||||
"PlausibleKey": plausibleKey,
|
||||
@@ -89,6 +124,10 @@ func (s *Server) renderTracking(w http.ResponseWriter, r *http.Request, errMsg s
|
||||
"LastStripe": lastS,
|
||||
"Customers": customers,
|
||||
"BookingFrom": set.BookingFrom,
|
||||
"Links": links,
|
||||
"Signups": signups,
|
||||
"OwnPatterns": strings.Join(own, "\n"),
|
||||
"LastPages": lastPages,
|
||||
}})
|
||||
}
|
||||
|
||||
@@ -188,6 +227,9 @@ func (s *Server) runTracking(ctx context.Context, source string) error {
|
||||
return errors.New("save the Plausible key first")
|
||||
}
|
||||
_, err := tracking.ImportPlausible(ctx, s.DB, c, s.sites(), now)
|
||||
if _, perr := tracking.ImportPages(ctx, s.DB, c, s.sites(), now); perr != nil && err == nil {
|
||||
err = fmt.Errorf("pages: %w", perr)
|
||||
}
|
||||
return err
|
||||
case tracking.SourceStripe:
|
||||
c := s.stripe()
|
||||
@@ -235,3 +277,67 @@ func (s *Server) RunTracking(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// patternOf is the pattern a ticked link saves as: its host and path.
|
||||
func patternOf(link string) string {
|
||||
if p := tracking.SuggestSignups([]string{link}); len(p) > 0 {
|
||||
return p[0]
|
||||
}
|
||||
if u, err := url.Parse(link); err == nil && u.Host != "" {
|
||||
return strings.ToLower(u.Host + strings.TrimRight(u.Path, "/"))
|
||||
}
|
||||
return strings.ToLower(link)
|
||||
}
|
||||
|
||||
// trackingSignups saves what counts as a sign-up, and reads the pages
|
||||
// again with it.
|
||||
func (s *Server) trackingSignups(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var pats []string
|
||||
for _, l := range r.PostForm["link"] {
|
||||
pats = append(pats, patternOf(l))
|
||||
}
|
||||
pats = append(pats, strings.Split(r.PostFormValue("patterns"), "\n")...)
|
||||
if err := tracking.SaveSignups(s.DB, pats); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
if c := s.plausible(); c != nil {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Minute)
|
||||
_, err := tracking.ImportPages(ctx, s.DB, c, s.sites(), time.Now())
|
||||
cancel()
|
||||
if err != nil {
|
||||
s.renderTracking(w, r, "Saved, but reading the pages again failed: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
redirect(w, r, "/settings/tracking#signups", "signups-saved")
|
||||
}
|
||||
|
||||
// convDays is the period the page tables show visits and sign-ups for, as
|
||||
// What ranks shows Search Console's.
|
||||
const convDays = 90
|
||||
|
||||
// pageConv is a website's visits, sign-ups and leads by page over the last
|
||||
// convDays, or nil before Plausible's pages were read.
|
||||
func (s *Server) pageConv(siteURL string) map[string]tracking.PageNumbers {
|
||||
p, _ := profile.Load(s.DB)
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
want := strings.TrimRight(strings.ToLower(strings.TrimSpace(siteURL)), "/")
|
||||
for _, w := range p.Websites {
|
||||
if strings.TrimRight(strings.ToLower(strings.TrimSpace(w.URL)), "/") != want {
|
||||
continue
|
||||
}
|
||||
pages, ok, err := tracking.Pages(s.DB, tracking.SiteID(w), convDays)
|
||||
if err != nil || !ok {
|
||||
return nil
|
||||
}
|
||||
return pages
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user