namespace pxt { export declare function aiTrackEvent ( id , data ? , measures ? ) ; export declare function aiTrackException ( err , kind , props ) ; export declare function setInteractiveConsent ( enabled ) ; } namespace pxt . analytics { const defaultProps = { } ; const defaultMeasures = { } ; let enabled = false ; export function addDefaultProperties ( props ) { Object . keys ( props ) . forEach ( k => { if ( typeof props [ k ] == "string" ) { defaultProps [ k ] = < string > props [ k ] ; } else { defaultMeasures [ k ] = < number > props [ k ] ; } } ) ; } export function enable ( ) { if ( ! pxt . aiTrackException || ! pxt . aiTrackEvent || enabled ) return ; enabled = true ; pxt . debug ( 'setting up app insights' ) ; const te = pxt . tickEvent ; pxt . tickEvent = function ( id , data ? , opts ? ) : void { if ( te ) te ( id , data , opts ) ; if ( opts ?. interactiveConsent ) pxt . setInteractiveConsent ( true ) ; if ( ! data ) pxt . aiTrackEvent ( id ) ; else { const props = { ... defaultProps } || { } ; const measures = { ... defaultMeasures } || { } ; Object . keys ( data ) . forEach ( k => { if ( typeof data [ k ] == "string" ) props [ k ] = < string > data [ k ] ; else if ( typeof data [ k ] == "number" ) measures [ k ] = < number > data [ k ] ; else props [ k ] = JSON . stringify ( data [ k ] || '' ) ; } ) ; pxt . aiTrackEvent ( id , props , measures ) ; } } ; const rexp = pxt . reportException ; pxt . reportException = function ( err , data ) : void { if ( rexp ) rexp ( err , data ) ; const props = { target : pxt . appTarget . id , version : pxt . appTarget . versions . target } ; if ( data ) Util . jsonMergeFrom ( props , data ) ; pxt . aiTrackException ( err , 'exception' , props ) ; } ; const re = pxt . reportError ; pxt . reportError = function ( cat , msg , data ? ) : void { if ( re ) re ( cat , msg , data ) ; try { throw msg ; } catch ( err ) { const props = { target : pxt . appTarget . id , version : pxt . appTarget . versions . target , category : cat , message : msg } ; if ( data ) Util . jsonMergeFrom ( props , data ) ; pxt . aiTrackException ( err , 'error' , props ) ; } } ; } }