Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

@dapperlabs/react-analytics

0.1.0 • Public • Published

react-analytics 👩‍🔬

Convenience component for creating a connection to segment.

Values provided on context:

  • analyticsReady: boolean
  • identify: function
  • alias: function
  • trackButtonClick: function
  • trackPageView: function
  • track: function

Provider Props

  • children: the contained react components
  • segmentWriteToken: the segment key

Setup

Import the provider and wrap your application in it.

import { AnalyticsProvider } from '@dapperlabs/react-analytics';

function Root() {
  return (
    <AnalyticsProvider
      segmentWriteToken={process.env.SEGMENT_WRITE_TOKEN}
    >
      <MyDApp />
    </AnalyticsProvider>
  )
}

Context

Context is directly available via import { AnalyticsContext } from '@dapperlabs/react-analytics' and can be consumed however you'd like:

  • useContext(AnalyticsContext) 👈IDEAL 😎
  • static contextType = AnalyticsContext
  • <AnalyticsContext.Consumer />
import React, { useContext, useRef } from 'react';
import { AnalyticsContext } from '@dapperlabs/react-analytics';

function Button({ children, id, onClick, trackingProperties }) {
  const buttonRef = useRef(null);
  const { trackButtonClick } = useContext(AnalyticsContext);

  const handleClick = () => {
    trackButtonClick({
      text: buttonRef.current.innerText || 'BUTTON_HAS_NO_TEXT',
      id: id || 'BUTTON_HAS_NO_ID',
      ...trackingProperties,
    });
    onClick();
  };

  return (
    <button
      ref={buttonRef}
      type="button"
      onClick={handleClick}
      id={id}
    >
      {children}
    </button>
  );
}

Keywords

none

Install

npm i @dapperlabs/react-analytics

DownloadsWeekly Downloads

224

Version

0.1.0

License

ISC

Unpacked Size

39.7 kB

Total Files

14

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar