Skip to content

Add optional click tracking to clickable components #49

Description

@Arthurk12

Is your feature request related to a problem? Please describe.
Consumers of this library often need to track which components are being interacted with for analytics or usage telemetry purposes. Currently, they must manually wrap every component's onClick to collect this data, which is repetitive boilerplate that has to be maintained outside the library.

Describe the solution you'd like
An opt-in, zero-configuration click tracking system built into the library. When a consumer passes an id prop to any clickable component, clicks are automatically counted in an internal store. The consumer retrieves the report by calling a single exported function whenever needed.

// src/clickTracker.ts (new module)
export function getClickReport(): Record<string, number>
export function resetClickReport(): void

// internal — called by components
function trackClick(id: string): void

Describe alternatives you've considered

  • Base class / class inheritance: Discarded — this library uses functional components with hooks throughout. A class hierarchy would require rewriting all components and goes against React's compositional model.
  • Higher-Order Component (HOC): Would work but adds unnecessary wrapping complexity for a use case that doesn't require it.
  • Context + Provider: Makes the system reactive (triggers re-renders), but requires consumers to wrap their app with a <ClickTrackerProvider>, which is unnecessary overhead for a simple analytics report.

Affected component(s)
All components that have meaningful user interaction:

  • BBButton
  • BBBCheckbox
  • BBBToggle
  • BBBAccordion (expand/collapse)
  • BBBNavigation (item clicks)
  • BBBSelect (option selection)
  • BBBSearch (search action)
  • BBBModal (action buttons)

Proposed API / Usage Example

import { BBButton, BBBToggle, getClickReport, resetClickReport } from '@mconf/bbb-ui-components-react';

// No setup required — tracking activates automatically when id is passed
function App() {
  return (
    <>
      <BBButton id="confirm-btn" onClick={handleConfirm} label="Confirm" />
      <BBBToggle id="notifications-toggle" onChange={handleToggle} />
    </>
  );
}

// Retrieve the report whenever needed (e.g. on page unload, on user logout)
const report = getClickReport();
// → { "confirm-btn": 14, "notifications-toggle": 3 }

sendToAnalytics(report);
resetClickReport(); // optional: reset counters

Components without an id prop are silently ignored — zero behavior change for existing consumers.

Screenshots or mockups
N/A

Additional context

  • TypeScript type ClickReport = Record<string, number> should be exported alongside the functions.
  • resetClickReport() enables consumers to segment data by session or time window.
  • The module is a singleton (module-level Map<string, number>), so it works across the entire component tree without any provider or context setup.
  • Easily testable: call resetClickReport() between tests to isolate state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    Fields

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions