|
| 1 | +/* istanbul ignore file: external system */ |
| 2 | +import * as os from "os"; |
| 3 | +import got from "got"; |
| 4 | +import * as qs from "qs"; |
| 5 | +import { isTelemetryEnabled } from "@utils/serverStateUtils"; |
| 6 | +import { ServerState } from "../types"; |
| 7 | +import { |
| 8 | + Analytics, |
| 9 | + AnalyticsPayload, |
| 10 | + DefaultRawAnalyticsPayload, |
| 11 | + RawAnalyticsPayload, |
| 12 | +} from "./types"; |
| 13 | + |
| 14 | +const GOOGLE_ANALYTICS_URL = "https://www.google-analytics.com/collect"; |
| 15 | + |
| 16 | +export class GoogleAnalytics implements Analytics { |
| 17 | + private readonly googleTrackingId: string; |
| 18 | + private serverState: ServerState | null; |
| 19 | + private machineId: string | undefined; |
| 20 | + private extensionVersion: string | undefined; |
| 21 | + |
| 22 | + constructor(googleTrackingId: string) { |
| 23 | + this.googleTrackingId = googleTrackingId; |
| 24 | + |
| 25 | + this.machineId = undefined; |
| 26 | + this.extensionVersion = undefined; |
| 27 | + this.serverState = null; |
| 28 | + } |
| 29 | + |
| 30 | + public init( |
| 31 | + machineId: string | undefined, |
| 32 | + extensionVersion: string | undefined, |
| 33 | + serverState: ServerState |
| 34 | + ): void { |
| 35 | + this.machineId = machineId; |
| 36 | + this.extensionVersion = extensionVersion; |
| 37 | + this.serverState = serverState; |
| 38 | + } |
| 39 | + |
| 40 | + public async sendPageView( |
| 41 | + taskName: string, |
| 42 | + more?: RawAnalyticsPayload |
| 43 | + ): Promise<void> { |
| 44 | + try { |
| 45 | + if ( |
| 46 | + this.serverState?.env !== "production" || |
| 47 | + !isTelemetryEnabled(this.serverState) || |
| 48 | + this.machineId === undefined |
| 49 | + ) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const payload = this._buildPayloadFrom(taskName, this.machineId, more); |
| 54 | + |
| 55 | + await this._sendHit(payload); |
| 56 | + } catch { |
| 57 | + // continue on failed analytics send |
| 58 | + return; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private _buildPayloadFrom( |
| 63 | + taskName: string, |
| 64 | + machineId: string, |
| 65 | + more?: RawAnalyticsPayload |
| 66 | + ): AnalyticsPayload { |
| 67 | + const defaultAnalytics: DefaultRawAnalyticsPayload = { |
| 68 | + // Measurement protocol version. |
| 69 | + v: "1", |
| 70 | + |
| 71 | + // Tracking Id. |
| 72 | + tid: this.googleTrackingId, |
| 73 | + |
| 74 | + // User agent, must be present. |
| 75 | + // We use it to inform Node version used and OS. |
| 76 | + // Example: |
| 77 | + // Node/v8.12.0 (Darwin 17.7.0) |
| 78 | + ua: this._getUserAgent(), |
| 79 | + |
| 80 | + // Client Id. |
| 81 | + cid: machineId, |
| 82 | + |
| 83 | + // Hit type, we're only using timing for now. |
| 84 | + t: "pageview", |
| 85 | + |
| 86 | + // Document page |
| 87 | + dp: `/${taskName}`, |
| 88 | + |
| 89 | + // Custom dimension 1: extension version |
| 90 | + // Example: 'v1.0.0'. |
| 91 | + cd1: `v${this.extensionVersion}`, |
| 92 | + }; |
| 93 | + |
| 94 | + return { ...defaultAnalytics, ...(more || {}) }; |
| 95 | + } |
| 96 | + |
| 97 | + private _sendHit(hit: AnalyticsPayload) { |
| 98 | + const hitPayload = qs.stringify(hit); |
| 99 | + |
| 100 | + return got.post(GOOGLE_ANALYTICS_URL, { |
| 101 | + headers: { |
| 102 | + Accept: "*/*", |
| 103 | + "Accept-Language": "en-US,en;q=0.5", |
| 104 | + "Content-Type": "text/plain;charset=UTF-8", |
| 105 | + }, |
| 106 | + body: hitPayload, |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + private _getUserAgent(): string { |
| 111 | + return `Node/${process.version} ${this._getOperatingSystem()}`; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * At the moment, we couldn't find a reliably way to report the OS () in Node, |
| 116 | + * as the versions reported by the various `os` APIs (`os.platform()`, `os.type()`, etc) |
| 117 | + * return values different to those expected by Google Analytics |
| 118 | + * We decided to take the compromise of just reporting the OS Platform (OSX/Linux/Windows) for now (version information is bogus for now). |
| 119 | + */ |
| 120 | + private _getOperatingSystem(): string { |
| 121 | + switch (os.type()) { |
| 122 | + case "Windows_NT": |
| 123 | + return "(Windows NT 6.1; Win64; x64)"; |
| 124 | + case "Darwin": |
| 125 | + return "(Macintosh; Intel Mac OS X 10_13_6)"; |
| 126 | + case "Linux": |
| 127 | + return "(X11; Linux x86_64)"; |
| 128 | + default: |
| 129 | + return "(Unknown)"; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments