Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setUserCookie method on cookie manager #1628

Merged
merged 5 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions extensions/applicationinsights-properties-js/src/Context/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,34 @@ export class User implements IUserContext {
}
}

if (!_self.id) {
function _generateNewId() {
let theConfig = (config || {}) as ITelemetryConfig;
let getNewId = (theConfig.getNewId ? theConfig.getNewId() : null) || newId;
_self.id = getNewId(theConfig.idLength ? config.idLength() : 22);
let id = getNewId(theConfig.idLength ? config.idLength() : 22);
return id;
}

function _generateNewCookie(userId: string) {
const acqStr = toISOString(new Date());
_self.accountAcquisitionDate = acqStr;
_self.isNewUser = true;
const newCookie = [userId, acqStr];
return newCookie;
}

function _setUserCookie(cookie: string) {
// without expiration, cookies expire at the end of the session
// set it to 365 days from now
// 365 * 24 * 60 * 60 = 31536000
const oneYear = 31536000;
const acqStr = toISOString(new Date());
_self.accountAcquisitionDate = acqStr;
_self.isNewUser = true;
const newCookie = [_self.id, acqStr];
_cookieManager.set(_storageNamePrefix(), cookie, oneYear);
}

_cookieManager.set(_storageNamePrefix(), newCookie.join(User.cookieSeparator), oneYear);
if (!_self.id) {
_self.id = _generateNewId();
const newCookie = _generateNewCookie(_self.id);

_setUserCookie(newCookie.join(User.cookieSeparator));

// If we have an config.namePrefix() + ai_session in local storage this means the user actively removed our cookies.
// We should respect their wishes and clear ourselves from local storage
Expand Down Expand Up @@ -156,6 +170,12 @@ export class User implements IUserContext {
_self.accountId = null;
_cookieManager.del(User.authUserCookieName);
};

_self.update = (userId?: string) => {
let user_id = userId ? userId : _generateNewId();
let user_cookie = _generateNewCookie(user_id);
_setUserCookie(user_cookie.join(User.cookieSeparator));
}
});
}

Expand All @@ -176,4 +196,8 @@ export class User implements IUserContext {
public clearAuthenticatedUserContext() {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}

public update(userId?: string) {
xiao-lix marked this conversation as resolved.
Show resolved Hide resolved
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export default class PropertiesPlugin extends BaseTelemetryPlugin implements IPr
theContext.sessionManager.update();
}
}

if (theContext.user) {
theContext.user.update(theContext.user.id);
}

_processTelemetryInternal(event, itemCtx);

Expand Down
1 change: 1 addition & 0 deletions shared/AppInsightsCommon/src/Interfaces/Context/IUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ export interface IUser {
export interface IUserContext extends IUser {
setAuthenticatedUserContext(authenticatedUserId: string, accountId?: string, storeInCookie?: boolean): void;
clearAuthenticatedUserContext(): void;
update (userId?: string): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export interface ICookieMgr {
* Configuration definition for instance based cookie management configuration
*/
export interface ICookieMgrConfig {
/** Defaults to true, A boolean that indicates whether the use of cookies by the SDK is enabled by the current instance.
/**
* Defaults to true, A boolean that indicates whether the use of cookies by the SDK is enabled by the current instance.
* If false, the instance of the SDK initialized by this configuration will not store or read any data from cookies
*/
enabled?: boolean;
Expand Down