Skip to content

Commit

Permalink
Optimized funnels.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Jan 2, 2025
1 parent 7b73bb3 commit 17e846c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 6.19.3

* added `Equal` to filter
* optimized funnels
* updated referrer blacklist
* updated User-Agent blacklist
* fixed tests
Expand Down
24 changes: 16 additions & 8 deletions pkg/analyzer/funnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,36 @@ func (funnel *Funnel) Steps(ctx context.Context, filter []Filter) ([]model.Funne
return nil, errors.New("not enough steps")
}

for i := range filter {
filter[i] = *funnel.analyzer.getFilter(&filter[i])
filter[i].funnelStep = i + 1
}

var query strings.Builder
args := make([]any, 0)

for i := range filter {
f := funnel.analyzer.getFilter(&filter[i])
f.funnelStep = i + 1
if i == 0 {
query.WriteString(fmt.Sprintf("WITH step%d AS ( ", i+1))
} else {
query.WriteString(fmt.Sprintf("step%d AS ( ", i+1))
}

fields := []Field{
FieldClientID,
FieldVisitorID,
FieldSessionID,
FieldTime,
}
q, a := f.buildQuery(fields, nil, nil, nil, "")
args = append(args, a...)

if i == 0 {
query.WriteString(fmt.Sprintf("WITH step%d AS ( ", i+1))
if i > 0 && filter[i].Equal(&filter[i-1]) {
query.WriteString(fmt.Sprintf("SELECT * FROM step%d", i))
} else {
query.WriteString(fmt.Sprintf("step%d AS ( ", i+1))
q, a := filter[i].buildQuery(fields, nil, nil, nil, "")
args = append(args, a...)
query.WriteString(q)
}

query.WriteString(q)
query.WriteString(") ")

if i != len(filter)-1 {
Expand Down
45 changes: 45 additions & 0 deletions pkg/analyzer/funnel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,48 @@ func TestFunnel_Steps(t *testing.T) {
assert.InDelta(t, 0, funnel[0].DropOff, 0.01)
assert.InDelta(t, 0, funnel[1].DropOff, 0.01)
}

func TestFunnel_EqualSteps(t *testing.T) {
db.CleanupDB(t, dbClient)
s := make([]model.Session, 0)
pv := make([]model.PageView, 0)
now := time.Now()

for i := 0; i < 100; i++ {
s = append(s, model.Session{
Sign: 1,
VisitorID: uint64(i) + 1,
Time: util.Today(),
Start: now,
EntryPath: "/",
ExitPath: "/thank-you",
IsBounce: false,
PageViews: 5,
Language: "en",
})
pv = append(pv, model.PageView{
VisitorID: 1,
Time: util.Today(),
Path: "/",
})
}

saveSessions(t, [][]model.Session{s})
assert.NoError(t, dbClient.SavePageViews(pv))
time.Sleep(time.Millisecond * 100)
analyzer := NewAnalyzer(dbClient)
steps := make([]Filter, 0)

for i := 0; i < 10; i++ {
steps = append(steps, Filter{
ClientID: 1,
From: util.Today(),
To: util.Today(),
Path: []string{"/"},
})
}

funnel, err := analyzer.Funnel.Steps(context.Background(), steps)
assert.NoError(t, err)
assert.Len(t, funnel, 10)
}

0 comments on commit 17e846c

Please sign in to comment.