Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Treat self referrals as direct #1426

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/__tests__/posthog-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { uuidv7 } from '../uuidv7'

const mockReferrerGetter = jest.fn()
const mockURLGetter = jest.fn()
const mockHostGetter = jest.fn()
jest.mock('../utils/globals', () => {
const orig = jest.requireActual('../utils/globals')
return {
Expand All @@ -22,6 +23,7 @@ jest.mock('../utils/globals', () => {
const url = mockURLGetter?.()
return {
href: url,
host: mockHostGetter?.(),
toString: () => url,
}
},
Expand Down Expand Up @@ -207,6 +209,27 @@ describe('posthog core', () => {
expect(properties['$referrer']).toBe('$direct')
expect(properties['$referring_domain']).toBe('$direct')
})

it('should use $direct when the referrer is the same as the current host', () => {
// arrange
const token = uuidv7()
mockReferrerGetter.mockReturnValue('https://tracked.example.com')
mockHostGetter.mockReturnValue('tracked.example.com')
const { posthog, onCapture } = setup({
token,
persistence_name: token,
})

// act
posthog.capture(eventName, eventProperties)

// assert
const { $set_once, properties } = onCapture.mock.calls[0][1]
expect($set_once['$initial_referrer']).toBe('$direct')
expect($set_once['$initial_referring_domain']).toBe('$direct')
expect(properties['$referrer']).toBe('$direct')
expect(properties['$referring_domain']).toBe('$direct')
})
})
})
})
29 changes: 17 additions & 12 deletions src/utils/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,27 @@ export const Info = {

deviceType: detectDeviceType,

referrer: function (): string {
return document?.referrer || '$direct'
referrerInfo: function (): Record<string, any> {
const referrer = document?.referrer
const referring_domain = referrer ? convertToURL(referrer)?.host : undefined
if (!referrer || !referring_domain || referring_domain === location?.host) {
return {
$referrer: '$direct',
$referring_domain: '$direct',
}
}
return {
$referrer: referrer,
$referring_domain: referring_domain,
}
},

referringDomain: function (): string {
if (!document?.referrer) {
return '$direct'
}
return convertToURL(document.referrer)?.host || '$direct'
referrer: function (): string {
return this.referrerInfo().$referrer
},

referrerInfo: function (): Record<string, any> {
return {
$referrer: this.referrer(),
$referring_domain: this.referringDomain(),
}
referringDomain: function (): string {
return this.referrerInfo().$referring_domain
},

initialPersonInfo: function (): Record<string, any> {
Expand Down
Loading