Andy
a year ago

Autocapture event bubbling

Hi all..

I just have a question that I couldn't find answer to in the docs and as a potential time saver on my side.

Let's say I have an element like so..

<a class="link" href="/hello-world">
<span class="link__text">Label text</span>
<svg class="link__icon".... </svg>
</a>

I wish to track autocapture events on this link.. Right now, I have an autocapture series filtered by css selector of

`.link[href="/hello-world"]'

This is fine, but I found that it wasn't capturing the true story.

I had to then add a custom action which included the following autocapture events css selectors to capture clicks on this link..

.link__text AND .link__icon

Is this approach correct or am I missing something? I was not sure if the autocapture CSS selector of `.link[href="/hello-world"]' would capture all child clicks and these would bubble up to this selector?

Sorry if my explanation isn't great

  • a year agoSolution

    Your approach is correct, and you're not missing anything. The autocapture functionality in PostHog is designed to be conservative to prevent capturing sensitive data(1). It captures clicks on specific HTML elements, including a tags, but it doesn't automatically capture clicks on child elements(1).

    When you use the CSS selector .link[href="/hello-world"], it will only capture clicks directly on the <a> element itself, not on its child elements like <span> or <svg>(1)(2).

    To capture clicks on all parts of your link, including the text and icon, your approach of creating a custom action with multiple match groups is the correct way to do it(1)(2). This allows you to capture clicks on the link itself as well as its child elements.

    Here's how you can set up your action:

    1. Create a new action(1)
    2. Add a match group for the link itself: .link[href="/hello-world"](2)
    3. Add another match group for the text: .link__text(2)
    4. Add a third match group for the icon: .link__icon(2)

    This setup will ensure that clicks on any part of your link are captured as a single action(1)(2).

    If you want to simplify your selector, you could consider adding a specific data attribute to your link for tracking purposes, like this:

    <a class="link" href="/hello-world" data-ph-capture-link>
    <span class="link__text">Label text</span>
    <svg class="link__icon"...</svg>
    </a>

    Then, you could use a single selector in your action: [data-ph-capture-link](2). This would capture clicks on the link and all its children.

    Remember, actions are retroactive, so this will apply to past events as well(1).

    Max AI's response was generated by Inkeep. Double-check for accuracy.

  • Andy
    Authora year ago

    Just feeding back here more visibly, these AI responses are just incredible! :)

PostHog.com doesn't use third party cookies - only a single in-house cookie.

No data is sent to a third party.

Ursula von der Leyen, President of the European Commission