
Security News
Node.js Moves Toward Stable TypeScript Support with Amaro 1.0
Amaro 1.0 lays the groundwork for stable TypeScript support in Node.js, bringing official .ts loading closer to reality.
@qwant/telemetry-react
Advanced tools
Qwant library for collecting telemetry information through react components
The goal of the logger is to be able to log events happening on the app (click, display, loading...) trough a react app. This library try to solve the headache of building an event with data available at different levels of the application 🧠.
The telemetry is exposed with multiple systems.
To bootstrap useTelemetry
you need to set the configuration with TelemetryConfiguration
.
const telemetryLogger = (endpoint, data) => {
/* You can use whatever you want here */
navigator.sendBeacon(endpoint, JSON.stringify(data))
}
return (
<TelemetryConfiguration
fetcher={telemetryLogger}
endpoint="https://www.qwant.com/action"
product={{
name: 'front',
module: 'my-product',
version: '0.0.1',
}}
>
{/* Your application */}
</TelemetryConfiguration>
)
The telemetry provider is used to "tag" specific area of the application and allows children to retrieve the context of an event more easily.
// app.jsx
return (
<TelemetryProvider context={TelemetryEnumContexts.HOMEPAGE}>
<Box>
<TelemetryProvider component={TelemetryEnumComponents.SIDEBAR}>
<Sidebar /> {/* // here we will have "context" and "component" set */}
</TelemetryProvider>
</Box>
</TelemetryProvider>
)
// Sidebar
function Sidebar() {
const logClick = useLogCallback('ui', {
component: TelemetryEnumZones.BUTTON,
})
return <button onClick={logClick}>I'm a button</button>
}
The button will automatically send the context and component retrieved from the provider and will call sendEvent('ui', {context: 'homepage', component: 'sidebar', zone: 'button'})
. Multiple providers can be nested to provide information for emitted events.
Return a memoized function that sends an event.
function MyComponent({ onClick }) {
const loggableClick = useLogCallback(TelemetryEvents.UI, {
zone: TelemetryEnumZones.BLOG,
})
return <button onClick={loggableClick}>Hello</button>
}
Wrap the callback
and return a function that will send an event after the original callback call.
function MyComponent({ onClick }) {
const loggableClick = useLogCallback(onClick, TelemetryEvents.UI, {
zone: TelemetryEnumZones.BLOG,
})
return <button onClick={loggableClick}>Hello</button>
}
Send an event when the dependencies change or when the component is mounted
This hook will retrieve the sendEvent
method from the context. Try to use the other hooks as much as possible before using this one.
function MyComponent({ onClick }) {
const { sendEvent } = useTelemetry()
return <button onClick={() => sendEvent('ui', {})}>Hello</button>
}
Hooks are a good way to inject the logger faster, but it's not always the best way to handle things. With HOC you can easily decorate external components.
Wraps a component and inject a log
function as a prop that can be used to send events.
function MyComponent({ log }) {
return <button onMouseUp={log({ component: 'button' })}>Click me</button>
}
const MyComponentWithLog = Loggable(MyComponent, 'ui')
Wraps a component and wraps the on{Trigger}
props
// my-components.jsx
function MyComponent({ onClick }) {
return <button onClick={onClick}>Click me</button>
}
export const MyComponentWithLog = Loggable(MyComponent, 'ui', 'click')
// app.jsx
return <MyComponentWithLog onClick={(e) => doSomething()} />
// An event will be emitted after the doSomething call
You can add additional information using props prefixed by "telemetry".
return (
<MyComponentWithLog
telemetryZone={TelemetryEnumZones.LINK}
onClick={(e) => doSomething()}
/>
)
Wraps a component with a TelemetryProvider
// The goal is to avoid repeating provider
;<TelemetryProvider component="sidebar">
<Sidebar />
</TelemetryProvider>
// Instead we can export an already contextualized sidebar
const Sidebar = WithTelemetryContext(SidebarPure, { component: 'sidebar' })
// Then you can simply use <Sidebar/>
Wrap a component with an useLogEffect
. It is useful when we need to trigger
a display log when component is mounted for example.
import { Multi } from 'qwant-ia'
const LoggableMulti = WithLogEffect(
Multi,
TelemetryEvents.DISPLAY_ELEMENT,
{
ia: TelemetryEnumIA.MAPS_MULTI,
},
['myDeps']
)
function MapsPhoenix() {
/* ... */
if (places.length > 1) {
return <LoggableMulti /* ... */ />
}
}
FAQs
Qwant library for collecting telemetry information through react components
The npm package @qwant/telemetry-react receives a total of 3 weekly downloads. As such, @qwant/telemetry-react popularity was classified as not popular.
We found that @qwant/telemetry-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Amaro 1.0 lays the groundwork for stable TypeScript support in Node.js, bringing official .ts loading closer to reality.
Research
A deceptive PyPI package posing as an Instagram growth tool collects user credentials and sends them to third-party bot services.
Product
Socket now supports pylock.toml, enabling secure, reproducible Python builds with advanced scanning and full alignment with PEP 751's new standard.