Skip to content

fingerprintjs/fingerprintjs-pro-angular

Repository files navigation

FingerprintJS

MIT license Discord server

FingerprintJS Pro Angular

FingerprintJS Pro Angular is an easy-to-use Angular library for FingerprintJS Pro. Example of usage is located in the src folder. This package works with FingerprintJS Pro, it is not compatible with open-source FingerprintJS. You can learn more about the difference between FingerprintJS Pro and open-source FingerprintJS in the official documentation.

Table of contents

Installation

Using npm:

npm install @fingerprintjs/ng-fingerprintjs-pro

Using yarn:

yarn add @fingerprintjs/ng-fingerprintjs-pro

Getting started

In order to identify visitors, you'll need a FingerprintJS Pro account (you can sign up for free). You can learn more about API keys in the official FingerprintJS Pro documentation.

  1. On your Angular Project, you shall include the NgFingerprintjsProModule on your highest level application module. The easiest install mode call the forRoot() method and pass the loadOptions with apiKey. You can specify multiple configuration options. Set a region if you have chosen a non-global region during registration. Please refer to the Regions page.
import { NgModule } from '@angular/core';
import { NgFingerprintjsProModule } from 'ng-fingerprintjs-pro';
// ...

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    NgFingerprintjsProModule.forRoot({loadOptions: {apiKey: 'your-fpjs-public-api-key'}})
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
  1. Add to your component NgFingerprintjsProService provided from DI. Use it method getVisitorData() to identify visitor.
import { Component, OnInit } from '@angular/core';
import { NgFingerprintjsProService } from 'ng-fingerprintjs-pro';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor(private ngFingerprintjsProService: NgFingerprintjsProService) { }
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  visitorId = 'Press "Identify" button to get visitorId';

  async onIdentifyButtonClick() : Promise<void> {
    const data = await this.ngFingerprintjsProService.getVisitorData();
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    this.visitorId = data.visitorId;
    this.extendedResult = data;
  }
}

Caching strategy

⚠️ WARNING If you use data from extendedResult, please pay additional attention to caching strategy.

FingerprintJS Pro uses API calls as the basis for billing. Our best practices strongly recommend using cache to optimise API calls rate. The Library uses the SessionStorage cache strategy by default.

Some fields from the extendedResult (e.g ip or lastSeenAt) might change for the same visitor. If you need exact current data, it is recommended to pass ignoreCache=true inside getData function.

Documentation

This library uses FingerprintJS Pro agent internally. The documentation for the FingerprintJS Pro agent is available on https://dev.fingerprintjs.com/docs.

NgFingerprintjsProModule

The module just initialize FingerprintJS Pro agent with load options, configure caching strategy and provides NgFingerprintjsProService to DI.

NgFingerprintjsProModule.forRoot props

loadOptions: FingerprintJS.LoadOptions

Options for the FingerprintJS JS Pro agent load() method. Options follow agent's initialisation properties.

cacheLocation?: CacheLocation

Defines which built-in cache mechanism the client should use. Caching options follow properties defined in fingerprintjs-pro-spa repository.

cache?: ICache

Custom cache implementation. Takes precedence over the cacheLocation property. Caching options follow properties defined in fingerprintjs-pro-spa repository.

cacheTimeInSeconds?: number

Duration in seconds for which data is stored in the cache. Cannot exceed 86_400 (24h) because caching data for longer than 24 hours can negatively affect identification accuracy. Caching options follow properties defined in fingerprintjs-pro-spa repository.

cachePrefix?: string

Custom prefix for localStorage and sessionStorage cache keys. Will be ignored if the cache is provided. Caching options follow properties defined in fingerprintjs-pro-spa repository.

NgFingerprintjsProService methods

getVisitorData(getOptions?: GetOptions)

Method performs identification requests with the FingerprintJS Pro API. The returned object contains information about loading status, errors, and visitor.

  • getOptions: GetOptions<TExtended> parameter follows parameters of the FingerprintJS Pro's get function.

clearCache

Method clears cache for current caching strategy.

Support and feedback

For support or to provide feedback, please raise an issue on our issue tracker. If you require private support, please email us at oss-support@fingerprintjs.com. If you'd like to have a similar Angular library for the open-source FingerprintJS, consider raising an issue in our issue tracker.

License

This project is licensed under the MIT license. See the LICENSE file for more info.