Fingerprint is a device intelligence platform offering 99.5% accurate visitor identification.
FingerprintJS Pro Svelte SDK is an easy way to integrate Fingerprint into your Svelte or Svelte-kit application. See example apps in the examples folder.
- Svelte 4.0.0 or higher
- For TypeScript users: use Typescript 4.8 or higher
- For SvelteKit users: SvelteKit 1.0.0 or higher
This package works with the commercial Fingerprint platform. It is not compatible with the source-available FingerprintJS library. Learn more about the differences between Fingerprint and FingerprintJS.
Yarn:
yarn add @fingerprintjs/fingerprintjs-pro-svelte
npm:
npm install @fingerprintjs/fingerprintjs-pro-svelte
pnpm:
pnpm add @fingerprintjs/fingerprintjs-pro-svelte
In order to identify visitors, you'll need a Fingerprint Pro account (you can sign up for free). To get your API key and get started, see the Quick start guide in our documentation.
- Wrap your application (or component) in
FpjsProvider
. You can specify multiple configuration options. Set a region if you have chosen a non-global region during registration. Setendpoint
andscriptUrlPattern
if you are using one of our proxy integrations to increase accuracy and effectiveness of visitor identification.
// App.svelte
<script>
import { FpjsProvider, FingerprintJSPro } from '@fingerprintjs/fingerprintjs-pro-svelte'
import VisitorData from './VisitorData.svelte'
const options = {
loadOptions: {
apiKey: '<YOUR_API_KEY>',
endpoint: [
// "https://metrics.yourwebsite.com",
FingerprintJSPro.defaultEndpoint
],
scriptUrlPattern: [
// "https://metrics.yourwebsite.com/web/v<version>/<apiKey>/loader_v<loaderVersion>.js",
FingerprintJSPro.defaultScriptUrlPattern
],
// region: 'eu',
},
};
</script>
<FpjsProvider {options}>
<VisitorData />
</FpjsProvider>
- Use the
useVisitorData
function in your svelte components to indentify visitors and get the results.
// VisitorData.svelte
<script>
import { useVisitorData } from '@fingerprintjs/fingerprintjs-pro-svelte';
// Set to true fo fetch data when component is mounted
export let immediate = false;
const { getData, data, isLoading, error } = useVisitorData({ extendedResult: true }, { immediate });
$: {
if ($data) {
// Do something with visitorData here
}
}
</script>
<div>
<button id='get_data' on:click={() => getData()}> Get data</button>
{#if $isLoading}
<div id='loading'>Loading...</div>
{/if}
{#if $error}
<div id='error'>Error occurred: {$error.message}</div>
{/if}
{#if $data}
<div>
<!--visitorData is available here!-->
</div>
{/if}
</div>
See the full code in the provided example applications.
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.
Associate the visitor ID with your data using the linkedId
or tag
parameter of the options object passed into the useVisitorData()
hook:
<script>
import { useVisitorData } from '@fingerprintjs/fingerprintjs-pro-svelte';
const { getData, data, isLoading, error } = useVisitorData({
linkedId: 'user_1234',
tag: {
userAction: 'login',
analyticsId: 'UA-5555-1111-1',
},
});
</script>
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<FpjsProvider>
to instead store results inmemory
orlocalStorage
. Usenone
to disable caching completely. - Specify the
cache
prop on<FpjsProvider>
to use your custom cache implementation instead. For more details, see Creating a custom cache in the Fingerprint Pro SPA repository (a lower-level Fingerprint library used by this SDK). - Pass
{ignoreCache: true}
to thegetData()
function to ignore cached results for that specific API call.
Note
If you use data from 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.
See the generated SDK API reference here.
This library uses Fingerprint Pro JavaScript agent under the hood. See our documentation for the full JavaScript Agent API reference.
The getData
function throws errors directly from the JS Agent without changing them. See JS Agent error handling for more details.
To report problems, ask questions or provide feedback, please use Issues. If you need private support, you can email us at [email protected].
This project is licensed under the MIT license.