From 4a8b2a5f5c65962e95701e68e4424cf4eba100e7 Mon Sep 17 00:00:00 2001 From: Robbie Coomber Date: Thu, 30 Nov 2023 16:44:33 +0000 Subject: [PATCH 1/2] Add site_name in config and $site_name property --- playground/nextjs/pages/_app.tsx | 1 + src/posthog-core.ts | 4 ++++ src/types.ts | 1 + 3 files changed, 6 insertions(+) diff --git a/playground/nextjs/pages/_app.tsx b/playground/nextjs/pages/_app.tsx index 7f0601849..222625689 100644 --- a/playground/nextjs/pages/_app.tsx +++ b/playground/nextjs/pages/_app.tsx @@ -15,6 +15,7 @@ if (typeof window !== 'undefined') { }, debug: true, __preview_send_client_session_params: true, + site_name: 'playground-nextjs', }) ;(window as any).posthog = posthog } diff --git a/src/posthog-core.ts b/src/posthog-core.ts index 0de7d319a..c6de3ecfe 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -988,6 +988,10 @@ export class PostHog { properties['$duration'] = parseFloat((duration_in_ms / 1000).toFixed(3)) } + if (this.config.site_name) { + properties['$site_name'] = this.config.site_name + } + // note: extend writes to the first object, so lets make sure we // don't write to the persistence properties object and info // properties object by passing in a new object diff --git a/src/types.ts b/src/types.ts index 0339727a1..013b2c7ff 100644 --- a/src/types.ts +++ b/src/types.ts @@ -124,6 +124,7 @@ export interface PostHogConfig { featureFlagPayloads?: Record } segment?: any + site_name?: string __preview_measure_pageview_stats?: boolean __preview_send_client_session_params?: boolean } From 08e5a9008e91a5dc15c0bf15ba2bd21573100a96 Mon Sep 17 00:00:00 2001 From: Robbie Coomber Date: Thu, 30 Nov 2023 17:01:15 +0000 Subject: [PATCH 2/2] Add test for $site_name property --- src/__tests__/posthog-core.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/__tests__/posthog-core.js b/src/__tests__/posthog-core.js index f9262e0ab..5a9b4c2fc 100644 --- a/src/__tests__/posthog-core.js +++ b/src/__tests__/posthog-core.js @@ -234,6 +234,26 @@ describe('posthog core', () => { undefined ) }) + + it('includes $site_name from config', () => { + given('config', () => ({ + site_name: 'test site name', + property_blacklist: [], + _onCapture: jest.fn(), + })) + const event = given.subject() + expect(event.properties['$site_name']).toBe('test site name') + }) + + it('doesnt include $site_name if not set', () => { + given('config', () => ({ + site_name: undefined, + property_blacklist: [], + _onCapture: jest.fn(), + })) + const event = given.subject() + expect(event.properties).not.toHaveProperty('$site_name') + }) }) describe('_afterDecideResponse', () => {