From dd8735e70d068a6ca9be524b6498d596b11a8980 Mon Sep 17 00:00:00 2001 From: Robbie Date: Mon, 28 Oct 2024 17:00:50 +0000 Subject: [PATCH] feat: Only set missing campaign params to null if there is at least one non-null (#1493) --- src/__tests__/posthog-core.test.ts | 36 ++++++++++++++++++++++++++++++ src/posthog-persistence.ts | 8 +++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/__tests__/posthog-core.test.ts b/src/__tests__/posthog-core.test.ts index d7eac3f58..af1ca4b01 100644 --- a/src/__tests__/posthog-core.test.ts +++ b/src/__tests__/posthog-core.test.ts @@ -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) + }) + }) }) }) diff --git a/src/posthog-persistence.ts b/src/posthog-persistence.ts index 4d0d1d8e4..131c85ed6 100644 --- a/src/posthog-persistence.ts +++ b/src/posthog-persistence.ts @@ -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' @@ -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 } }