Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 12x 12x 13x 13x 13x 13x 13x 13x 1x 2x 2x 2x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 3x 3x 3x 3x 3x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 5x 5x 4x 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 13x 14x 14x 14x 14x 13x 5x 14x 1x 15x 15x 1x 10x 10x 10x 10x 10x 10x 10x 10x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 7x 7x 7x 1x 4x 4x 3x 3x 3x 2x 2x 2x 2x 2x 3x 1x 1x | /* * Analytics.ts * @author Abhilash Panwar (abpanwar) * @copyright Microsoft 2020 * File containing Analytics object. */ import { AppInsightsCore, CoreUtils, IExtendedTelemetryItem, Utils, isDocumentObjectAvailable, getDocument, IExtendedConfiguration } from '@microsoft/1ds-core-js'; import { PostChannel, RT_PROFILE, IChannelConfiguration } from '@microsoft/1ds-post-js'; import { IEvent, IView, IAnalyticsConfiguration, IAction, IUser } from './analytics/DataModels'; import { IScreenResolution } from './analytics/InternalDataModels'; import { _Extensions, _SignalExtKeys, _PageViewType, _SharedKeys, _ViewExtKeys, _ReferrerExtKeys, _EIPreviousKey, _ViewEventName, _ViewSchemaVersion, _ActionEventName, _ActionSchemaVersion, _ActionExtKeys, _ClickExtKeys, _emptyString, _UserMappingEventName, _UserMappingEventVersion, _UserMappingStorageKey, _DayToMilliseconds } from './properties/Constants'; import Properties from './properties/Properties'; import Autocollector from './analytics/Autocollector'; import StorageManager from './properties/StorageManager'; import SessionManager from './properties/SessionManager'; import { _getPathNameFromUri, _hashCode } from './analytics/AnalyticsHelper'; const d = decodeURI; // for code minimization purpose /** * Engagement Insights class that is used to send events. */ export default class Analytics { private _applicationInsightsCore: AppInsightsCore; private _postChannel: PostChannel; private _properties: Properties = new Properties(); private _configuration: IAnalyticsConfiguration; private _isInitialized = false; private _lastView: IView; private _lastViewTime: Date; private _previousView: string; private _storageDisabled: boolean; private _storageManager: StorageManager; private _sessionManager: SessionManager; private _autoCollector = new Autocollector(); private _invalidSignalPrefix = _Extensions._SignalExt + '.'; private _invalidSignalLongPrefix = _Extensions._SignalLongExt + '.' private _user: IUser; private _setUserBeforeInitialize: boolean; /** * Initializes the Analytics class according to the passed configuration. It will send autocollected events if the passed * configuration specifies it. * @param configuration the configuration used to initialize the Analytics class */ initialize(configuration: IAnalyticsConfiguration) { Eif (!this._isInitialized) { this._isInitialized = true; this._configuration = configuration; this._applicationInsightsCore = new AppInsightsCore(); this._postChannel = new PostChannel(); let profile = {}; profile[RT_PROFILE] = [2, 0]; this._postChannel._loadTransmitProfiles(profile); this._storageDisabled = Utils.isValueAssigned(configuration.userConsent) ? !configuration.userConsent : false; this._storageDisabled = StorageManager._getLocalStorage() ? this._storageDisabled : true; this._storageManager = new StorageManager(this._storageDisabled); //Only enable session if the user has given consent. if (!this._storageDisabled) { this._sessionManager = new SessionManager(this._storageManager); this._properties.setup(this._sessionManager); } //Initialize the core let coreConfig: IExtendedConfiguration = { instrumentationKey: configuration.ingestionKey, endpointUrl: configuration.endpointUrl, disableCookiesUsage: this._storageDisabled, extensions: [this._properties, this._postChannel], extensionConfig: [] }; let postConfig: IChannelConfiguration = { ignoreMc1Ms0CookieProcessing: true }; coreConfig.extensionConfig[this._postChannel.identifier] = postConfig; this._applicationInsightsCore.initialize(coreConfig, []); //Pass autocapture config to Autocollector this._autoCollector._autocollect(this, configuration.autoCapture); // send stored usermapping if setUser is called before initialize Iif (this._setUserBeforeInitialize && this._configuration.userMapping) { this._updateUserMapping(this._user); } } } /** * Tracks a custom event to be sent to Engagement Insights. * @param event event to be sent */ trackEvent(event: IEvent) { event.properties = event.properties || {}; this._sanitizeProperties(event.properties); this._trackEvent(event); } /** * Tracks a view event to be sent to Engagement Insights. * @param view view event to be sent */ trackView(view: IView) { Eif (!this._storageDisabled) { Iif (!view || !Utils.isObject(view) || !Utils.isString(view.uri) || view.uri.length === 0) { throw 'View event should have uri'; } this._lastViewTime = view.timestamp || new Date(); let event: IEvent = { name: _ViewEventName, version: _ViewSchemaVersion, timestamp: this._lastViewTime }; let props = view.properties || {}; this._sanitizeProperties(props); // There should only be a previous view loaded if the session is ongoing. Eif (this._sessionManager._isSessionActive(this._lastViewTime.getTime()), true) { this._previousView = this._storageManager._getProperty(_EIPreviousKey); } this._addAutocollectableFieldsToView(view); this._addViewInfo(view, props); this._addScreenInfo(props); this._lastView = view; event.properties = props; this._trackEvent(event); this._storageManager._setProperty(_EIPreviousKey, view.uri); } } /** * Tracks an action event to be sent to Engagement Insights. * @param action action event to be sent * @param properties custom properties that are part of this event as defined by the emitter. */ trackAction(action: IAction, properties?: { [key: string]: string | number | boolean }) { Eif (!this._storageDisabled) { Iif (!this._lastView) { throw 'Views should be instrumented before actions are sent'; } Iif (!Utils.isValueAssigned(action.type)) { throw 'Action event should have type'; } let actionTime = action.timestamp || new Date(); // If this is the action event that starts a session then send a view event from the last view before the action is sent. if (!this._sessionManager._isSessionActive(actionTime.getTime())) { this._lastView.timestamp = actionTime; this.trackView(this._lastView); } let event: IEvent = { name: _ActionEventName, version: _ActionSchemaVersion, timestamp: actionTime }; let props = action.properties || {}; this._sanitizeProperties(props); this._addViewInfo(this._lastView, props); this._addScreenInfo(props); //Add action properties props[_Extensions._SignalExt][_SignalExtKeys._Action] = {}; props[_Extensions._SignalExt][_SignalExtKeys._Action][_SharedKeys._NameKey] = action.name; props[_Extensions._SignalExt][_SignalExtKeys._Action][_SharedKeys._Type] = action.type; props[_Extensions._SignalExt][_SignalExtKeys._Action][_ActionExtKeys._ElementId] = action.elementId; props[_Extensions._SignalExt][_SignalExtKeys._Action][_SharedKeys._Class] = action.class; props[_Extensions._SignalExt][_SignalExtKeys._Action][_ActionExtKeys._Target] = d(action.viewTarget); //Elapsed time props[_Extensions._SignalExt][_SignalExtKeys._Action][_ActionExtKeys._ElapsedTime] = actionTime.getTime() - this._lastViewTime.getTime(); //Outbound. Only check if there is a view target and we have an origin for the webpage. Eif (action.viewTarget && this._properties.origin) { props[_Extensions._SignalExt][_SignalExtKeys._Action][_ActionExtKeys._IsOutbound] = !action.viewTarget.startsWith(this._properties.origin); } props[_Extensions._SignalExt][_SignalExtKeys._Click] = {}; props[_Extensions._SignalExt][_SignalExtKeys._Click][_ClickExtKeys._Coordinates] = action.clickCoordinates; event.properties = props; this._trackEvent(event); } } /** * Teardown the SDK, and flushes all events currenlty in the queue. * After this method the SDK can be re-initialized with a new configuration. */ teardown() { Eif (this._isInitialized) { this._postChannel.teardown(); this._isInitialized = false; //Reset properties this._properties = new Properties(); this._autoCollector._removeAllListeners(); } } /** * Set custom property to be sent with every event. * @param name name of property * @param value value for the property */ setProperty(name: string, value: string | number | boolean | Date) { if (this._isValidKey(name)) { this._properties.setProperty(name, value); } } /** * Set user details to be sent with every event. Update UserMapping depending on whether the SDK is initialized or not * @param user user details */ setUser(user: IUser) { this._properties.setUser(user); if (this._isInitialized && this._configuration.userMapping) { this._updateUserMapping(user); } else { this._user = user; this._setUserBeforeInitialize = true; } } private _trackEvent(event: IEvent) { Eif (this._isInitialized) { let item: IExtendedTelemetryItem = { name: event.name, iKey: event.ingestionKey || this._configuration.ingestionKey }; let props: { [key: string]: string | number | boolean | string[] | number[] | boolean[] | object } = event.properties; let time: Date = event.timestamp ? event.timestamp : new Date(); item.time = CoreUtils.toISOString(time); props[_Extensions._SignalExt][_SignalExtKeys._ExtKey] = {}; props[_Extensions._SignalExt][_SignalExtKeys._ExtKey][_SharedKeys._Id] = Utils.createGuid(); if (!CoreUtils.isNullOrUndefined(event.version)) { props[_Extensions._SignalExt][_SignalExtKeys._ExtKey][_SharedKeys._Version] = event.version; } item.data = props; this._applicationInsightsCore.track(item); } } private _sanitizeProperties(props: { [key: string]: string | number | boolean | string[] | number[] | boolean[] | object }) { // Don't allow customer to send any fields in the event extension. It is reserved for EI. Utils.arrForEach(Utils.objKeys(props), (key) => { if (!this._isValidKey(key)) { delete props[key]; } }); props[_Extensions._SignalExt] = {}; } private _isValidKey(key: string): boolean { key = key.toLowerCase(); return !(key.length < 3 || key.startsWith(this._invalidSignalPrefix) || key.startsWith(this._invalidSignalLongPrefix) || key === _Extensions._SignalLongExt); } private _addScreenInfo(props: { [key: string]: string | number | boolean | string[] | number[] | boolean[] | object }) { // Get screen resolution let screen: IScreenResolution = this._autoCollector._getScreenResolution(); Eif (screen) { props[_Extensions._SignalExt][_SignalExtKeys._View][_ViewExtKeys._Height] = screen.h; props[_Extensions._SignalExt][_SignalExtKeys._View][_ViewExtKeys._Width] = screen.w; props[_Extensions._SignalExt][_SignalExtKeys._View][_ViewExtKeys._VpHeight] = screen.vH; props[_Extensions._SignalExt][_SignalExtKeys._View][_ViewExtKeys._VpWidth] = screen.vW; props[_Extensions._SignalExt][_SignalExtKeys._View][_ViewExtKeys._ColorDepth] = screen.cd; props[_Extensions._SignalExt][_SignalExtKeys._View][_ViewExtKeys._SResolution] = screen.sR; } } private _addViewInfo(view: IView, props: { [key: string]: string | number | boolean | string[] | number[] | boolean[] | object }) { props[_Extensions._SignalExt][_SignalExtKeys._View] = {}; props[_Extensions._SignalExt][_SignalExtKeys._View][_SharedKeys._Title] = view.title; props[_Extensions._SignalExt][_SignalExtKeys._View][_SharedKeys._Type] = _PageViewType; props[_Extensions._SignalExt][_SignalExtKeys._View][_SharedKeys._Uri] = d(view.uri); props[_Extensions._SignalExt][_SignalExtKeys._View][_SharedKeys._NameKey] = d(view.name); props[_Extensions._SignalExt][_SignalExtKeys._View][_SharedKeys._Class] = view.viewClass; props[_Extensions._SignalExt][_SignalExtKeys._View][_ViewExtKeys._PreviousViews] = this._previousView; Eif (view.referrer && Utils.isObject(view.referrer)) { props[_Extensions._SignalExt][_SignalExtKeys._Referrer] = {}; props[_Extensions._SignalExt][_SignalExtKeys._Referrer][_ReferrerExtKeys._PageTitle] = view.referrer.pageTitle; props[_Extensions._SignalExt][_SignalExtKeys._Referrer][_SharedKeys._Uri] = d(view.referrer.uri); } } private _addAutocollectableFieldsToView(view: IView) { view.name = view.name || this._autoCollector._getPageNameFromPath(_getPathNameFromUri(view.uri)); Eif ( isDocumentObjectAvailable) { view.referrer = view.referrer || this._autoCollector._getReferrer(getDocument()); } } private _updateUserMapping(user: IUser) { let time = CoreUtils.dateNow(); if (user.localId && user.authId) { let usermappingKey = _hashCode(user.localId + ',' + user.authId).toString(); let storedMapping = JSON.parse(this._storageManager._getProperty(_UserMappingStorageKey)) || {}; if (!CoreUtils.hasOwnProperty(storedMapping, usermappingKey) || time - parseInt(storedMapping[usermappingKey]) > 7 * _DayToMilliseconds) { storedMapping[usermappingKey] = time.toString(); this._storageManager._setProperty(_UserMappingStorageKey, JSON.stringify(storedMapping)); let event: IEvent = { name: _UserMappingEventName, version: _UserMappingEventVersion, properties: {} }; this._sanitizeProperties(event.properties); this._trackEvent(event); } Iif (Utils.objKeys(storedMapping).length > 5) { this._deleteOldestUserMapping(storedMapping); } } } private _deleteOldestUserMapping(storedMapping: { [key: string]: string}) { let oldestMappingKey = ''; let oldestTimestamp = Number.MAX_VALUE; Utils.arrForEach(Utils.objKeys(storedMapping), (key)=>{ if (parseInt(storedMapping[key]) < oldestTimestamp) { oldestMappingKey = key; oldestTimestamp = parseInt(storedMapping[key]); } }); delete storedMapping[oldestMappingKey]; this._storageManager._setProperty(_UserMappingStorageKey, JSON.stringify(storedMapping)); } } |