In several extensions (#relatedarticles, #page-previews) we have moved to sampling logging by buckets determined from a stable session id, in order to consistently log all events in a user session. See {T167236} for context, for example.
Right now we are manually:
* Checking if `window.navigator.sendBeacon` is available
* Calling `mw.experiments` with `mw.user.sessionId` and checking bucket to set the the logging on or off
* Setting the logger to `mw.track` or `noop` or the schema sampling to `0` or `1` depending on the previous check.
Which is a bit of boilerplate. Nothing too serious but if this type of logging is going to be something common then it would be interesting to abstract it.
== Some must haves
* Configurable sampling rate
* Initial events will be delivered (to log events like page/feature loaded)
* No php conditional registration of ResourceLoader modules and async loading of such modules in the client
== AC
[] List proposals of APIs w/ pros and cons
[] Discuss
[] Create follow up tasks to implement the API, and update existing code to use it
== Proposals
=== Use [Extension:WikimediaEvents](https://www.mediawiki.org/wiki/Extension:WikimediaEvents) buses
* Unconditionally log with `mw.track`
* Implement two more buses in WikimediaEvents that take a configuration param:
* `sampled_event.session.<Schema> (config: Config, eventData: any)`
* buckets/samples based on `mw.user.sessionId`
* do we need experiment name in the config or would the schema name be enough?
* `sampled_event.page.<Schema> (config: Config, eventData: any)`
* samples based on `mw.eventLog.inSample` like `mw.eventLog.Schema`
* Where `Config: { samplingRate: BoundedNumber<0, 1> }`