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: Only set missing campaign params to null if there is at least one non-null #1493

Merged
Merged
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
36 changes: 36 additions & 0 deletions src/__tests__/posthog-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,41 @@ describe('posthog core', () => {
expect(properties['$referring_domain']).toBe('$direct')
})
})

describe('campaign params', () => {
it('should not send campaign params as null if there are no non-null ones', () => {
// arrange
const token = uuidv7()
mockURLGetter.mockReturnValue('https://www.example.com/some/path')
const { posthog, onCapture } = setup({
token,
persistence_name: token,
})

// act
posthog.capture('$pageview')

//assert
expect(onCapture.mock.calls[0][1].properties).not.toHaveProperty('utm_source')
expect(onCapture.mock.calls[0][1].properties).not.toHaveProperty('utm_medium')
})

it('should send present campaign params, and nulls for others', () => {
// arrange
const token = uuidv7()
mockURLGetter.mockReturnValue('https://www.example.com/some/path?utm_source=source')
const { posthog, onCapture } = setup({
token,
persistence_name: token,
})

// act
posthog.capture('$pageview')

//assert
expect(onCapture.mock.calls[0][1].properties.utm_source).toBe('source')
expect(onCapture.mock.calls[0][1].properties.utm_medium).toBe(null)
})
})
})
})
8 changes: 6 additions & 2 deletions src/posthog-persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
PERSISTENCE_RESERVED_PROPERTIES,
} from './constants'

import { isObject, isUndefined } from './utils/type-utils'
import { isEmptyObject, isObject, isUndefined } from './utils/type-utils'
import { Info } from './utils/event-utils'
import { logger } from './utils/logger'

Expand Down Expand Up @@ -221,7 +221,11 @@ export class PostHogPersistence {

update_campaign_params(): void {
if (!this.campaign_params_saved) {
this.register(Info.campaignParams(this.config.custom_campaign_params))
const campaignParams = Info.campaignParams(this.config.custom_campaign_params)
// only save campaign params if there were any
if (!isEmptyObject(stripEmptyProperties(campaignParams))) {
this.register(Info.campaignParams(this.config.custom_campaign_params))
}
this.campaign_params_saved = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we only be marked them as saved when we did send something or doesn't matter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This flag is there to make sure we get the campaign params from the start of the session only, so I think it's correct as is. We want to set the flag regardless of whether we save anything, so that we don't pick up campaign params from later in the session.

}
}
Expand Down
Loading