Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions apps/dotcom/client/src/utils/analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ function getGA4() {

export function trackEvent(name: string, data?: { [key: string]: any }) {
getPosthog()?.capture(name, data)

// For GA4, rename 'source' to 'event_source' to avoid session attribution
if (data) {
const { source, ...rest } = data
data = source !== undefined ? { ...rest, event_source: source } : rest
}
getGA4()?.event(name, data)
}

Expand Down
12 changes: 10 additions & 2 deletions packages/sync-core/src/lib/TLSocketRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class TLSocketRoom<R extends UnknownRecord = UnknownRecord, SessionMeta =
{ assembler: JsonChunkAssembler; socket: WebSocketMinimal; unlisten: () => void }
>()
readonly log?: TLSyncLog
private readonly syncCallbacks: {
onDataChange?(): void
onPresenceChange?(): void
}

constructor(
public readonly opts: {
Expand Down Expand Up @@ -65,11 +69,14 @@ export class TLSocketRoom<R extends UnknownRecord = UnknownRecord, SessionMeta =
? convertStoreSnapshotToRoomSnapshot(opts.initialSnapshot!)
: opts.initialSnapshot

this.syncCallbacks = {
onDataChange: opts.onDataChange,
onPresenceChange: opts.onPresenceChange,
}
this.room = new TLSyncRoom<R, SessionMeta>({
...this.syncCallbacks,
schema: opts.schema ?? (createTLSchema() as any),
snapshot: initialSnapshot,
onDataChange: opts.onDataChange,
onPresenceChange: opts.onPresenceChange,
log: opts.log,
})
this.room.events.on('session_removed', (args) => {
Expand Down Expand Up @@ -314,6 +321,7 @@ export class TLSocketRoom<R extends UnknownRecord = UnknownRecord, SessionMeta =
})

const newRoom = new TLSyncRoom<R, SessionMeta>({
...this.syncCallbacks,
schema: oldRoom.schema,
snapshot: {
clock: oldRoom.clock + 1,
Expand Down
26 changes: 25 additions & 1 deletion packages/sync-core/src/test/TLSocketRoom.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InstancePresenceRecordType, PageRecordType } from '@tldraw/tlschema'
import { createTLSchema, createTLStore } from 'tldraw'
import { createTLSchema, createTLStore, ZERO_INDEX_KEY } from 'tldraw'
import { WebSocketMinimal } from '../lib/ServerSocketAdapter'
import { TLSocketRoom } from '../lib/TLSocketRoom'
import { RecordOpType } from '../lib/diff'
Expand Down Expand Up @@ -241,4 +241,28 @@ describe(TLSocketRoom, () => {
)
expect(documentRecordIds).toHaveLength(0)
})

it('passes onDataChange handler through', async () => {
const addPage = (room: TLSocketRoom) =>
room.updateStore((store) => {
store.put(
PageRecordType.create({ id: PageRecordType.createId(), name: '', index: ZERO_INDEX_KEY })
)
})
const store = getStore()
store.ensureStoreIsUsable()
let called = 0

const room = new TLSocketRoom({ onDataChange: () => ++called })
expect(called).toEqual(0)

await addPage(room)
expect(called).toEqual(1)

room.loadSnapshot(room.getCurrentSnapshot())
expect(called).toEqual(1)

await addPage(room)
expect(called).toEqual(2)
})
})
Loading