|
| 1 | +/** |
| 2 | + * Integration with @github/hydro-analytics-client for cross-subdomain tracking. |
| 3 | + * |
| 4 | + * This sends events to collector.githubapp.com alongside our existing analytics. |
| 5 | + * The client auto-collects: page, title, client_id, referrer, user_agent, |
| 6 | + * screen_resolution, browser_resolution, browser_languages, pixel_ratio, timestamp, tz_seconds |
| 7 | + * |
| 8 | + * We send all other docs-specific context fields, including: |
| 9 | + * - path_language, path_version, path_product, path_article |
| 10 | + * - page_document_type, page_type, content_type |
| 11 | + * - color_mode_preference, is_logged_in, experiment_variation, is_headless |
| 12 | + * - event_id, page_event_id, octo_client_id |
| 13 | + * - Plus any event-specific properties (exit metrics, link_url, etc.) |
| 14 | + * |
| 15 | + * All functions are wrapped in try/catch to ensure that issues with the |
| 16 | + * hydro-analytics-client or collector don't affect our primary analytics. |
| 17 | + */ |
| 18 | + |
| 19 | +import { |
| 20 | + AnalyticsClient, |
| 21 | + getOrCreateClientId as hydroGetOrCreateClientId, |
| 22 | +} from '@github/hydro-analytics-client' |
| 23 | +import { EventType } from '../types' |
| 24 | + |
| 25 | +/** |
| 26 | + * Safe wrapper around hydro-analytics-client's getOrCreateClientId. |
| 27 | + * Returns undefined if the client fails for any reason. |
| 28 | + */ |
| 29 | +export function getOctoClientId(): string | undefined { |
| 30 | + try { |
| 31 | + return hydroGetOrCreateClientId() |
| 32 | + } catch (error) { |
| 33 | + console.log('hydro-analytics-client getOctoClientId error:', error) |
| 34 | + return undefined |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +const hydroClient = new AnalyticsClient({ |
| 39 | + collectorUrl: 'https://collector.githubapp.com/docs/collect', |
| 40 | + clientId: getOctoClientId(), |
| 41 | +}) |
| 42 | + |
| 43 | +// Fields that hydro-analytics-client already collects automatically |
| 44 | +const AUTO_COLLECTED_FIELDS = new Set([ |
| 45 | + 'referrer', |
| 46 | + 'user_agent', |
| 47 | + 'viewport_width', |
| 48 | + 'viewport_height', |
| 49 | + 'screen_width', |
| 50 | + 'screen_height', |
| 51 | + 'pixel_ratio', |
| 52 | + 'timezone', |
| 53 | + 'user_language', |
| 54 | + 'href', |
| 55 | + 'title', |
| 56 | +]) |
| 57 | + |
| 58 | +/** |
| 59 | + * Flatten a nested event body into a single-level context object, |
| 60 | + * excluding fields that hydro-analytics-client already auto-collects. |
| 61 | + */ |
| 62 | +export function prepareData(body: Record<string, unknown>): { |
| 63 | + type: string |
| 64 | + context: Record<string, string> |
| 65 | +} { |
| 66 | + const { context: nestedContext, type, ...rest } = body |
| 67 | + const flattened = { |
| 68 | + ...((nestedContext as Record<string, unknown>) || {}), |
| 69 | + ...rest, |
| 70 | + } |
| 71 | + const context = Object.fromEntries( |
| 72 | + Object.entries(flattened) |
| 73 | + .filter(([, value]) => value != null) |
| 74 | + .filter(([key]) => !AUTO_COLLECTED_FIELDS.has(key)) |
| 75 | + .map(([key, value]) => [key, String(value)]), |
| 76 | + ) |
| 77 | + return { type: typeof type === 'string' ? type : 'unknown', context } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Send an event to hydro-analytics-client. |
| 82 | + * For page events, sends as a page view. For all other events, sends as a custom event. |
| 83 | + * |
| 84 | + * This is wrapped in try/catch to ensure that if the hydro collector is down |
| 85 | + * or errors, it doesn't affect our primary analytics pipeline. |
| 86 | + */ |
| 87 | +export function sendHydroAnalyticsEvent(body: Record<string, unknown>): void { |
| 88 | + try { |
| 89 | + const { type, context } = prepareData(body) |
| 90 | + if (type === EventType.page) { |
| 91 | + hydroClient.sendPageView(context) |
| 92 | + } else { |
| 93 | + hydroClient.sendEvent(type, context) |
| 94 | + } |
| 95 | + } catch (error) { |
| 96 | + console.log('hydro-analytics-client error:', error) |
| 97 | + } |
| 98 | +} |
0 commit comments