1+ ( function ( ) {
2+ const connectionString = window . APPLICATIONINSIGHTS_CONNECTION_STRING ;
3+
4+ if ( ! connectionString ) {
5+ return ;
6+ }
7+
8+ const script = document . createElement ( 'script' ) ;
9+ script . src = 'https://js.monitor.azure.com/scripts/b/ai.2.min.js' ;
10+ script . async = true ;
11+
12+ script . onload = function ( ) {
13+ const appInsights = new Microsoft . ApplicationInsights . ApplicationInsights ( {
14+ config : {
15+ connectionString : connectionString ,
16+ enableAutoRouteTracking : false ,
17+ disableExceptionTracking : true ,
18+ disableTelemetry : false ,
19+ samplingPercentage : 100
20+ }
21+ } ) ;
22+
23+ appInsights . loadAppInsights ( ) ;
24+
25+ // Generate anonymous user ID for session tracking
26+ const userId = sessionStorage . getItem ( 'ai_user' ) ||
27+ 'user_' + Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) ;
28+ sessionStorage . setItem ( 'ai_user' , userId ) ;
29+ appInsights . setAuthenticatedUserContext ( userId ) ;
30+
31+ // Collect comprehensive user statistics
32+ const userStats = {
33+ referrer : document . referrer || 'direct' ,
34+ userAgent : navigator . userAgent ,
35+ language : navigator . language ,
36+ languages : navigator . languages ? navigator . languages . join ( ',' ) : '' ,
37+ screenResolution : `${ screen . width } x${ screen . height } ` ,
38+ colorDepth : screen . colorDepth ,
39+ timezone : Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ,
40+ platform : navigator . platform ,
41+ cookieEnabled : navigator . cookieEnabled ,
42+ onLine : navigator . onLine ,
43+ deviceMemory : navigator . deviceMemory || 'unknown' ,
44+ connection : navigator . connection ? {
45+ effectiveType : navigator . connection . effectiveType ,
46+ downlink : navigator . connection . downlink ,
47+ rtt : navigator . connection . rtt
48+ } : 'unknown'
49+ } ;
50+
51+ // Track page view with enhanced user statistics
52+ appInsights . trackPageView ( {
53+ name : 'Tsunami Map' ,
54+ properties : userStats
55+ } ) ;
56+
57+ // Track user location (with permission)
58+ if ( navigator . geolocation && 'geolocation' in navigator ) {
59+ navigator . geolocation . getCurrentPosition (
60+ function ( position ) {
61+ // Track approximate location (country/region level for privacy)
62+ appInsights . trackEvent ( 'UserLocation' , {
63+ latitude : Math . round ( position . coords . latitude * 10 ) / 10 , // Rounded for privacy
64+ longitude : Math . round ( position . coords . longitude * 10 ) / 10 , // Rounded for privacy
65+ accuracy : position . coords . accuracy > 1000 ? 'low' : 'high'
66+ } ) ;
67+ } ,
68+ function ( error ) {
69+ // Track location access denied/failed
70+ appInsights . trackEvent ( 'LocationAccessDenied' , {
71+ error : error . message || 'unknown'
72+ } ) ;
73+ } ,
74+ { enableHighAccuracy : false , timeout : 5000 , maximumAge : 300000 }
75+ ) ;
76+ }
77+
78+ // Track map interactions
79+ window . trackMapInteraction = function ( action , properties ) {
80+ appInsights . trackEvent ( 'MapInteraction' , {
81+ action : action ,
82+ ...properties ,
83+ timestamp : new Date ( ) . toISOString ( )
84+ } ) ;
85+ } ;
86+
87+ // Track session duration
88+ let sessionStart = Date . now ( ) ;
89+ window . addEventListener ( 'beforeunload' , function ( ) {
90+ const sessionDuration = Math . round ( ( Date . now ( ) - sessionStart ) / 1000 ) ;
91+ appInsights . trackEvent ( 'SessionEnd' , {
92+ durationSeconds : sessionDuration ,
93+ userId : userId
94+ } ) ;
95+ } ) ;
96+ } ;
97+
98+ document . head . appendChild ( script ) ;
99+ } ) ( ) ;
0 commit comments