Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track element's data attributes if present #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

davidalejandroaguilar
Copy link

@davidalejandroaguilar davidalejandroaguilar commented Mar 8, 2024

Description

This PR allows tracking an element's data attributes if they're present.

Motivation

Allow easier querying and segmenting of events. For example on 2 different pages:

<a href="/">Open Github</a>
<a href="/">Sign Up</a>

Adding an id and grouping identifier to them:

<a href="/" data-ahoy-track-id="open-github" data-ahoy-track-group="onboarding">Open Github</a>
<a href="/" data-ahoy-track-id="sign-up" data-ahoy-track-group="onboarding">Sign Up</a>

Will allow you to both:

  • Easily query for specific clicks on the "Open Github" link, even if the link text changes.
  • Group clicks belonging to the onboarding group.

Note that you can't rely on the id, because it might be used for some other JS and might change.

Also, Ahoy's data-section is pretty limiting, since you can only use it for one of the above things. That is, either identifying an event across text changes, or grouping multiple links/forms, but not both.

Example

Clicking the "Open Github" link above will provide:

{
  // ...
  properties: {
    tag: "a",
    data: {
      "ahoy-track-id": "open-github",
      "ahoy-track-group": "onboarding"
    },
    page: "/"
  }
}

Get the events for onboarding:

Ahoy::Event.where_props("data" => {"ahoy-track-group" => "onboarding"})

Get the events for clicks on the "Open Github" link, even if the text changed:

Ahoy::Event.where_props("data" => {"ahoy-track-id" => "open-github"})

This will work even if the event has other properties on the data attribute.

@davidalejandroaguilar davidalejandroaguilar force-pushed the davidalejandroaguilar/track-data-attributes branch from ff261d1 to 709ed40 Compare March 11, 2024 13:20
function eventProperties() {
return cleanObject({
tag: this.tagName.toLowerCase(),
id: presence(this.id),
"class": presence(this.className),
page: page(),
section: getClosest(this, "data-section")
section: getClosest(this, "data-section"),
data: getDatasetInKebabCase(this)

Choose a reason for hiding this comment

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

Suggested change
data: getDatasetInKebabCase(this)
data: this.dataset

https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

seams to be available accross browsers:
https://caniuse.com/?search=dataset

Choose a reason for hiding this comment

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

Hey there!

this.dataset returns camelCase, but I'm returning them in kebab-case, so that the event properties reflect exactly how the element had them written, minus the data prefix. e.g.

<div data-foo-bar="baz"></div>

Would be:

properties: {
  data: {
    "foo-bar": "baz"
  }
}

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.

2 participants