Skip to content

Commit

Permalink
make args better
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Jan 16, 2025
1 parent 8483166 commit 5ac2ec1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
21 changes: 19 additions & 2 deletions src/__tests__/utils/event-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { detectBrowser } from '../../utils/user-agent-utils'
describe(`event-utils`, () => {
describe('properties', () => {
it('should have $host and $pathname in properties', () => {
const properties = Info.properties()
const properties = Info.properties(false, [])
expect(properties['$current_url']).toBeDefined()
expect(properties['$host']).toBeDefined()
expect(properties['$pathname']).toBeDefined()
Expand All @@ -19,7 +19,7 @@ describe(`event-utils`, () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globals['userAgent'] = 'blah'
const properties = Info.properties()
const properties = Info.properties(false, [])
expect(properties['$raw_user_agent']).toBe('blah')
})

Expand All @@ -32,6 +32,23 @@ describe(`event-utils`, () => {
expect(properties['$raw_user_agent'].length).toBe(1000)
expect(properties['$raw_user_agent'].substring(995)).toBe('aa...')
})

it('should mask out personal data from URL', () => {
// @ts-expect-error ok to set global in test
globals.location = { href: 'https://www.example.com/path?gclid=12345&other=true' }
const properties = Info.properties({ maskPersonalDataProperties: true })
expect(properties['$current_url']).toEqual('https://www.example.com/path?gclid=<masked>&other=true')
})

it('should mask out custom personal data', () => {
// @ts-expect-error ok to set global in test
globals.location = { href: 'https://www.example.com/path?gclid=12345&other=true' }
const properties = Info.properties({
maskPersonalDataProperties: true,
customPersonalDataProperties: ['other'],
})
expect(properties['$current_url']).toEqual('https://www.example.com/path?gclid=<masked>&other=<masked>')
})
})

describe('user agent', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ import { each, extend } from '../utils'
import { includes } from '../utils/string-utils'

export const setAllPersonProfilePropertiesAsPersonPropertiesForFlags = (posthog: PostHog): void => {
const allProperties = extend({}, Info.properties(), Info.campaignParams(), Info.referrerInfo())
const allProperties = extend(
{},
Info.properties({
maskPersonalDataProperties: posthog.config.mask_personal_data_properties,
customPersonalDataProperties: posthog.config.custom_personal_data_properties,
}),
Info.campaignParams({
customTrackedParams: posthog.config.custom_campaign_params,
maskPersonalDataProperties: posthog.config.mask_personal_data_properties,
customPersonalDataProperties: posthog.config.custom_personal_data_properties,
}),
Info.referrerInfo()
)
const personProperties: Record<string, string> = {}
each(allProperties, function (v, k: string) {
if (includes(CAMPAIGN_PARAMS, k) || includes(EVENT_TO_PERSON_PROPERTIES, k)) {
Expand Down
8 changes: 4 additions & 4 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,10 +963,10 @@ export class PostHog {
return properties
}

const infoProperties = Info.properties(
this.config.mask_personal_data_properties,
this.config.custom_personal_data_properties
)
const infoProperties = Info.properties({
maskPersonalDataProperties: this.config.mask_personal_data_properties,
customPersonalDataProperties: this.config.custom_personal_data_properties,
})

if (this.sessionManager) {
const { sessionId, windowId } = this.sessionManager.checkAndGetSessionAndWindowId()
Expand Down
23 changes: 15 additions & 8 deletions src/utils/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ export const EVENT_TO_PERSON_PROPERTIES = [
export const MASKED = '<masked>'

export const Info = {
campaignParams: function (
customTrackedParams?: string[],
maskPersonalDataProperties?: boolean,
campaignParams: function ({
customTrackedParams,
maskPersonalDataProperties,
customPersonalDataProperties,
}: {
customTrackedParams?: string[]
maskPersonalDataProperties?: boolean
customPersonalDataProperties?: string[] | undefined
): Record<string, string> {
}): Record<string, string> {
if (!document) {
return {}
}
Expand Down Expand Up @@ -244,10 +248,13 @@ export const Info = {
}
},

properties: function (
maskPersonalDataProperties: boolean,
customPersonalDataProperties: string[] | undefined
): Properties {
properties: function ({
maskPersonalDataProperties,
customPersonalDataProperties,
}: {
maskPersonalDataProperties?: boolean
customPersonalDataProperties?: string[]
} = {}): Properties {
if (!userAgent) {
return {}
}
Expand Down

0 comments on commit 5ac2ec1

Please sign in to comment.