-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 5.2] web/tracking: extract user and session tracking from E…
…ventLogger (#57884) (#58029) `EventLogger` is currently a very overloaded mechanism that handles all sorts of tracking mechanisms and cookie configurations on top of event logging. This change extracts user tracking (anonymous user ID, cohorts, and device ID) and session tracking (URL stuff) from `EventLogger` into separate, isolated classes. Functionally everything should be the same, but streamlined significantly. Most importantly, this unblocks us so that we can use these mechanisms for introducing a client implementation for the [new telemetry framework](https://docs.sourcegraph.com/dev/background-information/telemetry) while preserving existing behaviour. This only changes `client/web` - it seems this is copy-pasted into several places, we can work on centralizing them all later. Part of https://github.com/sourcegraph/sourcegraph/issues/56920 Enables https://github.com/sourcegraph/sourcegraph/pull/57939 `sg start dotcom`, then: ``` select * from event_logs where source = 'WEB' order by timestamp desc limit 3; ``` (cherry picked from commit f79aa36)
- Loading branch information
Showing
9 changed files
with
310 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import cookies, { type CookieAttributes } from 'js-cookie' | ||
|
||
/** | ||
* Cookies is a simple interface over real cookies from 'js-cookie'. | ||
*/ | ||
export interface Cookies { | ||
/** | ||
* Read cookie | ||
*/ | ||
get(name: string): string | undefined | ||
/** | ||
* Create a cookie | ||
*/ | ||
set(name: string, value: string, options?: CookieAttributes): string | undefined | ||
} | ||
|
||
/** | ||
* Alias for 'js-cookie' default implementation, behind the Cookies interface. | ||
*/ | ||
export function defaultCookies(): Cookies { | ||
return cookies | ||
} | ||
|
||
export const userCookieSettings: CookieAttributes = { | ||
// 365 days expiry, but renewed on activity. | ||
expires: 365, | ||
// Enforce HTTPS | ||
secure: true, | ||
// We only read the cookie with JS so we don't need to send it cross-site nor on initial page requests. | ||
// However, we do need it on page redirects when users sign up via OAuth, hence using the Lax policy. | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite | ||
sameSite: 'Lax', | ||
// Specify the Domain attribute to ensure subdomains (about.sourcegraph.com) can receive this cookie. | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#define_where_cookies_are_sent | ||
domain: location.hostname, | ||
} | ||
|
||
export const deviceSessionCookieSettings: CookieAttributes = { | ||
// ~30 minutes expiry, but renewed on activity. | ||
expires: 0.0208, | ||
// Enforce HTTPS | ||
secure: true, | ||
// We only read the cookie with JS so we don't need to send it cross-site nor on initial page requests. | ||
// However, we do need it on page redirects when users sign up via OAuth, hence using the Lax policy. | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite | ||
sameSite: 'Lax', | ||
// Specify the Domain attribute to ensure subdomains (about.sourcegraph.com) can receive this cookie. | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#define_where_cookies_are_sent | ||
domain: location.hostname, | ||
} |
Oops, something went wrong.