Skip to content

Commit 4d411fd

Browse files
haraldschillyclaude
andcommitted
hub/analytics: make analytics cookie optional
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 612d127 commit 4d411fd

File tree

10 files changed

+194
-94
lines changed

10 files changed

+194
-94
lines changed

src/.claude/settings.local.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
"Bash(pnpm tsc:*)",
55
"Bash(pnpm build:*)",
66
"Bash(git add:*)",
7-
"Bash(git commit:*)"
7+
"Bash(git commit:*)",
8+
"Bash(grep:*)",
9+
"Bash(tsc --noEmit)",
10+
"Bash(git fetch:*)",
11+
"Bash(git merge:*)"
812
],
913
"deny": []
1014
}

src/CLAUDE.md

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
44

55
# CoCalc Source Repository
66

7-
* This is the source code of CoCalc in a Git repository
8-
* It is a complex JavaScript/TypeScript SaaS application
9-
* CoCalc is organized as a monorepository (multi-packages) in the subdirectory "./packages"
10-
* The packages are managed as a pnpm workspace in "./packages/pnpm-workspace.yaml"
7+
- This is the source code of CoCalc in a Git repository
8+
- It is a complex JavaScript/TypeScript SaaS application
9+
- CoCalc is organized as a monorepository (multi-packages) in the subdirectory "./packages"
10+
- The packages are managed as a pnpm workspace in "./packages/pnpm-workspace.yaml"
1111

1212
## Code Style
1313

1414
- Everything is written in TypeScript code
1515
- Indentation: 2-spaces
16+
- Run `pretter -w [filename]` after modifying a file (ts, tsx, md, json, ...) to format it correctly.
1617
- All .js and .ts files are formatted by the tool prettier
1718
- Add suitable types when you write code
1819
- Variable name styles are "camelCase" for local and "FOO_BAR" for global variables. If you edit older code not following these guidlines, adjust this rule to fit the files style.
@@ -23,28 +24,30 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
2324
## Development Commands
2425

2526
### Essential Commands
27+
2628
- `pnpm build-dev` - Build all packages for development
2729
- `pnpm clean` - Clean all node_modules and dist directories
28-
- `pnpm database` - Start PostgreSQL database server
29-
- `pnpm hub` - Start the main hub server
30-
- `pnpm psql` - Connect to the PostgreSQL database
3130
- `pnpm test` - Run full test suite
32-
- `pnpm test-parallel` - Run tests in parallel across packages
3331
- `pnpm depcheck` - Check for dependency issues
32+
- `prettier -w [filename]` to format the style of a file after editing it
33+
- after creating a file, run `git add [filename]` to start tracking it
3434

3535
### Package-Specific Commands
36-
- `cd packages/[package] && pnpm tsc` - Watch TypeScript compilation for a specific package
36+
37+
- `cd packages/[package] && pnpm tsc` - TypeScript compilation for a specific package
3738
- `cd packages/[package] && pnpm test` - Run tests for a specific package
3839
- `cd packages/[package] && pnpm build` - Build a specific package
40+
- **IMPORTANT**: When modifying packages like `util` that other packages depend on, you must run `pnpm build` in the dependency package before typechecking dependent packages
3941

40-
### Development Setup
41-
1. Start database: `pnpm database`
42-
2. Start hub: `pnpm hub`
43-
3. For TypeScript changes, run `pnpm tsc` in the relevant package directory
42+
### Development
43+
44+
- After code changes, run `pretter -w [filename]` to ensure consistent styling
45+
- After TypeScript or `*.tsx` changes, run `pnpm tsc` in the relevant package directory
4446

4547
## Architecture Overview
4648

4749
### Package Structure
50+
4851
CoCalc is organized as a monorepo with key packages:
4952

5053
- **frontend** - React/TypeScript frontend application using Redux-style stores and actions
@@ -62,25 +65,29 @@ CoCalc is organized as a monorepo with key packages:
6265
### Key Architectural Patterns
6366

6467
#### Frontend Architecture
68+
6569
- **Redux-style State Management**: Uses custom stores and actions pattern (see `packages/frontend/app-framework/actions-and-stores.ts`)
6670
- **TypeScript React Components**: All frontend code is TypeScript with proper typing
6771
- **Modular Store System**: Each feature has its own store/actions (AccountStore, BillingStore, etc.)
6872
- **WebSocket Communication**: Real-time communication with backend via WebSocket messages
6973

7074
#### Backend Architecture
75+
7176
- **PostgreSQL Database**: Primary data store with sophisticated querying system
7277
- **WebSocket Messaging**: Real-time communication between frontend and backend
7378
- **Conat System**: Container orchestration for compute servers
7479
- **Event-Driven Architecture**: Extensive use of EventEmitter patterns
7580
- **Microservice-like Packages**: Each package handles specific functionality
7681

7782
#### Communication Patterns
83+
7884
- **WebSocket Messages**: Primary communication method (see `packages/comm/websocket/types.ts`)
7985
- **Database Queries**: Structured query system with typed interfaces
8086
- **Event Emitters**: Inter-service communication within backend
8187
- **REST-like APIs**: Some HTTP endpoints for specific operations
8288

8389
### Key Technologies
90+
8491
- **TypeScript**: Primary language for all new code
8592
- **React**: Frontend framework
8693
- **PostgreSQL**: Database
@@ -91,40 +98,56 @@ CoCalc is organized as a monorepo with key packages:
9198
- **SASS**: CSS preprocessing
9299

93100
### Database Schema
101+
94102
- Comprehensive schema in `packages/util/db-schema`
95103
- Query abstractions in `packages/database/postgres/`
96104
- Type-safe database operations with TypeScript interfaces
97105

98106
### Testing
107+
99108
- **Jest**: Primary testing framework
100109
- **ts-jest**: TypeScript support for Jest
101110
- **jsdom**: Browser environment simulation for frontend tests
102111
- Test files use `.test.ts` or `.spec.ts` extensions
103112
- Each package has its own jest.config.js
104113

105114
### Import Patterns
115+
106116
- Use absolute imports with `@cocalc/` prefix for cross-package imports
107117
- Example: `import { cmp } from "@cocalc/util/misc"`
108118
- Type imports: `import type { Foo } from "./bar"`
109119
- Destructure imports when possible
110120

111121
### Development Workflow
122+
112123
1. Changes to TypeScript require compilation (`pnpm tsc` in relevant package)
113124
2. Database must be running before starting hub
114125
3. Hub coordinates all services and should be restarted after changes
115126
4. Use `pnpm clean && pnpm build-dev` when switching branches or after major changes
116127

117128
# Workflow
129+
118130
- Be sure to typecheck when you're done making a series of code changes
119131
- Prefer running single tests, and not the whole test suite, for performance
120132

121133
## Git Workflow
122134

135+
- Never modify a file when in the `master` or `main` branch
136+
- All changes happen through feature branches, which are pushed as pull requests to GitHub
137+
- When creating a new file, run `git add [filename]` to track the file.
123138
- Prefix git commits with the package and general area. e.g. 'frontend/latex: ...' if it concerns latex editor changes in the packages/frontend/... code.
124139
- When pushing a new branch to Github, track it upstream. e.g. `git push --set-upstream origin feature-foo` for branch "feature-foo".
125140

126-
# important-instruction-reminders
141+
# Important Instruction Reminders
142+
127143
- Do what has been asked; nothing more, nothing less.
128144
- NEVER create files unless they're absolutely necessary for achieving your goal.
129145
- ALWAYS prefer editing an existing file to creating a new one.
130-
- NEVER proactively create documentation files (*.md) or README files. Only create documentation files if explicitly requested by the User.
146+
- REFUSE to modify files when the git repository is on the `master` or `main` branch.
147+
- NEVER proactively create documentation files (`*.md`) or README files. Only create documentation files if explicitly requested by the User.
148+
149+
# Ignore
150+
151+
- Ignore files covered by `.gitignore`
152+
- Ignore everything in `node_modules` or `dist` directories
153+
- Ignore all files not tracked by Git, unless they are newly created files

src/packages/frontend/user-tracking.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@
88
// completely change this if we want.
99

1010
import { query, server_time } from "./frame-editors/generic/client";
11-
import { analytics_cookie_name as analytics, uuid } from "@cocalc/util/misc";
11+
import { uuid } from "@cocalc/util/misc";
1212
import { redux } from "./app-framework";
1313
import { version } from "@cocalc/util/smc-version";
1414
import { get_cookie } from "./misc";
1515
import { webapp_client } from "./webapp-client";
1616

17+
import { ANALYTICS_COOKIE_NAME, ANALYTICS_ENABLED } from "@cocalc/util/consts";
18+
1719
export async function log(eventName: string, payload: any): Promise<void> {
20+
if (!ANALYTICS_ENABLED) {
21+
return;
22+
}
23+
1824
const central_log = {
1925
id: uuid(),
2026
event: `webapp-${eventName}`,
2127
value: {
2228
account_id: redux.getStore("account")?.get("account_id"),
23-
analytics_cookie: get_cookie(analytics),
29+
analytics_cookie: get_cookie(ANALYTICS_COOKIE_NAME),
2430
cocalc_version: version,
2531
...payload,
2632
},
@@ -43,6 +49,10 @@ export default async function track(
4349
event: string,
4450
value: object,
4551
): Promise<void> {
52+
if (!ANALYTICS_ENABLED) {
53+
return;
54+
}
55+
4656
// Replace all dashes with underscores in the event argument for consistency
4757
event = event.replace(/-/g, "_");
4858

src/packages/hub/analytics-script.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
* e.g. this filters the SSO auth pages, which are uninteresting referrals
1616
*/
1717

18-
// variable PREFIX, NAME, DOMAIN and ID are injected in the hub's http server
19-
declare var NAME, ID, DOMAIN, PREFIX, window, document;
18+
// variable PREFIX, NAME, DOMAIN, ID, and ANALYTICS_ENABLED are injected in the hub's http server
19+
declare var NAME, ID, DOMAIN, PREFIX, ANALYTICS_ENABLED, window, document;
2020

21-
// write cookie. it would be cool to set this via the http request itself,
22-
// but for reasons I don't know it doesn't work across subdomains.
23-
const maxage = 7 * 24 * 60 * 60; // 7 days
24-
document.cookie = `${NAME}=${ID}; path=/; domain=${DOMAIN}; max-age=${maxage}`;
21+
// write cookie only if analytics is enabled (for privacy in cookieless mode)
22+
if (ANALYTICS_ENABLED) {
23+
// it would be cool to set this via the http request itself,
24+
// but for reasons I don't know it doesn't work across subdomains.
25+
const maxage = 7 * 24 * 60 * 60; // 7 days
26+
document.cookie = `${NAME}=${ID}; path=/; domain=${DOMAIN}; max-age=${maxage}`;
27+
}
2528

2629
const { href, protocol, host, pathname } = window.location;
2730

0 commit comments

Comments
 (0)