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

@formo/analytics

v2.0.0-alpha.6

Published

## Option 1 - tracking code

Downloads

710

Readme

Installation Guide

Option 1 - tracking code


Add the following to your index.html:

<script>
  const script = document.createElement('script');
  const apiKey = 'YOUR_API_KEY';
  const projectId = 'YOUR_PROJECT_ID';

  script.src = 'https://unpkg.com/@formo/analytics';
  script.onload = function () {
    FormoAnalytics.init(apiKey, projectId)
      .then((sdkInstance) => {
        window.formo = sdkInstance;

        // Call the public `page` method to track a page hit
        window.formo.page();
      })
      .catch((error) => {
        console.error('Error initializing FormoAnalytics:', error);
      });
  };
  document.head.appendChild(script);
</script>

Option 2 - using npm package


1. Install the npm package:

Install @formo/analytics via yarn or npm:

yarn add @formo/analytics

or

npm install @formo/analytics --save

2. Set up the FormoAnalyticsProvider in your application:

// AnalyticsProvider.tsx

'use client';

import { FormoAnalytics, FormoAnalyticsProvider } from '@formo/analytics';
import React, { FC, useEffect } from 'react';

type FormoAnalyticsProviderProps = {
  apiKey: string,
  projectId: string,
  children: React.ReactNode,
};

// The provider component
export const AnalyticsProvider: FC<FormoAnalyticsProviderProps> = ({
  apiKey,
  projectId,
  children,
}) => {
  // Initialize the FormoAnalytics SDK inside useEffect
  const [isInitialized, setIsInitialized] = useState(false);

  useEffect(() => {
    const initialize = async () => {
      try {
        await FormoAnalytics.init(apiKey, projectId);
        console.log('FormoAnalytics SDK initialized');
        setIsInitialized(true);
      } catch (error) {
        console.error('Failed to initialize FormoAnalytics SDK', error);
      }
    };

    initialize();
  }, [apiKey, projectId]);

  // To prevent app crashes, render a loading state during initialization
  if (!isInitialized) {
    return (
      <FormoAnalyticsProvider apiKey={apiKey} projectId={projectId}>
        Loading Content
      </FormoAnalyticsProvider>
    );
  }

  return (
    <FormoAnalyticsProvider apiKey={apiKey} projectId={projectId}>
      {children}
    </FormoAnalyticsProvider>
  );
};

export default AnalyticsProvider;

3. Integrating the Provider in Your Root Layout

Wrap your application with the newly created AnalyticsProvider in your main layout file:

import { AnalyticsProvider } from './AnalyticsProvider';

export default function RootLayout({
  children,
}: {
  children: React.ReactNode,
}) {
  return (
    <html lang='en'>
      <body>
        <AnalyticsProvider apiKey='YOUR_API_KEY' projectId='YOUR_PROJECT_ID'>
          Your Page Content
        </AnalyticsProvider>
      </body>
    </html>
  );
}

4. Using the SDK

Once the SDK is initialized, you can use its methods to track events and user interactions. Here’s how to do that:

import { useFormoAnalytics } from '@formo/analytics';
import React, { useEffect } from 'react';

const YourComponent = () => {
  const analytics = useFormoAnalytics();

  useEffect(() => {
    const track = async () => {
      try {
        console.log('Tracking page hit...');
        analytics.page(); // Track the page view
        analytics.track('custom_event', { key: 'value' }); // Track a custom event
      } catch (error) {
        console.error('Failed to track page hit', error);
      }
    };

    track();
  }, [analytics]);

  return <div>Your Component Content</div>;
};

Development notes

To run a local version of the script:

  1. Run yarn build-cjs && yarn build-esm && yarn webpack --mode=production or npm run build at the root level to build the script.
  2. To authorize device, login into npmjs using npm login or npm adduser
  3. Run yarn publish or npm run publish to publish new versions of the package.