Code Monkey home page Code Monkey logo

use-react-amplitude's Introduction

use-react-amplitude

codecov npm

React hooks for Amplitude.

Features

  • Small. 27.2 kB (minified and gzipped). Size Limit controls the size.
  • Simple.
  • Modern. Based on the latest Typescript SDK for Web, but does not required installation. Сan be used with the Typescript SDK.
  • Typescript support.

Install

npm install use-react-amplitude

Changelog

https://github.com/Valyay/use-react-amplitude/blob/main/CHANGELOG.md

How to use

First, you must initialize the SDK. Find your Amplitude project's API Key in your project's Settings page and pass the first argument in the function initAmplitude. Your project's API key is required. You can pass an optional user ID and options object in this call. After initialization you can use hooks.

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { initAmplitude } from '@valyay/use-react-amplitude';

// Option 1, initialize with apiKey only
initAmplitude('YOUR_AMPLITUDE_API_KEY');

// Option 2, initialize with user id
initAmplitude('YOUR_AMPLITUDE_API_KEY', '[email protected]');

// Option 3, initialize with options
initAmplitude('YOUR_AMPLITUDE_API_KEY', '[email protected]', your_options);

ReactDOM.render(
    <React.StrictMode>
      <App />
  </React.StrictMode>,
document.getElementById("root"),
);

Track event when component mounts

import React from "react";
import { useTrackOnMount } from '@valyay/use-react-amplitude';

export const Component = () => {

  // Option 1, initialize with event input only
  const {code, event, message} = useTrackOnMount('user_event');

  // Option 2, initialize with event properties
  const {code, event, message} = useTrackOnMount('user_event', {userProp: "foo"});

  // Option 3, initialize with event options
  const {code, event, message} = useTrackOnMount('user_event', {userProp: "foo"}, {insert_id: "test_insert_id"});

  // Hook return code, event and message of event
  const {code, event, message} = useTrackOnMount('user_event', {userProp: "foo"}, {insert_id: "test_insert_id"});

  // console.log(event); // {...}
  // console.log(code); // 200
  // console.log(message); // "Event tracked successfully"

  return <div>Title</div>;

};

Track event when component mounts or dependencies change

import React, { useState } from "react";
import { useTrackOnChange } from '@valyay/use-react-amplitude';

export const Component = () => {

  const [count, setCount] = useState<number>(0);

  // Initialize with event input only
  useTrackOnChange("count increase", [count]);
  
  // Hook return code, event and message of event
  const {code, event, message} = useTrackOnChange("count increase", [count]);

  // console.log(event); // {...}
  // console.log(code); // 200
  // console.log(message); // "Event tracked successfully"

  return (
    <div>
        <button onClick={()=>{setCount(count+1);}}>click</button>
  </div>
  );

};

Track event when only dependencies change

import React, { useState } from "react";
import { useTrackOnUpdate } from '@valyay/use-react-amplitude';

export const Component = () => {

  const [count, setCount] = useState<number>(0);
  
  // Initialize with event input only
  useTrackOnUpdate("count increase", [count]);
  
  // Hook return code, event and message of event
  const {code, event, message} = useTrackOnChange("count increase", [count]);
  // console.log(event); // {...}
  // console.log(code); // 200
  // console.log(message); // "Event tracked successfully"
  
  return (
    <div>
        <button onClick={()=>{setCount(count+1);}}>click</button>
  </div>
  );

};

API

initAmplitude

Wrapper for initialization Amplitude. Full analogue of init.

Prop Type Required
apiKey string true
userId string false
options BrowserOptions false

useTrackOnMount

Hook is called after the component is mounted. Analogue of componentDidMount.

Prop Type Required
eventInput string true
eventProperties Record<string, unknown> false
eventOptions EventOptions false

useTrackOnChange

The hook is called after mounting a component or changing any of the values in the array. (deep comparison is used)

Prop Type Required
eventInput string true
dependencies DependencyList true
eventProperties Record<string, unknown> false
eventOptions EventOptions false

useTrackOnUpdate

The hook is called after changing any of the values in the array. Analogue of componentDidUpdate. (deep comparison is used)

Prop Type Required
eventInput string true
dependencies DependencyList true
eventProperties Record<string, unknown> false
eventOptions EventOptions false

use-react-amplitude's People

Contributors

valyay avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.