2.0.1 • Published 5 years ago

react-tracktor v2.0.1

Weekly downloads
102
License
MIT
Repository
github
Last release
5 years ago

react-tracktor 🚜

<Tracktor /> is a React render prop, inspired by react-tracking, to gradually build tracking data down the render tree. At each level, all data is available from "above" while also passing it "down". It provides several ways to trigger tracking events.

Documentation

https://react-tracktor.netlify.com/

Caveats

  • This library only works with React versions that support hooks. That means 16.8.0 and up.
  • This library uses the IntersectionObserver Browser API under the hood. This feature might not work without a polyfill for older browsers.

Installation

yarn add react-tracktor

or

npm install react-tracktor

Example

import { Tracktor, TracktorProvider } from 'react-tracktor';

<TracktorProvider dispatcher={trackingData => console.log(trackingData)}>
  <Tracktor trackingData={{ page: 'README.md' }}>
    <main>
      <h2>README.md</h2>

      <Tracktor trackingData={{ section: 'example' }}>
        {({ trackEvent }) => (
          <section>
            <h3>
              <code>trackEvent</code>
            </h3>

            <button onClick={() => trackEvent({ button: 'hello' })}>Hello</button>

            <button onClick={() => trackEvent({ button: 'goodbye' })}>Goodbye</button>
          </section>
        )}
      </Tracktor>
    </main>
  </Tracktor>
</TracktorProvider>;

API

<TracktorProvider />

<TracktorProvider /> is required for <Tracktor /> to work. Every render-tree requires one <TracktorProvider />. This is also where the dispatcher, the callback that is called whenever a tracking event occurs, is defined.

Props

NameTypeDefaultDescription
dispatcher(trackingData: object) => void(trackingData) => console.log(trackingData)The dispatcher is a callback that is called on every tracking event and is responsible to process and forward the data for and to any analytics solution.

<Tracktor />

The <Tracktor /> render prop enables the consumer to gradually build tracking data. At each point at the render tree data from all <Tracktor /> render props above is available without the data "underneath".

Props

<Tracktor /> Props
NameTypeDefaultDescription
childrenReactNode | (value: TracktorRenderProp) => ReactNoderequiredIf children is a function, it will act as a render prop that will be called with the content of TracktorRenderProp. If it's not a function, it will render the children.
eventDataobject{}An object with tracking data that should be provided to the dispatcher when the onClickWrapper is fired or (when using the intersectionRef or intersectionWrapper) the component scrolls into view.
intersectionOptionsobject{ triggerOnce: true }Options passed to the IntersectionObserver. See: https://github.com/thebuilder/react-intersection-observer#options
trackingDataobject{}An object with data that should be used when tracking events from this component and all components further down the render tree.
render Props
NameTypeDescription
createTrackEvent(trackEventData: object) => () => voidHigher-order-function to create a trackEvent function. Normally used in onClick props. Example: <button onClick={createTrackEvent({ hello: 'world' })}>Click Me</button>}>
intersectionRefReactRefIf used as a ref for a DOM element, a trackEvent will be fired with the the provided eventData when the refed element scrolls into view.
intersectionWrapper(WrappedComponent: ReactNode) => ReactNode)Can be used to wrap a component to inject an invisibile <div /> which will have the intersectionRef and track on intersection.
onClickWrapper(trackEventData: object) => () => voidCan be used to wrap a component's onClick prop, the returning function, also fires an trackEvent.
trackEvent(trackEventData: object) => voidFunction which fires an trackEvent with the provided data, merged with all previously provided trackingData.

useTracktor() 🎣

Since hooks can't provide context, useTracktor() is not a complete alternative to <Tracktor />, but provides the same functions as <Tracktor />'s render prop. It can also be provided with pageViewData to track page views. See <Tracktor /> & <PageView /> for more information.

Props

NameTypeDefaultDescription
eventDataobject{}An object with tracking data that should be provided to the dispatcher when the onClickWrapper is fired or (when using the intersectionRef or intersectionWrapper) the component scrolls into view.
intersectionOptionsobject{ triggerOnce: true }Options passed to the IntersectionObserver. See: https://github.com/thebuilder/react-intersection-observer#options
pageViewDataobjectundefinedAn object with data that the dispatcher should be called with on page load.
trackingDataobject{}An object with data that should be used when tracking events from this component and all components further down the render tree.

Returns

NameTypeDescription
createTrackEvent(trackEventData: object) => () => voidHigher-order-function to create a trackEvent function. Normally used in onClick props. Example: <button onClick={createTrackEvent({ hello: 'world' })}>Click Me</button>}>
intersectionRefReactRefIf used as a ref for a DOM element, a trackEvent will be fired with the the provided eventData when the refed element scrolls into view.
intersectionWrapper(WrappedComponent: ReactNode) => ReactNode)Can be used to wrap a component to inject an invisibile <div /> which will have the intersectionRef and track on intersection.
onClickWrapper(trackEventData: object) => () => voidCan be used to wrap a component's onClick prop, the returning function, also fires an trackEvent.
trackEvent(trackEventData: object) => voidFunction which fires an trackEvent with the provided data, merged with all previously provided trackingData.

<PageView />

<PageView /> is used to fire an tracking event when the page loads. It needs to be wrapped in one top-level <TracktorProvider /> per render-tree which defines a dispatcher function that recieves the provided pageViewData and does something with it. See the <TracktorProvider /> section for details.

Props

NameTypeDefaultDescription
pageViewDataobjectrequiredAn object with data that the dispatcher should be called with on page load.

Alternatives

react-tracking is what inspired this approach to tracking. Use it for more advanced use cases.

Development

This project was bootstrapped with TSDX.

2.0.1

5 years ago

2.0.0

5 years ago

2.0.0-1

5 years ago

2.0.0-0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago