PostHog / posthog-js-lite

Reimplementation of posthog-js to be as light and modular as possible.
https://posthog.com/docs/libraries
MIT License
70 stars 36 forks source link

feat: Allow customising appProperties with a function #69

Closed benjackwhite closed 1 year ago

benjackwhite commented 1 year ago

Problem

Customer request was basically asking to omit one of the properties we bundle by default. This is currently already possible but only by extending the PostHog class and overriding the property like this:

class CustomPostHog extends PostHog {
  getCommonEventProperties(): any {
    const properties = super.getCommonEventProperties()
    // Filter out or add more properties here

    delete properties.$device_name

    return properties
  }
}

We do provide an option which was intended for providing your own "native" app properties if you don't want to use the libraries we support via native-deps but this becomes a full override, meaning you have to build all the options yourself manually.

PostHog.initAsync("token", { customAppProperties: {
  $device_name: 'My own thing',
}})

Changes

Allow customAppProperties to be a function that takes the existing properties


PostHog.initAsync('token', {
  customAppProperties: (props) => ({
    ...props,
    $device_name: 'CUSTOM!',
  }),
})

which feels a lot more client-friendly that overriding the class.

Release info Sub-libraries affected

Bump level

Libraries affected

Changelog notes

neilkakkar commented 1 year ago

Lovely, elegant, I like it!