GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.7 KiB

2 months ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.emulatorSession = exports.trackEmulator = exports.track = exports.usageEnabled = exports.EMULATOR_GA4_MEASUREMENT_ID = void 0;
  4. const node_fetch_1 = require("node-fetch");
  5. const ua = require("universal-analytics");
  6. const uuid_1 = require("uuid");
  7. const auth_1 = require("./auth");
  8. const configstore_1 = require("./configstore");
  9. const logger_1 = require("./logger");
  10. const pkg = require("../package.json");
  11. exports.EMULATOR_GA4_MEASUREMENT_ID = process.env.FIREBASE_EMULATOR_GA4_MEASUREMENT_ID || "G-KYP2JMPFC0";
  12. function usageEnabled() {
  13. return !!configstore_1.configstore.get("usage");
  14. }
  15. exports.usageEnabled = usageEnabled;
  16. const FIREBASE_ANALYTICS_UA = process.env.FIREBASE_ANALYTICS_UA || "UA-29174744-3";
  17. let anonId = configstore_1.configstore.get("analytics-uuid");
  18. if (!anonId) {
  19. anonId = (0, uuid_1.v4)();
  20. configstore_1.configstore.set("analytics-uuid", anonId);
  21. }
  22. const visitor = ua(FIREBASE_ANALYTICS_UA, anonId, {
  23. strictCidFormat: false,
  24. https: true,
  25. });
  26. visitor.set("cd1", process.platform);
  27. visitor.set("cd2", process.version);
  28. visitor.set("cd3", process.env.FIREPIT_VERSION || "none");
  29. function track(action, label, duration = 0) {
  30. return new Promise((resolve) => {
  31. if (configstore_1.configstore.get("tokens") && usageEnabled()) {
  32. visitor.event("Firebase CLI " + pkg.version, action, label, duration).send(() => {
  33. resolve();
  34. });
  35. }
  36. else {
  37. resolve();
  38. }
  39. });
  40. }
  41. exports.track = track;
  42. const EMULATOR_GA4_API_SECRET = process.env.FIREBASE_EMULATOR_GA4_API_SECRET || "2V_zBYc4TdeoppzDaIu0zw";
  43. const EMULATOR_GA4_USER_PROPS = {
  44. node_platform: {
  45. value: process.platform,
  46. },
  47. node_version: {
  48. value: process.version,
  49. },
  50. cli_version: {
  51. value: pkg.version,
  52. },
  53. firepit_version: {
  54. value: process.env.FIREPIT_VERSION || "none",
  55. },
  56. };
  57. async function trackEmulator(eventName, params) {
  58. const session = emulatorSession();
  59. if (!session) {
  60. return;
  61. }
  62. const oldTotalEngagementSeconds = session.totalEngagementSeconds;
  63. session.totalEngagementSeconds = process.uptime();
  64. session.commandName = (params === null || params === void 0 ? void 0 : params.command_name) || session.commandName;
  65. const search = `?api_secret=${EMULATOR_GA4_API_SECRET}&measurement_id=${session.measurementId}`;
  66. const validate = session.validateOnly ? "debug/" : "";
  67. const url = `https://www.google-analytics.com/${validate}mp/collect${search}`;
  68. const body = {
  69. timestamp_micros: `${Date.now()}000`,
  70. client_id: session.clientId,
  71. user_properties: Object.assign(Object.assign({}, EMULATOR_GA4_USER_PROPS), { java_major_version: session.javaMajorVersion
  72. ? { value: session.javaMajorVersion }
  73. : undefined }),
  74. validationBehavior: session.validateOnly ? "ENFORCE_RECOMMENDATIONS" : undefined,
  75. events: [
  76. {
  77. name: eventName,
  78. params: Object.assign({ session_id: session.sessionId, engagement_time_msec: (session.totalEngagementSeconds - oldTotalEngagementSeconds)
  79. .toFixed(3)
  80. .replace(".", "")
  81. .replace(/^0+/, ""), debug_mode: session.debugMode ? true : undefined, command_name: session.commandName }, params),
  82. },
  83. ],
  84. };
  85. if (session.validateOnly) {
  86. logger_1.logger.info(`Sending Analytics for event ${eventName}`, params, body);
  87. }
  88. try {
  89. const response = await (0, node_fetch_1.default)(url, {
  90. method: "POST",
  91. headers: {
  92. "content-type": "application/json;charset=UTF-8",
  93. },
  94. body: JSON.stringify(body),
  95. });
  96. if (session.validateOnly) {
  97. if (!response.ok) {
  98. logger_1.logger.warn(`Analytics validation HTTP error: ${response.status}`);
  99. }
  100. const respBody = await response.text();
  101. logger_1.logger.info(`Analytics validation result: ${respBody}`);
  102. }
  103. }
  104. catch (e) {
  105. if (session.validateOnly) {
  106. throw e;
  107. }
  108. return;
  109. }
  110. }
  111. exports.trackEmulator = trackEmulator;
  112. function emulatorSession() {
  113. const validateOnly = !!process.env.FIREBASE_CLI_MP_VALIDATE;
  114. if (!usageEnabled()) {
  115. if (validateOnly) {
  116. logger_1.logger.warn("Google Analytics is DISABLED. To enable, (re)login and opt in to collection.");
  117. }
  118. return;
  119. }
  120. if (!currentEmulatorSession) {
  121. let clientId = configstore_1.configstore.get("emulator-analytics-clientId");
  122. if (!clientId) {
  123. clientId = (0, uuid_1.v4)();
  124. configstore_1.configstore.set("emulator-analytics-clientId", clientId);
  125. }
  126. currentEmulatorSession = {
  127. measurementId: exports.EMULATOR_GA4_MEASUREMENT_ID,
  128. clientId,
  129. sessionId: (Math.random() * Number.MAX_SAFE_INTEGER).toFixed(0),
  130. totalEngagementSeconds: 0,
  131. debugMode: isDebugMode(),
  132. validateOnly,
  133. };
  134. }
  135. return currentEmulatorSession;
  136. }
  137. exports.emulatorSession = emulatorSession;
  138. let currentEmulatorSession = undefined;
  139. function isDebugMode() {
  140. const account = (0, auth_1.getGlobalDefaultAccount)();
  141. if (account === null || account === void 0 ? void 0 : account.user.email.endsWith("@google.com")) {
  142. try {
  143. require("../tsconfig.json");
  144. logger_1.logger.info(`Using Google Analytics in DEBUG mode. Emulators (+ UI) events will be shown in GA Debug View only.`);
  145. return true;
  146. }
  147. catch (_a) {
  148. }
  149. }
  150. return false;
  151. }