fingerprintjs / fingerprintjs-pro-react

Fingerprint Pro Wrapper for React Single Page Applications (SPA)
MIT License
52 stars 8 forks source link
device-identification device-identifier fingerprinting fingerprintjs fraud-detection fraud-prevention identification integration multi-account-detection nextjs react single-page-app single-page-application spa wrapper

Fingerprint logo

CI badge coverage Current NPM version Monthly downloads from NPM MIT license Discord server Discord server

# Fingerprint Pro React Fingerprint is a device intelligence platform offering 99.5% accurate visitor identification. Fingerprint Pro React SDK is an easy way to integrate **[Fingerprint Pro](https://fingerprint.com/)** into your React application. It's also compatible with Next.js and Preact. See application demos in the [examples](https://github.com/fingerprintjs/fingerprintjs-pro-react/tree/main/examples) folder. ## Table of contents - [Requirements](#requirements) - [Installation](#installation) - [Getting started](#getting-started) - [Linking and tagging information](#linking-and-tagging-information) - [Caching strategy](#caching-strategy) - [Error handling](#error-handling) - [API Reference](#api-reference) - [Support and feedback](#support-and-feedback) - [License](#license) ## Requirements - React 18 or higher - For Preact users: Preact 10.3 or higher - For Next.js users: Next.js 13.1 or higher - For Typescript users: Typescript 4.8 or higher > [!NOTE] > This package assumes you have a Fingerprint Pro subscription or trial, it is not compatible with the [source-available FingerprintJS](https://github.com/fingerprintjs/fingerprintjs). See our documentation to learn more about the [differences between Fingerprint Pro and FingerprintJS](https://dev.fingerprint.com/docs/identification-vs-fingerprintjs). ## Installation Using [npm](https://npmjs.org): ```sh npm install @fingerprintjs/fingerprintjs-pro-react ``` Using [yarn](https://yarnpkg.com): ```sh yarn add @fingerprintjs/fingerprintjs-pro-react ``` Using [pnpm](https://pnpm.js.org): ```sh pnpm add @fingerprintjs/fingerprintjs-pro-react ``` ## Getting started In order to identify visitors, you'll need a Fingerprint Pro account (you can [sign up for free](https://dashboard.fingerprint.com/signup/)). To get your API key and get started, see the [Fingerprint Pro Quick Start Guide](https://dev.fingerprint.com/docs/quick-start-guide). ### 1. Wrap your application (or component) in ``. - Set `apiKey` to your Fingerprint [Public API Key](https://dashboard.fingerprint.com/api-keys). - Set `region` if you have chosen a non-global [region](ttps://dev.fingerprint.com/docs/regions) during registration. - Set `endpoint` and `scriptUrlPattern` if you are using [one of our proxy integrations to increase accuracy](https://dev.fingerprint.com/docs/protecting-the-javascript-agent-from-adblockers) and effectiveness of visitor identification. ```jsx // src/index.js import React from 'react' import ReactDOM from 'react-dom/client' import { FpjsProvider, FingerprintJSPro, } from '@fingerprintjs/fingerprintjs-pro-react' import App from './App' const root = ReactDOM.createRoot(document.getElementById('app')) root.render( //loader_v.js', FingerprintJSPro.defaultScriptUrlPattern, ], }} > ) ``` ### 2. Use the `useVisitorData()` hook in your components to identify visitors ```jsx // src/App.js import React from 'react' import { useVisitorData } from '@fingerprintjs/fingerprintjs-pro-react' function App() { const { isLoading, error, data } = useVisitorData() if (isLoading) { return
Loading...
} if (error) { return
An error occured: {error.message}
} if (data) { // Perform some logic based on the visitor data return (
Welcome {data.visitorFound ? 'back' : ''}, {data.visitorId}!
) } else { return null } } export default App ``` The `useVisitorData` hook also returns a `getData` method you can use to make an API call on command. - You can pass `{ ignoreCache: true }` to `useVisitorData` to force a fresh identification request. - You can pass `{ immediate: false }` to `useVisitorData` to disable automatic visitor identification on render. ```jsx // src/App.js import React, { useState } from 'react' import { useVisitorData } from '@fingerprintjs/fingerprintjs-pro-react' function App() { const { isLoading, error, getData } = useVisitorData( { ignoreCache: true }, { immediate: false } ) const [email, setEmail] = useState('') if (isLoading) { return
Loading...
} if (error) { return
An error occurred: {error.message}
} return (
{ e.preventDefault() getData() .then((data) => { // Do something with the visitor data, for example, // append visitor data to the form data to send to your server console.log(data) }) .catch((error) => { // Handle error }) }} > setEmail(e.currentTarget.value)} />
) } export default App ``` - See the full code example in the [examples folder](./examples/). - See our [Use cases](https://demo.fingerprint.com) page for [open-source](https://github.com/fingerprintjs/fingerprintjs-pro-use-cases) real-world examples of using Fingerprint to detect fraud and streamline user experiences. ## Linking and tagging information The `visitorId` provided by Fingerprint Identification is especially useful when combined with information you already know about your users, for example, account IDs, order IDs, etc. To learn more about various applications of the `linkedId` and `tag`, see [Linking and tagging information](https://dev.fingerprint.com/docs/tagging-information). Associate the visitor ID with your data using the `linkedId` or `tag` parameter of the options object passed into the `useVisitorData()` hook or the `getData` function: ```jsx // ... function App() { const { isLoading, error, getData } = useVisitorData({ linkedId: 'user_1234', tag: { userAction: 'login', analyticsId: 'UA-5555-1111-1', }, }) } // ... ``` ## Caching strategy Fingerprint Pro usage is billed per API call. To avoid unnecessary API calls, it is a good practice to cache identification results. By default, the SDK uses `sessionStorage` to cache results. - Specify the `cacheLocation` prop on `` to instead store results in `memory` or `localStorage`. Use `none` to disable caching completely. - Specify the `cache` prop on `` to use your custom cache implementation instead. For more details, see [Creating a custom cache](https://github.com/fingerprintjs/fingerprintjs-pro-spa#creating-a-custom-cache) in the Fingerprint Pro SPA repository (a lower-level Fingerprint library used by this SDK). - Pass `{ignoreCache: true}` to the `getData()` function to ignore cached results for that specific API call. > [!NOTE] > If you use data from [`extendedResult`](https://dev.fingerprint.com/reference/get-function#extendedresult), pay additional attention to your caching strategy. > Some fields, for example, `ip` or `lastSeenAt`, might change over time for the same visitor. Use `getData({ ignoreCache: true })` to fetch the latest identification results. ## Error handling The `getData` function throws errors directly from the JS Agent without changing them. See [JS Agent error handling](https://dev.fingerprint.com/reference/error-handling) for more details. ## API Reference See the full [generated API reference](https://fingerprintjs.github.io/fingerprintjs-pro-react/). ## Support and feedback To ask questions or provide feedback, use [Issues](https://github.com/fingerprintjs/fingerprintjs-pro-react/issues). If you need private support, please email us at `oss-support@fingerprint.com`. If you'd like to have a similar React wrapper for the [open-source FingerprintJS](https://github.com/fingerprintjs/fingerprintjs), consider creating an issue in the main [FingerprintJS repository](https://github.com/fingerprintjs/fingerprintjs/issues). ## License This project is licensed under the MIT license. See the [LICENSE](https://github.com/fingerprintjs/fingerprintjs-pro-react/blob/main/LICENSE) file for more info.