-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.js
More file actions
91 lines (80 loc) · 2.62 KB
/
Copy pathtelemetry.js
File metadata and controls
91 lines (80 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* ============================================================
telemetry.js — Application Insights Telemetry
Non-blocking wrapper: all tracking degrades gracefully
when App Insights is not configured.
============================================================ */
(function () {
'use strict';
// Configuration — connection string from deployment env or empty
const connectionString = (window.APP_INSIGHTS_CONNECTION_STRING || '').trim();
let appInsights = null;
/**
* Initialize Application Insights SDK if a connection string is provided.
* Called once on page load; silently no-ops if SDK or config is missing.
*/
function init() {
if (!connectionString) return;
try {
var sdkRef = window.Microsoft &&
window.Microsoft.ApplicationInsights &&
window.Microsoft.ApplicationInsights.ApplicationInsights;
if (!sdkRef) {
console.warn('[Telemetry] Application Insights SDK not loaded — tracking disabled.');
return;
}
var snippet = new sdkRef({
config: {
connectionString: connectionString,
enableAutoRouteTracking: false, // SPA — we handle page views
disableFetchTracking: false,
enableCorsCorrelation: false,
enableRequestHeaderTracking: false,
enableResponseHeaderTracking: false,
},
});
snippet.loadAppInsights();
snippet.trackPageView(); // initial page view
appInsights = snippet;
} catch (err) {
console.warn('[Telemetry] Failed to initialize Application Insights:', err);
}
}
/**
* Track a named custom event with optional properties.
* Safe to call even when App Insights is not configured.
* @param {string} name — event name, e.g. "LabCardClick"
* @param {Object} [properties] — key/value properties bag
*/
function trackEvent(name, properties) {
if (!appInsights) return;
try {
appInsights.trackEvent({ name: name }, properties || {});
} catch (_) {
// swallow — telemetry must never break the app
}
}
/**
* Track a page view (manual).
* @param {string} [pageName]
*/
function trackPageView(pageName) {
if (!appInsights) return;
try {
appInsights.trackPageView({ name: pageName || document.title });
} catch (_) {
// swallow
}
}
// Expose global telemetry helper
window.telemetry = {
init: init,
trackEvent: trackEvent,
trackPageView: trackPageView,
};
// Auto-init when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();