Skip to content

Commit

Permalink
Merge pull request #569 from microsoft/zoeyang/attentionmap
Browse files Browse the repository at this point in the history
recording start & end element hash while scrolling
  • Loading branch information
zoeyang7633 authored Apr 5, 2024
2 parents 5bf0f46 + 60ae3fd commit 163047d
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "0.7.29",
"version": "0.7.30",
"npmClient": "yarn",
"useWorkspaces": true
}
2 changes: 1 addition & 1 deletion packages/clarity-decode/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clarity-decode",
"version": "0.7.29",
"version": "0.7.30",
"description": "An analytics library that uses web page interactions to generate aggregated insights",
"author": "Microsoft Corp.",
"license": "MIT",
Expand Down
4 changes: 3 additions & 1 deletion packages/clarity-decode/src/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export function decode(tokens: Data.Token[]): InteractionEvent {
let scrollData: Interaction.ScrollData = {
target: tokens[2] as number,
x: tokens[3] as number,
y: tokens[4] as number
y: tokens[4] as number,
top: tokens[5] as string,
bottom: tokens[6] as string
};
return { time, event, data: scrollData };
case Data.Event.Timeline:
Expand Down
2 changes: 1 addition & 1 deletion packages/clarity-devtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clarity-devtools",
"version": "0.7.29",
"version": "0.7.30",
"private": true,
"description": "Adds Clarity debugging support to browser devtools",
"author": "Microsoft Corp.",
Expand Down
2 changes: 1 addition & 1 deletion packages/clarity-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clarity-js",
"version": "0.7.29",
"version": "0.7.30",
"description": "An analytics library that uses web page interactions to generate aggregated insights",
"author": "Microsoft Corp.",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/clarity-js/src/core/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
let version = "0.7.29";
let version = "0.7.30";
export default version;
29 changes: 28 additions & 1 deletion packages/clarity-js/src/interaction/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { schedule } from "@src/core/task";
import { time } from "@src/core/time";
import { clearTimeout, setTimeout } from "@src/core/timeout";
import { iframe } from "@src/layout/dom";
import * as dom from "../layout/dom";
import { target } from "@src/layout/target";
import encode from "./encode";

Expand Down Expand Up @@ -39,7 +40,16 @@ function recompute(event: UIEvent = null): void {
// And, if for some reason that is not available, fall back to looking up scrollTop on document.documentElement.
let x = element === de && "pageXOffset" in w ? Math.round(w.pageXOffset) : Math.round((element as HTMLElement).scrollLeft);
let y = element === de && "pageYOffset" in w ? Math.round(w.pageYOffset) : Math.round((element as HTMLElement).scrollTop);
let current: ScrollState = { time: time(event), event: Event.Scroll, data: {target: element, x, y} };
const width = window.innerWidth;
const height = window.innerHeight;
const xPosition = width / 3;
const yOffset = width > height ? height * 0.15 : height * 0.2;
const startYPosition = yOffset;
const endYPosition = height - yOffset;
const top = getPositionHash(xPosition, startYPosition);
const bottom = getPositionHash(xPosition, endYPosition);

let current: ScrollState = { time: time(event), event: Event.Scroll, data: {target: element, x, y, top, bottom} };

// We don't send any scroll events if this is the first event and the current position is top (0,0)
if ((event === null && x === 0 && y === 0) || (x === null || y === null)) { return; }
Expand All @@ -53,6 +63,23 @@ function recompute(event: UIEvent = null): void {
timeout = setTimeout(process, Setting.LookAhead, Event.Scroll);
}

function getPositionHash(x: number, y: number): string {
let node: Node;
if ("caretPositionFromPoint" in document) {
node = (document as any).caretPositionFromPoint(x, y)?.offsetNode;
} else if ("caretRangeFromPoint" in document) {
node = document.caretRangeFromPoint(x, y)?.startContainer;
}
if (!node) {
node = document.elementFromPoint(x, y) as Node;
}
if (node && node.nodeType === Node.TEXT_NODE) {
node = node.parentNode;
}

return dom.get(node)?.hash?.[1];
}

export function reset(): void {
state = [];
}
Expand Down
2 changes: 2 additions & 0 deletions packages/clarity-js/types/interaction.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ export interface ScrollData {
target: Target;
x: number;
y: number;
top: string;
bottom: string;
}

export interface SelectionData {
Expand Down
2 changes: 1 addition & 1 deletion packages/clarity-visualize/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clarity-visualize",
"version": "0.7.29",
"version": "0.7.30",
"description": "An analytics library that uses web page interactions to generate aggregated insights",
"author": "Microsoft Corp.",
"license": "MIT",
Expand Down

0 comments on commit 163047d

Please sign in to comment.