1.0.1 • Published 1 year ago

use-react-amplitude v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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', 'user@gmail.com');

// Option 3, initialize with options
initAmplitude('YOUR_AMPLITUDE_API_KEY', 'user@gmail.com', 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.

PropTypeRequired
apiKeystringtrue
userIdstringfalse
optionsBrowserOptionsfalse

useTrackOnMount

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

PropTypeRequired
eventInputstringtrue
eventPropertiesRecord<string, unknown>false
eventOptionsEventOptionsfalse

useTrackOnChange

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

PropTypeRequired
eventInputstringtrue
dependenciesDependencyListtrue
eventPropertiesRecord<string, unknown>false
eventOptionsEventOptionsfalse

useTrackOnUpdate

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

PropTypeRequired
eventInputstringtrue
dependenciesDependencyListtrue
eventPropertiesRecord<string, unknown>false
eventOptionsEventOptionsfalse