Skip to content

Commit

Permalink
Merge branch 'master' into zach/rbac/22
Browse files Browse the repository at this point in the history
  • Loading branch information
zlwaterfield authored Feb 13, 2025
2 parents 80410a9 + 00d5013 commit 2b38c73
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,30 @@ test('processEvent filters same events at different increasing percent', () => {
expect(event1).toEqual(event0)
}
})

test('processEvent filters events based on triggering events', () => {
// create an event. Hash generates 0.42
const event0 = createEvent({ event: 'blah', distinct_id: '1' })

for (let i = 0; i < 5; i++) {
meta.config.percentage = (i * 10).toString()
meta.config.triggeringEvents = 'blah,$pageview'

// Setup Plugin
setupPlugin(meta)

const event1 = processEvent(event0, meta)
expect(event1).toBeNull()
}

for (let i = 0; i < 5; i++) {
meta.config.percentage = (i * 10).toString()
meta.config.triggeringEvents = '$pageview'

// Setup Plugin
setupPlugin(meta)

const event1 = processEvent(event0, meta)
expect(event1).toEqual(event0)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export function setupPlugin({ config, global }: LegacyTransformationPluginMeta)
}
global.percentage = percentage
global.randomSampling = config.samplingMethod === 'Random sampling'
global.triggeringEvents =
(config.triggeringEvents ?? '').trim() === ''
? []
: config.triggeringEvents.split(',').map((event: string) => event.trim())
}

// /* Runs on every event */
Expand All @@ -21,13 +25,15 @@ export function processEvent(event: PluginEvent, { global }: LegacyTransformatio
// even if the percentage increases

let shouldIngestEvent = true
if (global.randomSampling) {
shouldIngestEvent = Math.round(Math.random() * 100) <= global.percentage
} else {
const hash = createHash('sha256').update(event.distinct_id).digest('hex')
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
const decisionValue = parseInt(hash.substring(0, 15), 16) / 0xfffffffffffffff
shouldIngestEvent = decisionValue <= global.percentage / 100
if (global.triggeringEvents.length === 0 || global.triggeringEvents.includes(event.event)) {
if (global.randomSampling) {
shouldIngestEvent = Math.round(Math.random() * 100) <= global.percentage
} else {
const hash = createHash('sha256').update(event.distinct_id).digest('hex')
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
const decisionValue = parseInt(hash.substring(0, 15), 16) / 0xfffffffffffffff
shouldIngestEvent = decisionValue <= global.percentage / 100
}
}

if (shouldIngestEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export const downsamplingPlugin: LegacyTransformationPlugin = {
default: 'Distinct ID aware sampling',
required: false,
},
{
type: 'string',
templating: false,
key: 'triggeringEvents',
description:
"A comma-separated list of PostHog events you want to downsample (e.g.: '$identify,mycustomevent'). If empty, all events will be downsampled.",
label: 'Triggering events',
default: '',
required: false,
},
],
},
}

0 comments on commit 2b38c73

Please sign in to comment.