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-appinsights-upgraded

v0.0.1

Published

Application Insights module for React applications - forked from 2.0.2 to upgrade appinsights-usage package

Downloads

42

Readme

react-appinsights

Build Status npm Downloads per month dependencies Greenkeeper badge

Javascript module to include Application Insights in applications built with React.
It extends Application Insights with additional React-specific features:

  • tracking of router changes
  • React components usage statistics
  • API to extend the standard telemetry with additional dimensions

Installation

With npm:

npm install --save react-appinsights

Usage

To initialize Application Insights add the following to the entry point file of your application (e.g. index.js):

import ReactAI from 'react-appinsights';
ReactAI.init({instrumentationKey:'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx'});

See this Application Insights tutorial for Node.js for more details on how to obtain the instrumentation key.

Track router changes

To track page views, pass a history object to the init method.
For more information see the documentation of the react-router package.

import ReactAI from 'react-appinsights';
import { Router } from 'react-router-dom';
import { createBrowserHistory } from 'history';

const history = createBrowserHistory()
ReactAI.init({instrumentationKey:'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx'}, history);

ReactDOM.render(
  <Router history={history}>
    <App />
  </Router>,
  document.getElementById("root")
);

Enable React components usage tracking

To enable React components usage tracking, apply the withTracking higher-order component function.

import ReactAI from 'react-appinsights';

class MyComponent extends React.Component {
    ... 
}

export default ReactAI.withTracking(MyComponent);

If, for any reason, you want to change the name string of the component that appears in Application Insights, you can pass in a custom name as second argument of withTracking.

export default ReactAI.withTracking(MyComponent, "CustomMyComponentName");

It will measure time from the ComponentDidMount event through the ComponentWillUnmount event. However, in order to make this time more accurate it will subtract idle time. In other words, React Component Engaged Time = ComponentWillUnmount timestamp - ComponentDidMount timestamp - idle time.

To see this metric in Azure portal you need to navigate to Application Insights resource, select Metrics Explorer from the top menu and configure one of the empty charts to display Custom metrics "React Component Engaged Time" grouped by Component Name. It can take up to 10 minutes for new custom metric to appear in Azure Portal.

Set application context

To augment all telemetry with additional properties use setAppContext method. For instance:

ReactAI.setAppContext({urlReferrer: document.referrer});

This will add urlReferrer property to all page views, ajax calls, exceptions and other telemetry sent to Application Insights:

Get AppInsights object

Use the following method to get the original AppInsights object:

var appInsights = ReactAI.ai();

Refer to this doc for information on the Javascript API of Application Insights.