|
| 1 | +import type { Map as MapSDK } from "./Map"; |
| 2 | +import { config, MAPTILER_SESSION_ID } from "./config"; |
| 3 | +import { defaults } from "./defaults"; |
| 4 | +import packagejson from "../package.json"; |
| 5 | + |
| 6 | +/** |
| 7 | + * A Telemetry instance sends some usage and merics to a dedicated endpoint at MapTiler Cloud. |
| 8 | + */ |
| 9 | +export class Telemetry { |
| 10 | + private map: MapSDK; |
| 11 | + private registeredModules = new Set<string>(); |
| 12 | + |
| 13 | + /** |
| 14 | + * |
| 15 | + * @param map : a Map instance |
| 16 | + * @param delay : a delay in milliseconds after which the payload is sent to MapTiler cloud (cannot be less than 1000ms) |
| 17 | + */ |
| 18 | + constructor(map: MapSDK, delay: number = 2000) { |
| 19 | + this.map = map; |
| 20 | + |
| 21 | + setTimeout( |
| 22 | + async () => { |
| 23 | + if (!config.telemetry) { |
| 24 | + return; |
| 25 | + } |
| 26 | + const endpointURL = this.preparePayload(); |
| 27 | + |
| 28 | + try { |
| 29 | + const response = await fetch(endpointURL, { method: "POST" }); |
| 30 | + if (!response.ok) { |
| 31 | + console.warn("The metrics could not be sent to MapTiler Cloud"); |
| 32 | + } |
| 33 | + } catch (e) { |
| 34 | + console.warn("The metrics could not be sent to MapTiler Cloud", e); |
| 35 | + } |
| 36 | + }, |
| 37 | + Math.max(1000, delay), |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Register a module to the telemetry system of the SDK. |
| 43 | + * The arguments `name` and `version` likely come from the package.json |
| 44 | + * of each module. |
| 45 | + */ |
| 46 | + registerModule(name: string, version: string) { |
| 47 | + // The telemetry is using a Set (and not an array) to avoid duplicates |
| 48 | + // of same module + same version. Yet we want to track is the same module |
| 49 | + // is being used with multiple version in a given project as this |
| 50 | + // could be a source of incompatibility |
| 51 | + this.registeredModules.add(`${name}:${version}`); |
| 52 | + } |
| 53 | + |
| 54 | + private preparePayload(): string { |
| 55 | + const telemetryUrl = new URL(defaults.telemetryURL); |
| 56 | + |
| 57 | + // Adding the version of the SDK |
| 58 | + telemetryUrl.searchParams.append("sdk", packagejson.version); |
| 59 | + |
| 60 | + // Adding the API key |
| 61 | + telemetryUrl.searchParams.append("key", config.apiKey); |
| 62 | + |
| 63 | + // Adding MapTiler Cloud session ID |
| 64 | + telemetryUrl.searchParams.append("mtsid", MAPTILER_SESSION_ID); |
| 65 | + |
| 66 | + // Is the app using session? |
| 67 | + telemetryUrl.searchParams.append("session", config.session ? "1" : "0"); |
| 68 | + |
| 69 | + // Is the app using tile caching? |
| 70 | + telemetryUrl.searchParams.append("caching", config.caching ? "1" : "0"); |
| 71 | + |
| 72 | + // Is the langauge updated from the original style? |
| 73 | + telemetryUrl.searchParams.append("lang-updated", this.map.isLanguageUpdated() ? "1" : "0"); |
| 74 | + |
| 75 | + // Is terrain enabled? |
| 76 | + telemetryUrl.searchParams.append("terrain", this.map.getTerrain() ? "1" : "0"); |
| 77 | + |
| 78 | + // Is globe enabled? |
| 79 | + telemetryUrl.searchParams.append("globe", this.map.isGlobeProjection() ? "1" : "0"); |
| 80 | + |
| 81 | + // Adding the modules |
| 82 | + // the list of modules are separated by a "|". For each module, a ":" is used to separate the name and the version: |
| 83 | + // "@maptiler/module-foo:1.1.0|@maptiler/module-bar:3.4.0|@maptiler/module-baz:9.0.3|@maptiler/module-quz:0.0.2-rc.1" |
| 84 | + // then the `.append()` function is in charge of URL-encoding the argument |
| 85 | + if (this.registeredModules.size > 0) { |
| 86 | + telemetryUrl.searchParams.append("modules", Array.from(this.registeredModules).join("|")); |
| 87 | + } |
| 88 | + |
| 89 | + return telemetryUrl.href; |
| 90 | + } |
| 91 | +} |
0 commit comments