diff --git a/packages/clarity-js/src/core/time.ts b/packages/clarity-js/src/core/time.ts index 54c8b6b3..c1e1f27f 100644 --- a/packages/clarity-js/src/core/time.ts +++ b/packages/clarity-js/src/core/time.ts @@ -1,12 +1,17 @@ let startTime = 0; export function start(): void { - startTime = performance.now(); + startTime = performance.now() + performance.timeOrigin; } +// event.timestamp is number of milliseconds elapsed since the document was loaded +// since iframes can be loaded later the event timestamp is not the same as performance.now() +// converting everything to absolute time by adding timeorigin of the event view +// to synchronize times before calculating the difference with start time export function time(event: UIEvent = null): number { let ts = event && event.timeStamp > 0 ? event.timeStamp : performance.now(); - return Math.max(Math.round(ts - startTime), 0); + let origin = event && event.view ? event.view.performance.timeOrigin : performance.timeOrigin; + return Math.max(Math.round(ts + origin - startTime), 0); } export function stop(): void { diff --git a/packages/clarity-js/src/layout/encode.ts b/packages/clarity-js/src/layout/encode.ts index 9731583b..3400bf48 100644 --- a/packages/clarity-js/src/layout/encode.ts +++ b/packages/clarity-js/src/layout/encode.ts @@ -93,8 +93,10 @@ export default async function (type: Event, timer: Timer = null, ts: number = nu let box = size(value); let factor = mangle ? -1 : 1; tokens.push(value.id * factor); - if (value.parent && active) { tokens.push(value.parent); } - if (value.previous && active) { tokens.push(value.previous); } + if (value.parent && active) { + tokens.push(value.parent); + if (value.previous) { tokens.push(value.previous); } + } tokens.push(suspend ? Constant.SuspendMutationTag : data[key]); if (box && box.length === 2) { tokens.push(`${Constant.Hash}${str(box[0])}.${str(box[1])}`); } break; diff --git a/packages/clarity-js/src/layout/mutation.ts b/packages/clarity-js/src/layout/mutation.ts index 0e46ebef..180f39a1 100644 --- a/packages/clarity-js/src/layout/mutation.ts +++ b/packages/clarity-js/src/layout/mutation.ts @@ -160,7 +160,8 @@ function track(m: MutationRecord, timer: Timer, instance: number, timestamp: num let value = m.target ? dom.get(m.target.parentNode) : null; // Check if the parent is already discovered and that the parent is not the document root if (value && value.data.tag !== Constant.HTML) { - let inactive = time() > activePeriod; + // calculate inactive period based on the timestamp of the mutation not when the mutation is processed + let inactive = timestamp > activePeriod; let target = dom.get(m.target); let element = target && target.selector ? target.selector.join() : m.target.nodeName; let parent = value.selector ? value.selector.join() : Constant.Empty;