All files / src/lib fingerprint.service.ts

89.47% Statements 17/19
60% Branches 3/5
100% Functions 3/3
88.88% Lines 16/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 691x 1x 1x 1x   1x                                                           1x           6x   6x 6x 6x 6x 6x               1x     1x       1x     1x      
import { Inject, Injectable, PLATFORM_ID } from '@angular/core'
import { isPlatformBrowser } from '@angular/common'
import * as Fingerprint from '@fingerprint/agent'
import { packageVersion } from './version'
import { FingerprintSettings } from './interfaces/fingerprint-settings'
import { FINGERPRINT_ANGULAR_SETTINGS_TOKEN } from './tokens/fingerprint-angular-settings-token'
 
/**
 * Inject FingerprintService and use it to make identification requests.
 *
 * @example ```typescript
 * import { Component, OnInit } from '@angular/core';
 * import { FingerprintService } from '@fingerprintjs/fingerprintjs-pro-angular';
 *
 * @Component({
 *   selector: 'app-home',
 *   templateUrl: './home.component.html',
 *   styleUrls: ['./home.component.css']
 * })
 * export class HomeComponent implements OnInit {
 *
 *   constructor(private FingerprintService: FingerprintService) { }
 *
 *   eventId = 'Press "Identify" button to get eventId';
 *
 *   async onIdentifyButtonClick() : Promise<void> {
 *     const data = await this.FingerprintService.getVisitorData();
 *     this.eventId = data.event_id;
 *   }
 * }
 * ```
 */
@Injectable({
  providedIn: 'root',
})
export class FingerprintService {
  private readonly publicAgent: Fingerprint.Agent | undefined
  private readonly settings: FingerprintSettings
 
  constructor(
    @Inject(FINGERPRINT_ANGULAR_SETTINGS_TOKEN) settings: FingerprintSettings,
    @Inject(PLATFORM_ID) private readonly platformId: any
  ) {
    this.settings = settings
    if (isPlatformBrowser(this.platformId)) {
      const { startOptions } = settings
      const integrationInfo = startOptions.integrationInfo || []
      this.publicAgent = Fingerprint.start({
        ...startOptions,
        integrationInfo: [...integrationInfo, `angular/${packageVersion}`],
      })
    }
  }
 
  getVisitorData(getDataOptions?: Fingerprint.GetOptions): Promise<Fingerprint.GetResult> {
    Iif (!this.publicAgent) {
      return Promise.reject(new Error('Fingerprint agent is not initialized on this platform'))
    }
    return this.publicAgent.get(getDataOptions)
  }
 
  collectData(collectOptions?: Fingerprint.GetOptions): Promise<string> {
    Iif (!this.publicAgent) {
      return Promise.reject(new Error('Fingerprint agent is not initialized on this platform'))
    }
    return this.publicAgent.collect(collectOptions)
  }
}