|
| 1 | +import type { |
| 2 | + EventNames, |
| 3 | + Payload, |
| 4 | + TaskParams, |
| 5 | + TelemetryConsentPayload, |
| 6 | +} from "./types.js"; |
| 7 | + |
| 8 | +import os from "node:os"; |
| 9 | + |
| 10 | +import { spawnDetachedSubProcess } from "@ignored/hardhat-vnext-utils/subprocess"; |
| 11 | +import debug from "debug"; |
| 12 | + |
| 13 | +import { getHardhatVersion } from "../../../utils/package.js"; |
| 14 | +import { |
| 15 | + isTelemetryAllowedInEnvironment, |
| 16 | + isTelemetryAllowed, |
| 17 | +} from "../telemetry-permissions.js"; |
| 18 | + |
| 19 | +import { getAnalyticsClientId } from "./utils.js"; |
| 20 | + |
| 21 | +const log = debug("hardhat:cli:telemetry:analytics"); |
| 22 | + |
| 23 | +const SESSION_ID = Math.random().toString(); |
| 24 | +const ENGAGEMENT_TIME_MSEC = "10000"; |
| 25 | + |
| 26 | +// Return a boolean for testing purposes to verify that analytics are not sent in CI environments |
| 27 | +export async function sendTelemetryConsentAnalytics( |
| 28 | + consent: boolean, |
| 29 | +): Promise<boolean> { |
| 30 | + // This is a special scenario where only the consent is sent, all the other analytics info |
| 31 | + // (like node version, hardhat version, etc.) are stripped. |
| 32 | + |
| 33 | + if (!isTelemetryAllowedInEnvironment()) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + const payload: TelemetryConsentPayload = { |
| 38 | + client_id: "hardhat_telemetry_consent", |
| 39 | + user_id: "hardhat_telemetry_consent", |
| 40 | + user_properties: {}, |
| 41 | + events: [ |
| 42 | + { |
| 43 | + name: "TelemetryConsentResponse", |
| 44 | + params: { |
| 45 | + userConsent: consent ? "yes" : "no", |
| 46 | + }, |
| 47 | + }, |
| 48 | + ], |
| 49 | + }; |
| 50 | + |
| 51 | + await createSubprocessToSendAnalytics(payload); |
| 52 | + |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +export async function sendTaskAnalytics(taskId: string[]): Promise<boolean> { |
| 57 | + const eventParams: TaskParams = { |
| 58 | + task: taskId.join(", "), |
| 59 | + }; |
| 60 | + |
| 61 | + return sendAnalytics("task", eventParams); |
| 62 | +} |
| 63 | + |
| 64 | +// Return a boolean for testing purposes to confirm whether analytics were sent based on the consent value and not in CI environments |
| 65 | +async function sendAnalytics( |
| 66 | + eventName: EventNames, |
| 67 | + eventParams: TaskParams, |
| 68 | +): Promise<boolean> { |
| 69 | + if (!(await isTelemetryAllowed())) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + const payload = await buildPayload(eventName, eventParams); |
| 74 | + |
| 75 | + await createSubprocessToSendAnalytics(payload); |
| 76 | + |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +async function createSubprocessToSendAnalytics( |
| 81 | + payload: TelemetryConsentPayload | Payload, |
| 82 | +): Promise<void> { |
| 83 | + log( |
| 84 | + `Sending analytics for '${payload.events[0].name}'. Payload: ${JSON.stringify(payload)}`, |
| 85 | + ); |
| 86 | + |
| 87 | + // The HARDHAT_TEST_SUBPROCESS_RESULT_PATH env variable is used in the tests to instruct the subprocess to write the payload to a file |
| 88 | + // instead of sending it. |
| 89 | + // During testing, the subprocess file is a ts file, whereas in production, it is a js file (compiled code). |
| 90 | + // The following lines adjust the file extension based on whether the environment is for testing or production. |
| 91 | + const fileExt = |
| 92 | + process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH !== undefined ? "ts" : "js"; |
| 93 | + const subprocessFile = `${import.meta.dirname}/subprocess.${fileExt}`; |
| 94 | + |
| 95 | + const env: Record<string, string> = {}; |
| 96 | + if (process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH !== undefined) { |
| 97 | + // ATTENTION: only for testing |
| 98 | + env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH = |
| 99 | + process.env.HARDHAT_TEST_SUBPROCESS_RESULT_PATH; |
| 100 | + } |
| 101 | + |
| 102 | + await spawnDetachedSubProcess(subprocessFile, [JSON.stringify(payload)], env); |
| 103 | + |
| 104 | + log("Payload sent to detached subprocess"); |
| 105 | +} |
| 106 | + |
| 107 | +async function buildPayload( |
| 108 | + eventName: EventNames, |
| 109 | + eventParams: TaskParams, |
| 110 | +): Promise<Payload> { |
| 111 | + const clientId = await getAnalyticsClientId(); |
| 112 | + |
| 113 | + return { |
| 114 | + client_id: clientId, |
| 115 | + user_id: clientId, |
| 116 | + user_properties: { |
| 117 | + projectId: { value: "hardhat-project" }, |
| 118 | + hardhatVersion: { value: await getHardhatVersion() }, |
| 119 | + operatingSystem: { value: os.platform() }, |
| 120 | + nodeVersion: { value: process.version }, |
| 121 | + }, |
| 122 | + events: [ |
| 123 | + { |
| 124 | + name: eventName, |
| 125 | + params: { |
| 126 | + engagement_time_msec: ENGAGEMENT_TIME_MSEC, |
| 127 | + session_id: SESSION_ID, |
| 128 | + ...eventParams, |
| 129 | + }, |
| 130 | + }, |
| 131 | + ], |
| 132 | + }; |
| 133 | +} |
0 commit comments