-
# Fingerprint Pro SPA
[Fingerprint](https://fingerprint.com/) is a device intelligence platform offering 99.5% accurate visitor identification
-This library is designed to be used in single-page-application framework wrappers for the Fingerprint Pro JavaScript Agent.
-It has multiple built-in caching mechanisms with recommended default settings.
+This library is designed to be used in single-page-application framework wrappers for the Fingerprint Pro JavaScript Agent.
+It has multiple built-in caching mechanisms with recommended default settings.
If you just need the Fingerprint Pro [JS agent](https://www.npmjs.com/package/@fingerprintjs/fingerprintjs-pro), you can use it directly, without this wrapper. If you're looking for a framework-specific integration, we have dedicated SDKs for [React (including Next, Preact)](https://github.com/fingerprintjs/fingerprintjs-pro-react), [Vue](https://github.com/fingerprintjs/fingerprintjs-pro-vue), [Svelte](https://github.com/fingerprintjs/fingerprintjs-pro-svelte) and [Angular](https://github.com/fingerprintjs/fingerprintjs-pro-angular).
-**This SDK works with Fingerprint Pro, it will not work with the open-source FingerprintJS version!**
-Learn more about the [difference between Pro and OSS](https://dev.fingerprint.com/docs/pro-vs-open-source).
+**This SDK works with Fingerprint Pro, it will not work with the open-source FingerprintJS version!**
+Learn more about the [difference between Pro and OSS](https://dev.fingerprint.com/docs/pro-vs-open-source).
If you'd like to have a similar SPA wrapper for the OSS version of FingerprintJS, consider [raising an issue in our issue tracker](https://github.com/fingerprintjs/fingerprintjs-pro-spa/issues).
## Table of Contents
@@ -35,6 +34,7 @@ If you'd like to have a similar SPA wrapper for the OSS version of FingerprintJS
- [Requirements](#requirements)
- [Installation](#installation)
- [Getting Started](#getting-started)
+- [Caching](#caching)
- [Support and Feedback](#support-and-feedback)
- [Documentation](#documentation)
- [License](#license)
@@ -77,23 +77,21 @@ In order to identify visitors you'll need a Fingerprint Pro account (you can [si
Create a `FpjsClient` instance before rendering or initializing your application. You should only have one instance of the client. You need to specify your public API key and other configuration options based on your chosen region and active integration.
```js
-import {
- FpjsClient,
- FingerprintJSPro
-} from '@fingerprintjs/fingerprintjs-pro-spa';
+import { FpjsClient, FingerprintJSPro } from '@fingerprintjs/fingerprintjs-pro-spa'
-// It can receive multiple parameters but the only required one is `loadOptions`,
-// which contains the public API key
const fpjsClient = new FpjsClient({
+ // You can also pass these options later in `.init()` method
loadOptions: {
- apiKey: "",
+ apiKey: '',
// endpoint: ["", FingerprintJSPro.defaultEndpoint],
// scriptUrlPattern: ["", FingerprintJSPro.defaultScriptUrlPattern],
// region: "eu"
- }
-});
+ },
+})
```
-You can learn more about different load options in the [JS Agent API Reference](https://dev.fingerprint.com/docs/js-agent#initializing-the-agent).
+
+> [!NOTE]
+> You must provide `loadOptions` containing your public API key either in the constructor or in the `init` method. If you don't, the SDK will throw an error. You can learn more about different load options here in the [JS Agent documentation](https://dev.fingerprint.com/docs/js-agent#initializing-the-agent).
### 3. Initialize the JS Agent
@@ -102,6 +100,7 @@ Before you start making identification requests to the Fingerprint Pro API, you
```js
// with async/await
await fpjsClient.init()
+
const visitorData = await fpjsClient.getVisitorData()
// with promises
@@ -110,6 +109,17 @@ const visitorData = fpjsClient.init().then(() => {
})
```
+You can also pass the `loadOptions` into the `init` method here. They will be merged with the options passed to the constructor.
+
+```js
+await fpjsClient.init({
+ apiKey: '',
+ // endpoint: ["", FingerprintJSPro.defaultEndpoint],
+ // scriptUrlPattern: ["", FingerprintJSPro.defaultScriptUrlPattern],
+ // region: "eu"
+})
+```
+
### 4. Identify visitors
The `getVisitorData` method returns visitor identification data based on the request [options](https://dev.fingerprint.com/docs/js-agent#get-options).
@@ -126,50 +136,70 @@ const visitorData = fpjsClient.getVisitorData({ extendedResult: true }).then((vi
})
```
-See the [JS Agent API reference](https://dev.fingerprint.com/docs/js-agent) for more details.
+See the [JS Agent API reference](https://dev.fingerprint.com/docs/js-agent) for more details.
-### Caching
+## Caching
Fingerprint Pro usage is billed per API call. To avoid unnecessary API calls, it is a good practice to cache identification results. The SDK provides three ways to cache visitor data out of the box:
-* Session storage (default) - `sessionStorage`
-* Local storage - `localStorage`
-* Memory - `memory`
-* No cache - `nocache`
-
+- Session storage (default) - `sessionStorage`
+- Local storage - `localStorage`
+- Memory - `memory`
+- No cache - `nocache`
+
You can specify the `cacheLocation` option when creating the `FpjsClient`:
```js
const fpjsClient = new FpjsClient({
loadOptions: {
- apiKey: "your-fpjs-public-api-key"
+ apiKey: 'your-fpjs-public-api-key',
},
- cacheLocation: 'localstorage'
+ cacheLocation: 'localstorage',
// You can also use the provided TypeScript enum
// cacheLocation: CacheLocation.LocalStorage
-});
+})
```
Cache keys are based on the combination of _GetOptions_. For example, API responses for calls with `extendedResult: true` and `extendedResult: false` are stored independently.
-* You can ignore the cached result for a specific API call by passing `{ ignoreCache: true }` to the `getVisitorData()` method.
-* You can also use your custom cache implementation as described below.
-
> [!NOTE]
-> If you use data from [`extendedResult`](https://dev.fingerprint.com/docs/js-agent#extendedresult), pay additional attention to your caching strategy.
-> Some fields, for example, `ip` or `lastSeenAt`, might change over time for the same visitor. Use `getVisitorData({ ignoreCache: true })` to fetch the latest identification results.
+> If you use data from [`extendedResult`](https://dev.fingerprint.com/docs/js-agent#extendedresult), pay additional attention to your caching strategy. Some fields, for example, `ip` or `lastSeenAt`, might change over time for the same visitor.
+
+You can ignore the cached result for a specific API call and using `{ ignoreCache: true }`:
-#### Creating a custom cache
+```js
+const visitorData = await fpjsClient.getVisitorData({ ignoreCache: true })
+```
+
+Check if your response was retrieved from cache using the returned `cacheHit` flag:
+
+```js
+const { cacheHit, ...visitorData } = await fpjsClient.getVisitorData()
+```
+
+Use `getVisitorDataFromCache` to directly retrieve responses from cache:
+
+```js
+// Checks if request matching given options is present in cache
+await fpjsClient.isInCache({ extendedResult: true })
+
+// Returns cached visitor data based on the request options, or undefined if the data is not present in cache
+const cachedResult = await fpjsClient.getVisitorDataFromCache({ extendedResult: true })
+```
+
+You can also use your custom cache implementation as described below.
+
+### Creating a custom cache
The SDK can use a custom cache store implemented inside your application. This is useful when a different data store is more convenient in your environment, such as a hybrid mobile app.
You can provide an object to the `cache` property of the SDK configuration that implements the following functions. All the functions can return a Promise or a static value.
-| Signature | Return type | Description |
-| -------------------------------- | ------------------------------ |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| `get(key)` | Promise