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

@gashon/analytics

v1.8.3

Published

Analytics Component

Downloads

13

Readme

@gashon/analytics

A React-based analytics tool for easy integration and tracking of analytics data in applications. Supports metadata foreign key linking, session tracking, browser fingerprinting, mouse click reporting, cursor hotspot tracking, visit duration, and bot mitigation.

Installation

npm install @gashon/analytics

Or using yarn:

yarn add @gashon/analytics

Usage @gashon/analytics offers two primary exports: Analytics and useAnalytics, with added support for browser fingerprinting and session tracking.

1. Analytics Component

Import and use the Analytics component in your application to automatically send analytics data on page mount.

import React from 'react';
import { Analytics } from '@gashon/analytics';

function App() {
  return (
    <div>
      <Analytics
        apiKey="YOUR_API_KEY"
        endpoint="YOUR_ENDPOINT_URL"
        metadata={{ cookie: document.cookie }}
        debug
        trackSession
        fingerprintBrowser
        trackClickEvents
      />
      {/* Your application components */}
    </div>
  );
}

export default App;

Props
apiKey: String - Your unique API key for authentication.
endpoint: String - The URL to which analytics data is sent.
debug: Boolean (optional) - Enables debug mode for additional logging.
metadata: Record<\String, String> (optional) - Optional metadata to associate with a request.
trackClickEvents: Boolean (default false) - Enables click event tracking.
trackSession: Boolean (default true) - Enables session tracking.
fingerprintBrowser: Boolean (default true) - Enables browser fingerprinting for unique user identification.
disableOnDev: Boolean (default false) - Disables sending analytics on local development envs.
trackMouseEvent: Boolean (default false) - Attaches a mouse movement listener to track the user's cursor throughout the session. Uses mouseMovementSamplingRate to determine tracking interval.
mouseMovementSamplingRate: Number - Determines the sampling rate for collecting mouse movement data.

2. useAnalytics Hook

For more control, use the useAnalytics hook. This allows you to send analytics data programmatically, with options for session tracking and browser fingerprinting.

import React, { useEffect } from 'react';
import { useAnalytics } from '@gashon/analytics';

function MyComponent() {
  const { data, error, isFetching } = useAnalytics({
    apiKey: 'YOUR_API_KEY',
    endpoint: 'YOUR_ENDPOINT_URL',
    metadata: { userId },
    trackClickEvents: true,
    fingerprintBrowser: false,
  });

  useEffect(() => {
    // You can use the data, error, and isFetching states here
  }, [data, error, isFetching]);

  return <div>My Component</div>;
}

export default MyComponent;

Click Event Tracking

To enable click event tracking, set trackClickEvents to true in your Analytics component or useAnalytics hook. Attach a data-tracking-label attribute to any HTML element you wish to track. When the element is clicked, a payload containing the label and additional click data is sent to your specified endpoint.

Mouse Event Tracking

The trackMouseMovement and mouseMovementSamplingRate props allow you to track a users cursor on the screen at a given interval. All collected data (MouseTrackEvent[]) is sent when the user leaves the page.

API

useAnalytics<T>(config: FetchData): UseAnalytics<T>

Parameters
config: Object
apiKey: String - Your API key.
endpoint: String - The endpoint URL.
metadata: Record<String, String> - Optional analytics metadata.
trackSession: Boolean (default true) - Enables session tracking.
trackClickEvents: Boolean (default false) - Enables click event tracking.
fingerprintBrowser: Boolean (default true) - Enables browser fingerprinting.
Returns
An object containing:

data: T | null - The response data.
error: Error | null - Any error encountered during the request.
isFetching: boolean - Indicates if the request is in progress.

Event Payloads

type RequestData = {
  payload: RequestPayload;
  checksum: string;
  disable_notifications: boolean;
  path: string;
  user_id: string | null;
  api_key: AnalyticsProps['apiKey'];
};

type ClickEventPayload = {
  event: 'click';
  request_id: string;
  metadata: Record<String, String>;
  window: {
    width: number;
    height: number;
  };
  element_rect: ElementRect;
  tag: string;
  session_id: string;
};

type PageVisitPayload = {
  event: 'visit';
  metadata: Record<String, String>;
  request_id: string;
  session_id: string;
  fingerprint_id?: string;
};

type PageLeavePayload = {
  event: 'leave';
  request_id: string;
  timestamp: number;
  session_id: string;
};

export type MouseTrackingPayload = {
  event: 'mouse_track';
  request_id: string;
  session_id: string;
  window: {
    width: number;
    height: number;
  };
  data: MouseTrackEvent[];
};

License
This project is licensed under the MIT License.