PostHog / posthog-js

Send usage data from your web app or site to PostHog, with autocapture.
https://posthog.com/docs/libraries/js
Other
302 stars 126 forks source link

feat: allow config of before_send function to edit or reject events #1515

Closed pauldambra closed 6 days ago

pauldambra commented 2 weeks ago

see RFC: https://github.com/PostHog/product-internal/pull/668/ see docs PR: https://github.com/PostHog/posthog.com/pull/9844

The number of config options to redact urls or edit properties is growing It is simpler to have a single hook where you can edit an event before it is captured

This also allows the enterprising customer configure a sampling function


tested locally and event capture still works without a beforeSend set and e.g. the pre-canned event sampling function works as expected

vercel[bot] commented 2 weeks ago

The latest updates on your projects. Learn more about Vercel for Git ↗ī¸Ž

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Nov 19, 2024 11:36am
github-actions[bot] commented 2 weeks ago

Size Change: +10.4 kB (+0.33%)

Total Size: 3.12 MB

Filename Size Change
dist/all-external-dependencies.js 204 kB -6 B (0%)
dist/array.full.es5.js 254 kB +1.06 kB (+0.42%)
dist/array.full.js 357 kB +940 B (+0.26%)
dist/array.full.no-external.js 356 kB +940 B (+0.27%)
dist/array.js 171 kB +943 B (+0.55%)
dist/array.no-external.js 170 kB +941 B (+0.56%)
dist/dead-clicks-autocapture.js 13.6 kB +78 B (+0.58%)
dist/external-scripts-loader.js 2.27 kB +74 B (+3.37%)
dist/main.js 172 kB +1.01 kB (+0.59%)
dist/module.full.js 357 kB +1 kB (+0.28%)
dist/module.full.no-external.js 356 kB +1 kB (+0.28%)
dist/module.js 171 kB +1 kB (+0.59%)
dist/module.no-external.js 170 kB +1 kB (+0.59%)
dist/recorder-v2.js 113 kB +71 B (+0.06%)
dist/recorder.js 113 kB +71 B (+0.06%)
dist/surveys-preview.js 56.7 kB +71 B (+0.13%)
dist/surveys.js 62.1 kB +81 B (+0.13%)
dist/tracing-headers.js 1.4 kB +74 B (+5.57%) 🔍
ℹī¸ View Unchanged | Filename | Size | | :--- | :---: | | `dist/exception-autocapture.js` | 8.7 kB | | `dist/web-vitals.js` | 10.3 kB |

compressed-size-action

pauldambra commented 1 week ago

have updated and tested the examples in the posthog.com docs PR

and tested this locally

i think this is good to go

pauldambra commented 1 week ago

@robbie-c how about allowing an array of functions...

then we can do

before_send: [
    sampleByDistinctId(0.5), // only half of people
    sampleByEvent(['$web_vitals], 0.1), // and they capture all events except 10% of web vitals
]
pauldambra commented 1 week ago

updated docs PR here https://github.com/PostHog/posthog.com/pull/9844/files#diff-e9e9ef37e4e756e5cf3bc804e89db87a8e60864b4a39d43c61c311cd0ae13eb6R583-R596

robbie-c commented 1 week ago

Makes sense, would you multiply the sample thresholds together?