Skip to content

fix: spans inside buttons - #563

Merged
raquelmsmith merged 10 commits into
masterfrom
fix/spans-inside-buttons
Mar 2, 2023
Merged

raquelmsmith merged 10 commits into
masterfrom
fix/spans-inside-buttons

Conversation

@raquelmsmith

@raquelmsmith raquelmsmith commented Mar 2, 2023 •

Copy link
Copy Markdown
Member

Changes

Fixes PostHog/posthog#14387

If an anchor or button element had its text nested inside a <span> (eg <button href="#"><span>Some Text</span></button> then autocapture would not assign that text to the button when the button click was captured. So if in the example there, someone clicked on the button element but not directly on the span (because padding or something) then it would capture the button click, but someone's action for autocapture text contains Some Text would not get triggered.

With this change we now iterate through any children inside a and button elements, looking for spans, pull the text out of those spans, and assign them to the button text property.

❓ I'm not sure how impactful this change will be... I believe that this would be the expected behavior for autocapture, but it's possible that it could mess up some peoples' metrics.... How should we go about deciding how to release this?

...

Checklist

@github-actions

github-actions Bot commented Mar 2, 2023 •

Copy link
Copy Markdown
Contributor

Size Change: +2.53 kB (0%)

Total Size: 2.28 MB

Filename Size Change
dist/array.full.js 152 kB +632 B (0%)
dist/array.js 93.6 kB +632 B (+1%)
dist/es.js 93.4 kB +632 B (+1%)
dist/module.js 93.6 kB +632 B (+1%)
ℹ️ View Unchanged
Filename Size
dist/recorder-v2.js 92.5 kB
dist/recorder.js 58.3 kB
lib/rrweb/dist/plugins/console-record.js 26.6 kB
lib/rrweb/dist/plugins/console-record.min.js 10.8 kB
lib/rrweb/dist/plugins/console-replay.js 12.7 kB
lib/rrweb/dist/plugins/console-replay.min.js 5.72 kB
lib/rrweb/dist/plugins/sequential-id-record.js 910 B
lib/rrweb/dist/plugins/sequential-id-record.min.js 417 B
lib/rrweb/dist/plugins/sequential-id-replay.js 1.24 kB
lib/rrweb/dist/plugins/sequential-id-replay.min.js 578 B
lib/rrweb/dist/record/rrweb-record-pack.js 31.4 kB
lib/rrweb/dist/record/rrweb-record-pack.min.js 10.7 kB
lib/rrweb/dist/record/rrweb-record.js 140 kB
lib/rrweb/dist/record/rrweb-record.min.js 51.7 kB
lib/rrweb/dist/replay/rrweb-replay-unpack.js 188 kB
lib/rrweb/dist/replay/rrweb-replay-unpack.min.js 71.1 kB
lib/rrweb/dist/replay/rrweb-replay.js 172 kB
lib/rrweb/dist/replay/rrweb-replay.min.js 66.4 kB
lib/rrweb/dist/rrweb-all.js 356 kB
lib/rrweb/dist/rrweb-all.min.js 133 kB
lib/rrweb/dist/rrweb.js 302 kB
lib/rrweb/dist/rrweb.min.js 116 kB
lib/rrweb/es/rrweb/ext/base64-arraybuffer/dist/base64-arraybuffer.es5.js 1.94 kB
lib/rrweb/es/rrweb/ext/mitt/dist/mitt.es.js 1.99 kB

compressed-size-action

@raquelmsmith raquelmsmith changed the title Fix/spans inside buttons fix: spans inside buttons Mar 2, 2023
@raquelmsmith
raquelmsmith marked this pull request as ready for review March 2, 2023 05:00

@pauldambra pauldambra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me, I've not used yalc to run it, but I can see you edited those instructions so I'm assuming you did 🙌

I think that anyone who was relying on us not detecting spans can create an action to unify the old and new autocapture event if they need to. But this behaviour seems more correct to me so 👍

Comment thread package.json Resolved
Comment thread src/autocapture-utils.ts Resolved
@pauldambra

Copy link
Copy Markdown
Member

it could mess up some peoples' metrics.... How should we go about deciding how to release this?

I bet we could go spelunking for insights that use autocapture where those elements contain buttons that contain spans. But that feels like overkill, let's drop a heads-up in the community slack when we release it and can help anyone that needs the help

@raquelmsmith

raquelmsmith commented Mar 2, 2023 •

Copy link
Copy Markdown
Member Author

I realized that if someone clicked the span text inside the button that the element tree didn't include the span text on the button element. So if <button><span>Some Text</span></button> and someone clicked directly on <span>Some Text</span>, we wouldn't get an event for autocapture clicked link with text contains Some Text. So that should be fixed now too!

Comment thread src/autocapture-utils.ts Outdated
Comment on lines +341 to +342
if (target.children?.length > 0) {
for (const child of target?.children) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (target.children?.length > 0) {
for (const child of target?.children) {
if ((target?.children || []).length > 0) {
for (const child of target.children) {

nit picking, since we've got target.children on the line above we don't need target?.children here.

If target is undefined it'll fail on line 341.

(sorry, if my last nit-pick here dig a hole for you with IE 😅)

@raquelmsmith raquelmsmith Mar 2, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird because that change you suggested initially passed the IE test, but then failed subsequently, but reverting it made it pass again 🤷‍♀️ I'll update kinda as you suggest here (I'd argue that the way I did it is overall more readable esp since we can't call the func without a target) but if it fails after I make the commit it'll be your fault 😛

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤣

(I'd argue that the way I did it is overall more readable)

So, there meaning

if (target.children?.length > 0)

vs.

if ((target?.children || []).length > 0)

Yep, we're fighting JS (and I did say I was nit-picking so feel free to ignore)

Even though TS says target can't be undefined JS won't care so it can be. Which would mean target.children could fail. Before you get to the target.children?.length. A little over cautious but the SDK is a pain for hitting all the possible errors.

I guess you could have if(target && target.children?.length > 0) instead.

If it's not undefined then the spec says we definitely get children... But, JS is fun so I won't quibble over the children?.length not needing the elvis operator since it doesn't cost us anything.

(Going into detail in case I'm being stupid and this helps you figure out whether to ignore me :))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you're not being stupid, I guess I'm just being overly trusting of typescript haha. I hit the merge button too quick. I'll open another PR and it can go out with the next version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autocapture not capturing button label if the label is contained within a span

2 participants