@microsoft/applicationinsights-properties-js 3.0.0-beta.2209-03 → 3.0.0-beta.2210-02
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser/applicationinsights-properties-js.integrity.json +9 -9
- package/browser/applicationinsights-properties-js.js +938 -495
- package/browser/applicationinsights-properties-js.js.map +1 -1
- package/browser/applicationinsights-properties-js.min.js +2 -2
- package/browser/applicationinsights-properties-js.min.js.map +1 -1
- package/dist/applicationinsights-properties-js.api.json +22 -170
- package/dist/applicationinsights-properties-js.api.md +9 -20
- package/dist/applicationinsights-properties-js.d.ts +28 -33
- package/dist/applicationinsights-properties-js.js +938 -495
- package/dist/applicationinsights-properties-js.js.map +1 -1
- package/dist/applicationinsights-properties-js.min.js +2 -2
- package/dist/applicationinsights-properties-js.min.js.map +1 -1
- package/dist/applicationinsights-properties-js.rollup.d.ts +28 -33
- package/dist-esm/Context/Application.js +1 -1
- package/dist-esm/Context/Device.js +1 -1
- package/dist-esm/Context/Internal.js +3 -3
- package/dist-esm/Context/Internal.js.map +1 -1
- package/dist-esm/Context/Location.js +1 -1
- package/dist-esm/Context/Session.js +30 -35
- package/dist-esm/Context/Session.js.map +1 -1
- package/dist-esm/Context/TelemetryTrace.js +1 -1
- package/dist-esm/Context/User.js +54 -46
- package/dist-esm/Context/User.js.map +1 -1
- package/dist-esm/Interfaces/IPropTelemetryContext.js +1 -1
- package/dist-esm/Interfaces/{ITelemetryConfig.js → IPropertiesConfig.js} +2 -2
- package/dist-esm/Interfaces/IPropertiesConfig.js.map +1 -0
- package/dist-esm/PropertiesPlugin.js +57 -50
- package/dist-esm/PropertiesPlugin.js.map +1 -1
- package/dist-esm/TelemetryContext.js +7 -5
- package/dist-esm/TelemetryContext.js.map +1 -1
- package/dist-esm/__DynamicConstants.js +8 -13
- package/dist-esm/__DynamicConstants.js.map +1 -1
- package/dist-esm/applicationinsights-properties-js.js +1 -1
- package/package.json +4 -4
- package/src/Context/Internal.ts +10 -9
- package/src/Context/Session.ts +33 -45
- package/src/Context/User.ts +67 -56
- package/src/Interfaces/IPropertiesConfig.ts +20 -0
- package/src/PropertiesPlugin.ts +58 -54
- package/src/TelemetryContext.ts +8 -4
- package/src/__DynamicConstants.ts +7 -12
- package/types/Context/Internal.d.ts +2 -2
- package/types/Context/Session.d.ts +7 -11
- package/types/Context/User.d.ts +5 -5
- package/types/Interfaces/IPropertiesConfig.d.ts +17 -0
- package/types/PropertiesPlugin.d.ts +1 -3
- package/types/TelemetryContext.d.ts +2 -2
- package/types/__DynamicConstants.d.ts +1 -6
- package/types/tsdoc-metadata.json +1 -1
- package/dist-esm/Interfaces/ITelemetryConfig.js.map +0 -1
- package/src/Interfaces/ITelemetryConfig.ts +0 -20
- package/types/Interfaces/ITelemetryConfig.d.ts +0 -17
package/src/Context/Session.ts
CHANGED
|
@@ -8,20 +8,23 @@ import {
|
|
|
8
8
|
getExceptionName, isFunction, newId, safeGetCookieMgr, safeGetLogger
|
|
9
9
|
} from "@microsoft/applicationinsights-core-js";
|
|
10
10
|
|
|
11
|
-
const
|
|
11
|
+
const SESSION_COOKIE_NAME = "ai_session";
|
|
12
|
+
const ACQUISITION_SPAN = 86400000; // 24 hours in ms
|
|
13
|
+
const RENEWAL_SPAN = 1800000; // 30 minutes in ms
|
|
14
|
+
const COOKIE_UPDATE_INTERVAL = 60000 // 1 minute in ms
|
|
12
15
|
|
|
13
16
|
export interface ISessionConfig {
|
|
14
|
-
sessionRenewalMs?:
|
|
15
|
-
sessionExpirationMs?:
|
|
16
|
-
namePrefix?:
|
|
17
|
-
sessionCookiePostfix?:
|
|
18
|
-
idLength?:
|
|
19
|
-
getNewId?: (
|
|
17
|
+
readonly sessionRenewalMs?: number;
|
|
18
|
+
readonly sessionExpirationMs?: number;
|
|
19
|
+
readonly namePrefix?: string;
|
|
20
|
+
readonly sessionCookiePostfix?: string;
|
|
21
|
+
readonly idLength?: number;
|
|
22
|
+
readonly getNewId?: (idLength?: number) => string;
|
|
20
23
|
|
|
21
24
|
/**
|
|
22
25
|
* @deprecated Avoid using this value to override the cookie manager cookie domain.
|
|
23
26
|
*/
|
|
24
|
-
cookieDomain?:
|
|
27
|
+
cookieDomain?: string;
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
export class Session implements ISession {
|
|
@@ -46,18 +49,16 @@ export class Session implements ISession {
|
|
|
46
49
|
|
|
47
50
|
export class _SessionManager {
|
|
48
51
|
|
|
49
|
-
public static acquisitionSpan = 86400000; // 24 hours in ms
|
|
50
|
-
public static renewalSpan = 1800000; // 30 minutes in ms
|
|
51
|
-
public static cookieUpdateInterval = 60000 // 1 minute in ms
|
|
52
52
|
public automaticSession: Session;
|
|
53
|
-
public config: ISessionConfig;
|
|
54
53
|
|
|
55
54
|
constructor(config: ISessionConfig, core?: IAppInsightsCore) {
|
|
56
55
|
let self = this;
|
|
57
|
-
let _storageNamePrefix:
|
|
56
|
+
let _storageNamePrefix: string;
|
|
58
57
|
let _cookieUpdatedTimestamp: number;
|
|
59
58
|
let _logger: IDiagnosticLogger = safeGetLogger(core);
|
|
60
59
|
let _cookieManager: ICookieMgr = safeGetCookieMgr(core);
|
|
60
|
+
let _sessionExpirationMs: number;
|
|
61
|
+
let _sessionRenewalMs: number;
|
|
61
62
|
|
|
62
63
|
dynamicProto(_SessionManager, self, (_self) => {
|
|
63
64
|
|
|
@@ -65,21 +66,13 @@ export class _SessionManager {
|
|
|
65
66
|
config = ({} as any);
|
|
66
67
|
}
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (!isFunction(config.sessionRenewalMs)) {
|
|
73
|
-
config.sessionRenewalMs = () => _SessionManager.renewalSpan;
|
|
74
|
-
}
|
|
69
|
+
_sessionExpirationMs = config.sessionExpirationMs || ACQUISITION_SPAN;
|
|
70
|
+
_sessionRenewalMs = config.sessionRenewalMs || RENEWAL_SPAN;
|
|
75
71
|
|
|
76
|
-
_self.config = config;
|
|
77
72
|
// sessionCookiePostfix takes the preference if it is configured, otherwise takes namePrefix if configured.
|
|
78
|
-
const sessionCookiePostfix =
|
|
79
|
-
_self.config.sessionCookiePostfix() :
|
|
80
|
-
((_self.config.namePrefix && _self.config.namePrefix()) ? _self.config.namePrefix() : "");
|
|
73
|
+
const sessionCookiePostfix = config.sessionCookiePostfix || config.namePrefix || "";
|
|
81
74
|
|
|
82
|
-
_storageNamePrefix =
|
|
75
|
+
_storageNamePrefix = SESSION_COOKIE_NAME + sessionCookiePostfix;
|
|
83
76
|
|
|
84
77
|
_self.automaticSession = new Session();
|
|
85
78
|
|
|
@@ -94,15 +87,12 @@ export class _SessionManager {
|
|
|
94
87
|
isExpired = !_initializeAutomaticSession(session, nowMs);
|
|
95
88
|
}
|
|
96
89
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (!isExpired && sessionExpirationMs > 0) {
|
|
100
|
-
const sessionRenewalMs = _self.config.sessionRenewalMs();
|
|
90
|
+
if (!isExpired && _sessionExpirationMs > 0) {
|
|
101
91
|
const timeSinceAcqMs = nowMs - session.acquisitionDate;
|
|
102
92
|
const timeSinceRenewalMs = nowMs - session.renewalDate;
|
|
103
|
-
isExpired = timeSinceAcqMs < 0 || timeSinceRenewalMs < 0;
|
|
104
|
-
isExpired = isExpired || timeSinceAcqMs >
|
|
105
|
-
isExpired = isExpired || timeSinceRenewalMs >
|
|
93
|
+
isExpired = timeSinceAcqMs < 0 || timeSinceRenewalMs < 0; // expired if the acquisition or last renewal are in the future
|
|
94
|
+
isExpired = isExpired || timeSinceAcqMs > _sessionExpirationMs; // expired if the time since acquisition is more than session Expiration
|
|
95
|
+
isExpired = isExpired || timeSinceRenewalMs > _sessionRenewalMs; // expired if the time since last renewal is more than renewal period
|
|
106
96
|
}
|
|
107
97
|
|
|
108
98
|
// renew if acquisitionSpan or renewalSpan has elapsed
|
|
@@ -111,7 +101,7 @@ export class _SessionManager {
|
|
|
111
101
|
_renew(nowMs);
|
|
112
102
|
} else {
|
|
113
103
|
// do not update the cookie more often than cookieUpdateInterval
|
|
114
|
-
if (!_cookieUpdatedTimestamp || nowMs - _cookieUpdatedTimestamp >
|
|
104
|
+
if (!_cookieUpdatedTimestamp || nowMs - _cookieUpdatedTimestamp > COOKIE_UPDATE_INTERVAL) {
|
|
115
105
|
_setCookie(session, nowMs);
|
|
116
106
|
}
|
|
117
107
|
}
|
|
@@ -134,7 +124,7 @@ export class _SessionManager {
|
|
|
134
124
|
*/
|
|
135
125
|
function _initializeAutomaticSession(session: ISession, now: number): boolean {
|
|
136
126
|
let isValid = false;
|
|
137
|
-
const cookieValue = _cookieManager.get(_storageNamePrefix
|
|
127
|
+
const cookieValue = _cookieManager.get(_storageNamePrefix);
|
|
138
128
|
if (cookieValue && isFunction(cookieValue.split)) {
|
|
139
129
|
isValid = _initializeAutomaticSessionWithData(session, cookieValue);
|
|
140
130
|
} else {
|
|
@@ -142,7 +132,7 @@ export class _SessionManager {
|
|
|
142
132
|
// This can happen if the session expired or the user actively deleted the cookie
|
|
143
133
|
// We only want to recover data if the cookie is missing from expiry. We should respect the user's wishes if the cookie was deleted actively.
|
|
144
134
|
// The User class handles this for us and deletes our local storage object if the persistent user cookie was removed.
|
|
145
|
-
const storageValue = utlGetLocalStorage(_logger, _storageNamePrefix
|
|
135
|
+
const storageValue = utlGetLocalStorage(_logger, _storageNamePrefix);
|
|
146
136
|
if (storageValue) {
|
|
147
137
|
isValid = _initializeAutomaticSessionWithData(session, storageValue);
|
|
148
138
|
}
|
|
@@ -155,7 +145,7 @@ export class _SessionManager {
|
|
|
155
145
|
* Extract id, acquisitionDate, and renewalDate from an ai_session payload string and
|
|
156
146
|
* use this data to initialize automaticSession.
|
|
157
147
|
*
|
|
158
|
-
* @param
|
|
148
|
+
* @param sessionData - The string stored in an ai_session cookie or local storage backup
|
|
159
149
|
* @returns true if values set otherwise false
|
|
160
150
|
*/
|
|
161
151
|
function _initializeAutomaticSessionWithData(session: ISession, sessionData: string) {
|
|
@@ -197,9 +187,8 @@ export class _SessionManager {
|
|
|
197
187
|
}
|
|
198
188
|
|
|
199
189
|
function _renew(nowMs: number) {
|
|
200
|
-
let
|
|
201
|
-
|
|
202
|
-
_self.automaticSession.id = getNewId(theConfig.idLength ? theConfig.idLength() : 22);
|
|
190
|
+
let getNewId = config.getNewId || newId;
|
|
191
|
+
_self.automaticSession.id = getNewId(config.idLength || 22);
|
|
203
192
|
_self.automaticSession.acquisitionDate = nowMs;
|
|
204
193
|
|
|
205
194
|
_setCookie(_self.automaticSession, nowMs);
|
|
@@ -217,12 +206,11 @@ export class _SessionManager {
|
|
|
217
206
|
let acq = session.acquisitionDate;
|
|
218
207
|
session.renewalDate = nowMs;
|
|
219
208
|
|
|
220
|
-
let
|
|
221
|
-
let renewalPeriodMs = config.sessionRenewalMs();
|
|
209
|
+
let renewalPeriodMs = _sessionRenewalMs;
|
|
222
210
|
|
|
223
211
|
// Set cookie to expire after the session expiry time passes or the session renewal deadline, whichever is sooner
|
|
224
212
|
// Expiring the cookie will cause the session to expire even if the user isn't on the page
|
|
225
|
-
const acqTimeLeftMs = (acq +
|
|
213
|
+
const acqTimeLeftMs = (acq +_sessionExpirationMs) - nowMs;
|
|
226
214
|
const cookie = [session.id, acq, nowMs];
|
|
227
215
|
let maxAgeSec = 0;
|
|
228
216
|
|
|
@@ -232,12 +220,12 @@ export class _SessionManager {
|
|
|
232
220
|
maxAgeSec = renewalPeriodMs / 1000;
|
|
233
221
|
}
|
|
234
222
|
|
|
235
|
-
const cookieDomain = config.cookieDomain
|
|
223
|
+
const cookieDomain = config.cookieDomain || null;
|
|
236
224
|
|
|
237
225
|
// if sessionExpirationMs is set to 0, it means the expiry is set to 0 for this session cookie
|
|
238
226
|
// A cookie with 0 expiry in the session cookie will never expire for that browser session. If the browser is closed the cookie expires.
|
|
239
227
|
// Depending on the browser, another instance does not inherit this cookie, however, another tab will
|
|
240
|
-
_cookieManager.set(_storageNamePrefix
|
|
228
|
+
_cookieManager.set(_storageNamePrefix, cookie.join("|"), _sessionExpirationMs > 0 ? maxAgeSec : null, cookieDomain);
|
|
241
229
|
_cookieUpdatedTimestamp = nowMs;
|
|
242
230
|
}
|
|
243
231
|
|
|
@@ -245,7 +233,7 @@ export class _SessionManager {
|
|
|
245
233
|
// Keep data in local storage to retain the last session id, allowing us to cleanly end the session when it expires
|
|
246
234
|
// Browsers that don't support local storage won't be able to end sessions cleanly from the client
|
|
247
235
|
// The server will notice this and end the sessions itself, with loss of accurate session duration
|
|
248
|
-
utlSetLocalStorage(_logger, _storageNamePrefix
|
|
236
|
+
utlSetLocalStorage(_logger, _storageNamePrefix, [guid, acq, renewal].join("|"));
|
|
249
237
|
}
|
|
250
238
|
});
|
|
251
239
|
}
|
package/src/Context/User.ts
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
import dynamicProto from "@microsoft/dynamicproto-js";
|
|
5
5
|
import { IUserContext, utlRemoveStorage } from "@microsoft/applicationinsights-common";
|
|
6
6
|
import {
|
|
7
|
-
IAppInsightsCore, ICookieMgr, _eInternalMessageId, _throwInternal, eLoggingSeverity, newId,
|
|
7
|
+
IAppInsightsCore, ICookieMgr, _eInternalMessageId, _throwInternal, eLoggingSeverity, newId, onConfigChange, safeGetCookieMgr,
|
|
8
|
+
safeGetLogger, toISOString
|
|
8
9
|
} from "@microsoft/applicationinsights-core-js";
|
|
9
|
-
import {
|
|
10
|
+
import { objDefineProp } from "@nevware21/ts-utils";
|
|
11
|
+
import { IPropertiesConfig } from "../Interfaces/IPropertiesConfig";
|
|
10
12
|
|
|
11
13
|
function _validateUserInput(id: string): boolean {
|
|
12
14
|
// Validate:
|
|
@@ -30,7 +32,7 @@ export class User implements IUserContext {
|
|
|
30
32
|
/**
|
|
31
33
|
* The telemetry configuration.
|
|
32
34
|
*/
|
|
33
|
-
public config:
|
|
35
|
+
public readonly config: IPropertiesConfig;
|
|
34
36
|
|
|
35
37
|
/**
|
|
36
38
|
* The user ID.
|
|
@@ -67,32 +69,72 @@ export class User implements IUserContext {
|
|
|
67
69
|
*/
|
|
68
70
|
public isUserCookieSet = false;
|
|
69
71
|
|
|
70
|
-
constructor(config:
|
|
72
|
+
constructor(config: IPropertiesConfig, core: IAppInsightsCore) {
|
|
71
73
|
let _logger = safeGetLogger(core);
|
|
72
74
|
let _cookieManager: ICookieMgr = safeGetCookieMgr(core);
|
|
73
|
-
let _storageNamePrefix:
|
|
75
|
+
let _storageNamePrefix: string;
|
|
74
76
|
|
|
75
77
|
dynamicProto(User, this, (_self) => {
|
|
76
|
-
_self.config
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
78
|
+
// Define _self.config
|
|
79
|
+
objDefineProp(_self, "config", {
|
|
80
|
+
configurable: true,
|
|
81
|
+
enumerable: true,
|
|
82
|
+
get: () => config
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
onConfigChange(config, () => {
|
|
86
|
+
|
|
87
|
+
const userCookiePostfix = config.userCookiePostfix || "";
|
|
88
|
+
_storageNamePrefix = User.userCookieName + userCookiePostfix;
|
|
89
|
+
|
|
90
|
+
// get userId or create new one if none exists
|
|
91
|
+
const cookie = _cookieManager.get(_storageNamePrefix);
|
|
92
|
+
if (cookie) {
|
|
93
|
+
_self.isNewUser = false;
|
|
94
|
+
const params = cookie.split(User.cookieSeparator);
|
|
95
|
+
if (params.length > 0) {
|
|
96
|
+
_self.id = params[0];
|
|
97
|
+
// we already have a cookie
|
|
98
|
+
_self.isUserCookieSet = !!_self.id;
|
|
99
|
+
}
|
|
89
100
|
}
|
|
90
|
-
|
|
101
|
+
|
|
102
|
+
if (!_self.id) {
|
|
103
|
+
_self.id = _generateNewId();
|
|
104
|
+
const newCookie = _generateNewCookie(_self.id);
|
|
105
|
+
|
|
106
|
+
_setUserCookie(newCookie.join(User.cookieSeparator));
|
|
107
|
+
|
|
108
|
+
// If we have an config.namePrefix() + ai_session in local storage this means the user actively removed our cookies.
|
|
109
|
+
// We should respect their wishes and clear ourselves from local storage
|
|
110
|
+
const name = (config.namePrefix || "") + "ai_session";
|
|
111
|
+
utlRemoveStorage(_logger, name);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// We still take the account id from the ctor param for backward compatibility.
|
|
115
|
+
// But if the the customer set the accountId through the newer setAuthenticatedUserContext API, we will override it.
|
|
116
|
+
_self.accountId = config.accountId || undefined;
|
|
117
|
+
|
|
118
|
+
// Get the auth user id and account id from the cookie if exists
|
|
119
|
+
// Cookie is in the pattern: <authenticatedId>|<accountId>
|
|
120
|
+
let authCookie = _cookieManager.get(User.authUserCookieName);
|
|
121
|
+
if (authCookie) {
|
|
122
|
+
authCookie = decodeURI(authCookie);
|
|
123
|
+
const authCookieString = authCookie.split(User.cookieSeparator);
|
|
124
|
+
if (authCookieString[0]) {
|
|
125
|
+
_self.authenticatedId = authCookieString[0];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (authCookieString.length > 1 && authCookieString[1]) {
|
|
129
|
+
_self.accountId = authCookieString[1];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
91
133
|
|
|
92
134
|
function _generateNewId() {
|
|
93
|
-
let theConfig = (config || {}) as
|
|
94
|
-
let getNewId =
|
|
95
|
-
let id = getNewId(theConfig.idLength ? config.idLength
|
|
135
|
+
let theConfig = (config || {}) as IPropertiesConfig;
|
|
136
|
+
let getNewId = theConfig.getNewId || newId;
|
|
137
|
+
let id = getNewId(theConfig.idLength ? config.idLength : 22);
|
|
96
138
|
return id;
|
|
97
139
|
}
|
|
98
140
|
|
|
@@ -109,38 +151,7 @@ export class User implements IUserContext {
|
|
|
109
151
|
// set it to 365 days from now
|
|
110
152
|
// 365 * 24 * 60 * 60 = 31536000
|
|
111
153
|
const oneYear = 31536000;
|
|
112
|
-
_self.isUserCookieSet = _cookieManager.set(_storageNamePrefix
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (!_self.id) {
|
|
116
|
-
_self.id = _generateNewId();
|
|
117
|
-
const newCookie = _generateNewCookie(_self.id);
|
|
118
|
-
|
|
119
|
-
_setUserCookie(newCookie.join(User.cookieSeparator));
|
|
120
|
-
|
|
121
|
-
// If we have an config.namePrefix() + ai_session in local storage this means the user actively removed our cookies.
|
|
122
|
-
// We should respect their wishes and clear ourselves from local storage
|
|
123
|
-
const name = config.namePrefix && config.namePrefix() ? config.namePrefix() + "ai_session" : "ai_session";
|
|
124
|
-
utlRemoveStorage(_logger, name);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// We still take the account id from the ctor param for backward compatibility.
|
|
128
|
-
// But if the the customer set the accountId through the newer setAuthenticatedUserContext API, we will override it.
|
|
129
|
-
_self.accountId = config.accountId ? config.accountId() : undefined;
|
|
130
|
-
|
|
131
|
-
// Get the auth user id and account id from the cookie if exists
|
|
132
|
-
// Cookie is in the pattern: <authenticatedId>|<accountId>
|
|
133
|
-
let authCookie = _cookieManager.get(User.authUserCookieName);
|
|
134
|
-
if (authCookie) {
|
|
135
|
-
authCookie = decodeURI(authCookie);
|
|
136
|
-
const authCookieString = authCookie.split(User.cookieSeparator);
|
|
137
|
-
if (authCookieString[0]) {
|
|
138
|
-
_self.authenticatedId = authCookieString[0];
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (authCookieString.length > 1 && authCookieString[1]) {
|
|
142
|
-
_self.accountId = authCookieString[1];
|
|
143
|
-
}
|
|
154
|
+
_self.isUserCookieSet = _cookieManager.set(_storageNamePrefix, cookie, oneYear);
|
|
144
155
|
}
|
|
145
156
|
|
|
146
157
|
_self.setAuthenticatedUserContext = (authenticatedUserId: string, accountId?: string, storeInCookie = false) => {
|
|
@@ -196,8 +207,8 @@ export class User implements IUserContext {
|
|
|
196
207
|
/**
|
|
197
208
|
* Sets the authenticated user id and the account id in this session.
|
|
198
209
|
*
|
|
199
|
-
* @param authenticatedUserId {string} - The authenticated user id. A unique and persistent string that represents each authenticated user in the service.
|
|
200
|
-
* @param accountId {string} - An optional string to represent the account associated with the authenticated user.
|
|
210
|
+
* @param authenticatedUserId - {string} - The authenticated user id. A unique and persistent string that represents each authenticated user in the service.
|
|
211
|
+
* @param accountId - {string} - An optional string to represent the account associated with the authenticated user.
|
|
201
212
|
*/
|
|
202
213
|
public setAuthenticatedUserContext(authenticatedUserId: string, accountId?: string, storeInCookie = false) {
|
|
203
214
|
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
|
|
4
|
+
export interface IPropertiesConfig {
|
|
5
|
+
readonly instrumentationKey: string;
|
|
6
|
+
readonly accountId: string;
|
|
7
|
+
readonly sessionRenewalMs: number;
|
|
8
|
+
readonly samplingPercentage: number;
|
|
9
|
+
readonly sessionExpirationMs: number;
|
|
10
|
+
readonly cookieDomain: string,
|
|
11
|
+
readonly sdkExtension: string;
|
|
12
|
+
readonly isBrowserLinkTrackingEnabled: boolean;
|
|
13
|
+
readonly appId: string;
|
|
14
|
+
readonly getSessionId: string;
|
|
15
|
+
readonly namePrefix: string;
|
|
16
|
+
readonly sessionCookiePostfix: string;
|
|
17
|
+
readonly userCookiePostfix: string;
|
|
18
|
+
readonly idLength: number;
|
|
19
|
+
readonly getNewId: (idLength?: number) => string;
|
|
20
|
+
}
|
package/src/PropertiesPlugin.ts
CHANGED
|
@@ -8,40 +8,37 @@ import {
|
|
|
8
8
|
BreezeChannelIdentifier, IConfig, IPropertiesPlugin, PageView, PropertiesPluginIdentifier, createDistributedTraceContextFromTrace
|
|
9
9
|
} from "@microsoft/applicationinsights-common";
|
|
10
10
|
import {
|
|
11
|
-
BaseTelemetryPlugin, IAppInsightsCore, IConfiguration, IDistributedTraceContext, IPlugin, IProcessTelemetryContext,
|
|
11
|
+
BaseTelemetryPlugin, IAppInsightsCore, IConfigDefaults, IConfiguration, IDistributedTraceContext, IPlugin, IProcessTelemetryContext,
|
|
12
12
|
IProcessTelemetryUnloadContext, ITelemetryItem, ITelemetryPluginChain, ITelemetryUnloadState, _InternalLogMessage, _eInternalMessageId,
|
|
13
|
-
_logInternalMessage, createProcessTelemetryContext, eLoggingSeverity, getNavigator, getSetValue, isNullOrUndefined,
|
|
13
|
+
_logInternalMessage, createProcessTelemetryContext, eLoggingSeverity, getNavigator, getSetValue, isNullOrUndefined, onConfigChange
|
|
14
14
|
} from "@microsoft/applicationinsights-core-js";
|
|
15
|
+
import { objDeepFreeze, objDefineProp } from "@nevware21/ts-utils";
|
|
15
16
|
import { IPropTelemetryContext } from "./Interfaces/IPropTelemetryContext";
|
|
16
|
-
import {
|
|
17
|
+
import { IPropertiesConfig } from "./Interfaces/IPropertiesConfig";
|
|
17
18
|
import { TelemetryContext } from "./TelemetryContext";
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
let undefString: string;
|
|
21
|
+
const nullValue: any = null;
|
|
22
|
+
|
|
23
|
+
const _defaultConfig: IConfigDefaults<IPropertiesConfig> = objDeepFreeze({
|
|
24
|
+
instrumentationKey: undefString,
|
|
25
|
+
accountId: nullValue,
|
|
26
|
+
sessionRenewalMs: 30 * 60 * 1000,
|
|
27
|
+
samplingPercentage: 100,
|
|
28
|
+
sessionExpirationMs: 24 * 60 * 60 * 1000,
|
|
29
|
+
cookieDomain: nullValue,
|
|
30
|
+
sdkExtension: nullValue,
|
|
31
|
+
isBrowserLinkTrackingEnabled: false,
|
|
32
|
+
appId: nullValue,
|
|
33
|
+
getSessionId: nullValue,
|
|
34
|
+
namePrefix: undefString,
|
|
35
|
+
sessionCookiePostfix: undefString,
|
|
36
|
+
userCookiePostfix: undefString,
|
|
37
|
+
idLength: 22,
|
|
38
|
+
getNewId: nullValue
|
|
39
|
+
});
|
|
20
40
|
|
|
21
|
-
|
|
22
|
-
let defaultValue: string;
|
|
23
|
-
let nullValue: any = null;
|
|
24
|
-
|
|
25
|
-
const defaultConfig: ITelemetryConfig = {
|
|
26
|
-
instrumentationKey: () => defaultValue,
|
|
27
|
-
accountId: () => nullValue,
|
|
28
|
-
sessionRenewalMs: () => 30 * 60 * 1000,
|
|
29
|
-
samplingPercentage: () => 100,
|
|
30
|
-
sessionExpirationMs: () => 24 * 60 * 60 * 1000,
|
|
31
|
-
cookieDomain: () => nullValue,
|
|
32
|
-
sdkExtension: () => nullValue,
|
|
33
|
-
isBrowserLinkTrackingEnabled: () => false,
|
|
34
|
-
appId: () => nullValue,
|
|
35
|
-
getSessionId: () => nullValue,
|
|
36
|
-
namePrefix: () => defaultValue,
|
|
37
|
-
sessionCookiePostfix: () => defaultValue,
|
|
38
|
-
userCookiePostfix: () => defaultValue,
|
|
39
|
-
idLength: () => 22,
|
|
40
|
-
getNewId: () => nullValue
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
return defaultConfig;
|
|
44
|
-
}
|
|
41
|
+
export default class PropertiesPlugin extends BaseTelemetryPlugin implements IPropertiesPlugin {
|
|
45
42
|
|
|
46
43
|
public context: IPropTelemetryContext;
|
|
47
44
|
|
|
@@ -51,14 +48,23 @@ export default class PropertiesPlugin extends BaseTelemetryPlugin implements IPr
|
|
|
51
48
|
constructor() {
|
|
52
49
|
super();
|
|
53
50
|
|
|
54
|
-
let _extensionConfig:
|
|
51
|
+
let _extensionConfig: IPropertiesConfig;
|
|
55
52
|
let _distributedTraceCtx: IDistributedTraceContext;
|
|
56
53
|
let _previousTraceCtx: IDistributedTraceContext;
|
|
54
|
+
let _context: IPropTelemetryContext;
|
|
57
55
|
|
|
58
56
|
dynamicProto(PropertiesPlugin, this, (_self, _base) => {
|
|
59
57
|
|
|
60
58
|
_initDefaults();
|
|
61
59
|
|
|
60
|
+
objDefineProp(_self, "context", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
configurable: true,
|
|
63
|
+
get: function() {
|
|
64
|
+
return _context;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
62
68
|
_self.initialize = (config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?:ITelemetryPluginChain) => {
|
|
63
69
|
_base.initialize(config, core, extensions, pluginChain);
|
|
64
70
|
_populateDefaults(config);
|
|
@@ -66,23 +72,21 @@ export default class PropertiesPlugin extends BaseTelemetryPlugin implements IPr
|
|
|
66
72
|
|
|
67
73
|
/**
|
|
68
74
|
* Add Part A fields to the event
|
|
69
|
-
* @param event The event that needs to be processed
|
|
75
|
+
* @param event - The event that needs to be processed
|
|
70
76
|
*/
|
|
71
77
|
_self.processTelemetry = (event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) => {
|
|
72
|
-
if (isNullOrUndefined(event)) {
|
|
73
|
-
// TODO(barustum): throw an internal event once we have support for internal logging
|
|
74
|
-
} else {
|
|
78
|
+
if (!isNullOrUndefined(event)) {
|
|
75
79
|
itemCtx = _self._getTelCtx(itemCtx);
|
|
76
80
|
// If the envelope is PageView, reset the internal message count so that we can send internal telemetry for the new page.
|
|
77
81
|
if (event.name === PageView.envelopeType) {
|
|
78
82
|
itemCtx.diagLog().resetInternalMessageCount();
|
|
79
83
|
}
|
|
80
84
|
|
|
81
|
-
let theContext: TelemetryContext = (
|
|
85
|
+
let theContext: TelemetryContext = (_context || {}) as TelemetryContext;
|
|
82
86
|
|
|
83
87
|
if (theContext.session) {
|
|
84
88
|
// If customer did not provide custom session id update the session manager
|
|
85
|
-
if (typeof
|
|
89
|
+
if (typeof _context.session.id !== "string" && theContext.sessionManager) {
|
|
86
90
|
theContext.sessionManager.update();
|
|
87
91
|
}
|
|
88
92
|
}
|
|
@@ -120,30 +124,30 @@ export default class PropertiesPlugin extends BaseTelemetryPlugin implements IPr
|
|
|
120
124
|
_extensionConfig = null;
|
|
121
125
|
_distributedTraceCtx = null;
|
|
122
126
|
_previousTraceCtx = null;
|
|
127
|
+
_context = null;
|
|
123
128
|
}
|
|
124
129
|
|
|
125
130
|
function _populateDefaults(config: IConfiguration) {
|
|
126
131
|
let identifier = _self.identifier;
|
|
127
132
|
let core = _self.core;
|
|
128
133
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
_self["_extConfig"] = _extensionConfig;
|
|
134
|
+
// This function will be re-called whenever any referenced configuration is changed
|
|
135
|
+
_self._addHook(onConfigChange(config, () => {
|
|
136
|
+
let ctx = createProcessTelemetryContext(null, config, core);
|
|
137
|
+
_extensionConfig = ctx.getExtCfg(identifier, _defaultConfig);
|
|
138
|
+
|
|
139
|
+
_previousTraceCtx = core.getTraceCtx(false);
|
|
140
|
+
_context = new TelemetryContext(core, _extensionConfig, _previousTraceCtx);
|
|
141
|
+
_distributedTraceCtx = createDistributedTraceContextFromTrace(_self.context.telemetryTrace, _previousTraceCtx);
|
|
142
|
+
core.setTraceCtx(_distributedTraceCtx);
|
|
143
|
+
_self.context.appId = () => {
|
|
144
|
+
let breezeChannel = core.getPlugin<IPlugin>(BreezeChannelIdentifier);
|
|
145
|
+
return breezeChannel ? breezeChannel.plugin["_appId"] : null;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// Test hook to allow accessing the internal values -- explicitly not defined as an available property on the class
|
|
149
|
+
_self["_extConfig"] = _extensionConfig;
|
|
150
|
+
}));
|
|
147
151
|
}
|
|
148
152
|
|
|
149
153
|
function _processTelemetryInternal(evt: ITelemetryItem, itemCtx: IProcessTelemetryContext) {
|
|
@@ -173,7 +177,7 @@ export default class PropertiesPlugin extends BaseTelemetryPlugin implements IPr
|
|
|
173
177
|
|
|
174
178
|
/**
|
|
175
179
|
* Add Part A fields to the event
|
|
176
|
-
* @param event The event that needs to be processed
|
|
180
|
+
* @param event - The event that needs to be processed
|
|
177
181
|
*/
|
|
178
182
|
public processTelemetry(event: ITelemetryItem, itemCtx?: IProcessTelemetryContext) {
|
|
179
183
|
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
|
package/src/TelemetryContext.ts
CHANGED
|
@@ -20,7 +20,7 @@ import { Session, _SessionManager } from "./Context/Session";
|
|
|
20
20
|
import { TelemetryTrace } from "./Context/TelemetryTrace";
|
|
21
21
|
import { User } from "./Context/User";
|
|
22
22
|
import { IPropTelemetryContext } from "./Interfaces/IPropTelemetryContext";
|
|
23
|
-
import {
|
|
23
|
+
import { IPropertiesConfig } from "./Interfaces/IPropertiesConfig";
|
|
24
24
|
|
|
25
25
|
const strExt = "ext";
|
|
26
26
|
const strTags = "tags";
|
|
@@ -31,6 +31,10 @@ function _removeEmpty(target: any, name: string) {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
function _nullResult(): string {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
34
38
|
export class TelemetryContext implements IPropTelemetryContext {
|
|
35
39
|
|
|
36
40
|
public application: IApplication; // The object describing a component tracked by this object - legacy
|
|
@@ -46,12 +50,12 @@ export class TelemetryContext implements IPropTelemetryContext {
|
|
|
46
50
|
public appId: () => string;
|
|
47
51
|
public getSessionId: () => string;
|
|
48
52
|
|
|
49
|
-
constructor(core: IAppInsightsCore, defaultConfig:
|
|
53
|
+
constructor(core: IAppInsightsCore, defaultConfig: IPropertiesConfig, previousTraceCtx?: IDistributedTraceContext) {
|
|
50
54
|
let logger = core.logger
|
|
51
|
-
this.appId = () => null;
|
|
52
|
-
this.getSessionId = () => null;
|
|
53
55
|
|
|
54
56
|
dynamicProto(TelemetryContext, this, (_self) => {
|
|
57
|
+
_self.appId = _nullResult;
|
|
58
|
+
_self.getSessionId = _nullResult;
|
|
55
59
|
_self.application = new Application();
|
|
56
60
|
_self.internal = new Internal(defaultConfig);
|
|
57
61
|
if (hasWindow()) {
|
|
@@ -24,23 +24,18 @@ export const _DYN_APPLY_USER_CONTEXT = "applyUserContext"; // Count: 2
|
|
|
24
24
|
export const _DYN_APPLY_OPERATING_SYST3 = "applyOperatingSystemContxt"; // Count: 2
|
|
25
25
|
export const _DYN_APPLY_LOCATION_CONTE4 = "applyLocationContext"; // Count: 2
|
|
26
26
|
export const _DYN_APPLY_INTERNAL_CONTE5 = "applyInternalContext"; // Count: 2
|
|
27
|
-
export const _DYN_ACCOUNT_ID = "accountId"; // Count:
|
|
28
|
-
export const _DYN_SDK_EXTENSION = "sdkExtension"; // Count: 4
|
|
27
|
+
export const _DYN_ACCOUNT_ID = "accountId"; // Count: 7
|
|
29
28
|
export const _DYN_GET_SESSION_ID = "getSessionId"; // Count: 4
|
|
30
|
-
export const _DYN_NAME_PREFIX = "namePrefix"; // Count:
|
|
31
|
-
export const _DYN_SESSION_COOKIE_POSTF6 = "sessionCookiePostfix"; // Count:
|
|
32
|
-
export const _DYN_USER_COOKIE_POSTFIX = "userCookiePostfix"; // Count:
|
|
33
|
-
export const _DYN_ID_LENGTH = "idLength"; // Count:
|
|
34
|
-
export const _DYN_GET_NEW_ID = "getNewId"; // Count:
|
|
29
|
+
export const _DYN_NAME_PREFIX = "namePrefix"; // Count: 3
|
|
30
|
+
export const _DYN_SESSION_COOKIE_POSTF6 = "sessionCookiePostfix"; // Count: 2
|
|
31
|
+
export const _DYN_USER_COOKIE_POSTFIX = "userCookiePostfix"; // Count: 2
|
|
32
|
+
export const _DYN_ID_LENGTH = "idLength"; // Count: 4
|
|
33
|
+
export const _DYN_GET_NEW_ID = "getNewId"; // Count: 3
|
|
35
34
|
export const _DYN_LENGTH = "length"; // Count: 4
|
|
36
35
|
export const _DYN_AUTOMATIC_SESSION = "automaticSession"; // Count: 5
|
|
37
36
|
export const _DYN_AUTHENTICATED_ID = "authenticatedId"; // Count: 6
|
|
38
|
-
export const _DYN_SESSION_EXPIRATION_M7 = "sessionExpirationMs"; // Count: 5
|
|
39
|
-
export const _DYN_SESSION_RENEWAL_MS = "sessionRenewalMs"; // Count: 4
|
|
40
|
-
export const _DYN_CONFIG = "config"; // Count: 4
|
|
41
37
|
export const _DYN_ACQUISITION_DATE = "acquisitionDate"; // Count: 5
|
|
42
38
|
export const _DYN_RENEWAL_DATE = "renewalDate"; // Count: 4
|
|
43
|
-
export const _DYN_COOKIE_DOMAIN = "cookieDomain"; // Count: 3
|
|
44
39
|
export const _DYN_JOIN = "join"; // Count: 5
|
|
45
40
|
export const _DYN_COOKIE_SEPARATOR = "cookieSeparator"; // Count: 5
|
|
46
|
-
export const
|
|
41
|
+
export const _DYN_AUTH_USER_COOKIE_NAM7 = "authUserCookieName"; // Count: 3
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IInternal } from "@microsoft/applicationinsights-common";
|
|
2
|
-
import {
|
|
2
|
+
import { IPropertiesConfig } from "../Interfaces/IPropertiesConfig";
|
|
3
3
|
export declare class Internal implements IInternal {
|
|
4
4
|
/**
|
|
5
5
|
* The SDK version used to create this telemetry item.
|
|
@@ -25,5 +25,5 @@ export declare class Internal implements IInternal {
|
|
|
25
25
|
/**
|
|
26
26
|
* Constructs a new instance of the internal telemetry data class.
|
|
27
27
|
*/
|
|
28
|
-
constructor(config:
|
|
28
|
+
constructor(config: IPropertiesConfig);
|
|
29
29
|
}
|