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

use-form-and-click-tracker

v1.0.3

Published

A React hook to store form data and track clicks

Downloads

3

Readme

useFormAndClickTracker

useFormAndClickTracker is a React custom hook for tracking form data and user click positions. It also provides a consent popup for users to allow cookie storage before any data is saved.

Installation

To install the package, run:

npm install use-form-and-click-tracker

Usage

import useFormAndClickTracker from 'use-form-and-click-tracker';
import ConsentPopup from './ConsentPopup'

Example Component

import React from 'react';
import useFormAndClickTracker from 'use-form-and-click-tracker';
import ConsentPopup from './ConsentPopup';

const FormComponent = ({ pageName, metadata }) => {
  const { formData, clickData, handleFormChange, hasConsent, showConsentPopup, handleConsent } = useFormAndClickTracker(pageName, metadata);

  return (
    <div>
      {showConsentPopup && <ConsentPopup onConsent={handleConsent} />}
      {hasConsent ? (
        <>
          <div data-section="section1">
            <h2>Section 1</h2>
            <form>
              <input
                type="text"
                name="username"
                value={formData.username || ''}
                onChange={handleFormChange}
                placeholder="Username"
              />
              <input
                type="email"
                name="email"
                value={formData.email || ''}
                onChange={handleFormChange}
                placeholder="Email"
              />
              <input
                type="password"
                name="password"
                value={formData.password || ''}
                onChange={handleFormChange}
                placeholder="Password"
              />
            </form>
          </div>
          <div data-section="section2">
            <h2>Section 2</h2>
            <button onClick={() => alert('Button in Section 2 clicked')}>Click me</button>
          </div>
          <div>
            <p>Total Clicks: {clickData.clickCount}</p>
            <p>Click Positions:</p>
            <ul>
              {clickData.clickPositions.map((pos, index) => (
                <li key={index}>X: {pos.x}, Y: {pos.y}, Section: {pos.section}</li>
              ))}
            </ul>
          </div>
        </>
      ) : (
        <p>Please allow cookies to use the form.</p>
      )}
    </div>
  );
};
export default FormComponent;

Consent Popup Component

Create a separate file for the consent

popup component:

import React from 'react';

const ConsentPopup = ({ onConsent }) => {
  const handleAccept = () => {
    onConsent(true);
  };

  const handleDecline = () => {
    onConsent(false);
  };

  return (
    <div style={popupStyle}>
      <p>Do you allow us to store cookies to improve your experience?</p>
      <button onClick={handleAccept} style={buttonStyle}>Accept</button>
      <button onClick={handleDecline} style={buttonStyle}>Decline</button>
    </div>
  );
};

const popupStyle = {
  position: 'fixed',
  bottom: '20px',
  right: '20px',
  backgroundColor: '#fff',
  padding: '15px',
  boxShadow: '0px 0px 10px rgba(0,0,0,0.1)',
  borderRadius: '5px',
  zIndex: 1000
};

const buttonStyle = {
  margin: '5px'
};

export default ConsentPopup;

Example Usage in an App

import React from 'react';
import FormComponent from './FormComponent';

const App = () => {
 const pageName = 'Home Page';
 const metadata = { title: 'Home', description: 'Welcome to the homepage' };

 return (
   <div>
     <FormComponent pageName={pageName} metadata={metadata} />
   </div>
 );
};

export default App;

API

useFormAndClickTracker(pageName, metadata)

Parameters pageName (string): The name of the current page. metadata (object): An object containing metadata about the page. Returns formData (object): An object containing the form data. clickData (object): An object containing the click data (click count and click positions). handleFormChange (function): A function to handle form changes. hasConsent (boolean): A boolean indicating whether the user has given consent for cookie storage. showConsentPopup (boolean): A boolean indicating whether to show the consent popup. handleConsent (function): A function to handle user consent.

License

MIT