npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-trekker

v2.2.3

Published

Strongly typed simple decoupled integration of trackers.

Downloads

372

Readme

React-Trekker

Strongly typed simple decoupled integration of trackers.

Two steps:

  1. React components only register "contextual information" and emit events. No need to think about what GA dimensions to set, or what categories and labels. Just store context information like, "this is the search page body", "this is the 3rd card in a list" like this <WithTrekkerContext context={{card:13}}> ...</WithTrekkerContext> and all events coming from this part of the tree will have this context information.

  2. Handle events At another place you register your event listeners to send the information to your trackers, Google Analytics, Facebook, whatever you have. Events contain all context information available to them, so you know the button clicked was inside a card on the search-page for product X and was the 3rd card in a row.

Warning

All top level fields in the context object that are arrays are merged instead of overriden. At NODE_ENV = development (or empty), warnings will be console.warn-logged for overrideing values. See unit test for details.

how to use

Step 1:

import {TrekkerProvider, useTrekker, WithTrekkerContext, TrekkerHoc, bus} from 'react-trekker'

// or using strong typing

import {trekkerFactory} from 'react-trekker'
const {TrekkerProvider, useTrekker, `WithTrekkerContext`, TrekkerHoc, bus} = trekkerFactory<CustomContext, CustomEventNames>(optionalIni)

Now start top level with your TrekkerProvider to initiate the context, and use WithTrekkerContext to add variables to your context. Using the hook useTrekker you can retrieve an instance of the Trekker:

 interface PublicTrekker<CustomContext,CustomEventTypes>{
    get context():Readonly<CustomContext>
    event(name:CustomEventTypes, extra?:CustomContext):void
  }

You can subscribe to these events to send them to your actual trackers using the bus of type TrekkerEventBus

interface TrekkerEventBus<CustomContext,CustomEventTypes>{
  dispatchEvent(event:PrivateTrekkerEvent<CustomContext, CustomEventTypes>):boolean

  addCatchAllEventListener(handler: TrekkerEventHandler<CustomContext, CustomEventTypes>):void
  removeCatchAllEventListener(handler:TrekkerEventHandler<CustomContext, CustomEventTypes>):void

  addEventListener(name:E, handler: TrekkerEventHandler<CustomContext, CustomEventTypes>):void
  removeEventListener(name:E, handler:TrekkerEventHandler<CustomContext, CustomEventTypes>):void
}

Philosophy

While building a UI you just want to think about the current components, not its place inside the entire tree. By implicitly passing along contextual information, simple components dont have to know their place inside the world, which means you can just focus on making a good and simple component.

In order to do tracking well you need to know what choices the user is making and what choices are not made, by adding context information about this to events, you can reason about the users behaviour.

For example, "component-in-view" events combined with "clicked-on-this" events, when combined in analytics provide information about what items the user had in view but did not click as well as what they did click.

By combining strong typing with decoupling of the trackers from your UI code, its a lot simpler to do analytics correct without worrying about it too much.

Example

This example here demos how to do strong typing, but you can also just straight up import {TrekkerProvider, useTrekker, WithTrekkerContext, TrekkerHoc, bus} from 'react-trekker' and skip the strong typing

test('Trekker adds context to events, with strong typing', async () => {
  const {TrekkerProvider, useTrekker, WithTrekkerContext, TrekkerHoc, bus} = trekkerFactory<CustomContext, CustomEventNames>(()=>({url: location.href}))

  const clicks:any[] = []
  // its recommended to do this part somwhere else in real life
  bus.addEventListener('klik',(evt) =>{
    clicks.push(evt)
  })
  const ButtonWithEvent = () =>{
    const trekker = useTrekker()
    return (<button className={"button"} onClick={() => trekker.event("klik")}>
      click
    </button>)
  }

  const HocComponent = TrekkerHoc({id:'abc'})(() => (<ButtonWithEvent />))

  const component = renderer.create(
    <div>
      Root
      <TrekkerProvider>
        <div>
          <HocComponent /> { /* this button will klik with id:abc */ }
        </div>
        <WithTrekkerContext context={{id:dfe}}>
          <div>
            <WithTrekkerContext context={{position:123}}>
              <ButtonWithEvent /> { /* this button will klik with id:dfe, position:123 */ }
            </WithTrekkerContext>
          </div>
        </WithTrekkerContext>
      </TrekkerProvider>
    </div>,
  );

test coverage

$ jest
 PASS  src/subscribers/__tests__/subscribers.test.ts
 PASS  src/__tests__/trekker-hooks-custom.test.tsx (5.061 s)
 PASS  src/__tests__/trekker-hooks.test.tsx (5.412 s)
 PASS  src/__tests__/bus.test.ts (6.045 s)
-----------------|---------|----------|---------|---------|-------------------
File             | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------------|---------|----------|---------|---------|-------------------
All files        |     100 |    71.42 |     100 |     100 |
 src             |     100 |       50 |     100 |     100 |
  bus.ts         |     100 |      100 |     100 |     100 |
  factory.tsx    |     100 |       50 |     100 |     100 | 23
  index.tsx      |     100 |      100 |     100 |     100 |
  trekker.ts     |     100 |       50 |     100 |     100 | 15
  types.ts       |     100 |      100 |     100 |     100 |
 src/subscribers |     100 |      100 |     100 |     100 |
  fbq.ts         |     100 |      100 |     100 |     100 |
  ga.ts          |     100 |      100 |     100 |     100 |
  gtag.ts        |     100 |      100 |     100 |     100 |
-----------------|---------|----------|---------|---------|-------------------

Test Suites: 4 passed, 4 total
Tests:       9 passed, 9 total
Snapshots:   0 total
Time:        10.946 s
Ran all test suites.