Skip to content

Commit

Permalink
fix: properly clean pageview path
Browse files Browse the repository at this point in the history
  • Loading branch information
negrel committed Feb 28, 2024
1 parent eacbf29 commit d18f83d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/event/pageview.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package event

import (
"net/url"
"path"
"time"

"github.com/prismelabs/analytics/pkg/services/ipgeolocator"
Expand All @@ -22,10 +23,12 @@ type PageView struct {
}

// NewPageView creates a new PageView event.
func NewPageView(pvUrl *url.URL,
func NewPageView(
pvUrl *url.URL,
cli uaparser.Client,
pageReferrer string,
countryCode ipgeolocator.CountryCode) (PageView, error) {
countryCode ipgeolocator.CountryCode,
) (PageView, error) {
domain, err := ParseDomainName(pvUrl.Hostname())
if err != nil {
return PageView{}, err
Expand All @@ -36,17 +39,15 @@ func NewPageView(pvUrl *url.URL,
return PageView{}, err
}

path := pvUrl.Path
if path == "" {
path = "/"
} else if path[len(path)-1] == '/' && len(path) > 1 {
path = path[:len(path)-1]
pageviewPath := pvUrl.Path
if pageviewPath == "" {
pageviewPath = "/"
}

return PageView{
Timestamp: time.Now().UTC(),
DomainName: domain,
PathName: path,
PathName: path.Clean(pageviewPath),
Client: cli,
ReferrerDomain: referrerDomain,
CountryCode: countryCode,
Expand Down
24 changes: 24 additions & 0 deletions tests/bun/events/events_pageviews.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,30 @@ test('valid pageview with US IP address', async () => {
})
})

test('valid pageview with dirty path', async () => {
const response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
headers: {
'X-Forwarded-For': '8.8.8.8', // Google public DNS
Referer: 'http://foo.mywebsite.localhost///another/../another/foo?bar=baz#qux'
}
})
expect(response.status).toBe(200)

const data = await getLatestPageview()

expect(data).toMatchObject({
timestamp: expect.stringMatching(TIMESTAMP_REGEX),
domain: 'foo.mywebsite.localhost',
path: '/another/foo',
operating_system: 'Other',
browser_family: 'Other',
device: 'Other',
referrer_domain: 'direct',
country_code: 'US'
})
})

async function getLatestPageview (): Promise<any> {
// Wait for clickhouse to ingest batch.
Bun.sleepSync(1000)
Expand Down

0 comments on commit d18f83d

Please sign in to comment.