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

@typedigital/telemetrydeck-react

v0.2.0

Published

A library for using TelemetryDeck in your React app.

Downloads

278

Readme

TelemetryDeck React

A library for using TelemetryDeck in your React app.

Installation

npm install -S @typedigital/telemetrydeck-react

Setup

To set up this library, simply create a TelemetryDeck instance with the factory createrTelemetryDeck and pass it to the TelemetryDeckProvider, which should sit relatively high up in your component tree. You'll need a TelemetryDeck account and an app to be able to use this libary.

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { TelemetryDeckProvider, createTelemetryDeck } from '@typedigital/telemetrydeck-react';
import { Dashboard } from './Dashboard';

const td = createTelemetryDeck({app: process.env.APP_ID, user: 'anonymous'});

const App = () => {

  return (
    <div>
      <TelemetryDeckProvider telemetryDeck={td}>
        <Dashboard />
      </TelemetryDeckProvider>
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById('root'));

Basic usage

To send signals, use the useTelemetryDeck hook and destructure the various methods that can be used to modify the instance or send signals to TelemetryDeck. For more information, see the TelemetryDeck documentation.

import * as React from 'react';
import { useTelemetryDeck } from '@typedigital/telemetrydeck-react';


function Dashboard() {

  const { signal } = useTelemetryDeck();

  const clickHandler = async () => {
    const res = await signal('click', { event: 'button-click', target: 'Call to Action' })
    console.log(res); // the response of the TelemetryDeck API
  }

  // If you want to track if a user saw a certain page or component just use an effect
  React.useEffect(() => {
    (async () => {
      const { pathname } = window.location;
      await signal('pageview', { component: 'dashboard', path: pathname });
    })();
  }, [])

  return (
    <React.Fragment>
      <h1>My Dashboard</h1>
      <button onClick={async () => await clickHandler()}>
        Click me
      </button>
    </React.Fragment>
  )
}

export {
  Dashboard
}

React Native & Expo Support

telemetrydeck-react also supports React Native or Expo. If no global implementation is available because you are not on the web, TelemetryDeck needs a subtle implementation which can be either injected by extending `globalThis or added to the TelemetryDeck instance.

In the React Native context, a TextEncoder is also needed for it to work properly.

If you are developing an Expo project, you should install the following dependencies in addition to this library.

npm i -S expo-crypto text-encoding

Monkey-Patching crypto and TextEncoder

To patch the functionalities, a file named globals.js should be created first. The following code should be added to this file. This code extends the global object for the React Native Context with the TextEncoder and the crypto.subtle.digest function, which converts a message to a hash.

// globals.js

import * as Crypto from 'expo-crypto';

globalThis.crypto = {
    subtle: {
        digest: (algorithm, message) => Crypto.digest(algorithm, message)
    }
}
global.TextEncoder = require('text-encoding').TextEncoder;

Finally, the created file should be imported into the index.js or any other root file for the bundler.

// index.js

import { registerRootComponent } from 'expo';
import './globals.js';
import App from './App';

registerRootComponent(App);

Feedback & Contributions

We appreciate any feedback. Pull requests, ideas for new features and bug reports are very welcome.

License

MIT

Sponsors