Skip to content

Commit

Permalink
feat: Only set missing campaign params to null if there is at least o…
Browse files Browse the repository at this point in the history
…ne non-null (#1493)
  • Loading branch information
robbie-c authored Oct 28, 2024
1 parent 9c70330 commit dd8735e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
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
}
}
Expand Down

0 comments on commit dd8735e

Please sign in to comment.