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

Add custom click properties #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

mgk
Copy link

@mgk mgk commented Jun 11, 2019

Overview

This adds custom click properties if data-ahoy-click- prefixed attributes are present.

Notes

Considered but did not implement the following features, keeping things simple, but happy to add them if you think they make sense:

  • make the data- prefix configurable and default it as above: seemed a bit overkill
  • put the extra properties at the top, not nested under properties: I was mimicking Rails Ahoy here and didn't want to complicate the behavior with the possibility of name collisions

@mgk
Copy link
Author

mgk commented Jul 15, 2019

Hiya @ankane, any thoughts on this one?

@ankane
Copy link
Owner

ankane commented Jul 15, 2019

@mgk Thanks for the PR, but haven't had a chance to review yet. Will take a look next time I'm working on Ahoy.js.

@mgk
Copy link
Author

mgk commented Nov 1, 2019

Can this be included in the upcoming 0.3.5 release?

Michael Keirnan added 3 commits January 8, 2020 13:16
- DOM data attributes generally use dashes instead of underscores
  and rails ERB templates replace undescores with dashes

- update example in README to reflect this convention

- convert dashes to underscores in generated event properties JSON
  to be consistent with consistent with Ahoy JSON naming convention
@ankane
Copy link
Owner

ankane commented Jun 3, 2020

Hey @mgk, thanks for the PR, and sorry for the long delay. The idea and implementation look great. For naming, what do you think of data-ahoy-properties (or data-ahoy-props) for the JSON format and data-ahoy-prop-product-id for individual props so they don't overlap?

It might also be good to have an option to automatically typecast values for individual properties so they're not always strings (true and false converted to boolean, 123 converted to integer). Wanted to bring it up now in case it makes sense to have as the default. Let me know what you think.

let jsonAttribute = prefix + 'json';

if (element.hasAttribute(jsonAttribute)) {
return JSON.parse(element.getAttribute(jsonAttribute));
Copy link
Owner

Choose a reason for hiding this comment

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

Would be good to combine if both are set.

for (let i = 0; i < attributeNames.length; i++) {
let attributeName = attributeNames[i];
if (attributeName.startsWith(prefix)) {
let propertyName = attributeName.substring(prefix.length).replace("-", "_");
Copy link
Owner

Choose a reason for hiding this comment

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

👍

@mgk
Copy link
Author

mgk commented Jun 10, 2020

Hey @mgk, thanks for the PR, and sorry for the long delay. The idea and implementation look great. For naming, what do you think of data-ahoy-properties (or data-ahoy-props) for the JSON format and data-ahoy-prop-product-id for individual props so they don't overlap?

That makes sense. I'm neutral on the names. Do you prefer data-ahoy-properties or data-ahoy-props ?

It might also be good to have an option to automatically typecast values for individual properties so they're not always strings (true and false converted to boolean, 123 converted to integer). Wanted to bring it up now in case it makes sense to have as the default. Let me know what you think.

I think of the JSON data-ahoy-props as how you would specify a structure with datatypes. I'm somewhat opposed to automatic type conversion for the scalars. Doing that raises questions of what types to try in what order and how one could disable the conversion if for example they wanted "42" as a string. I originally though of doing something like data-ahoy-prop-foo="42" and data-ahoy-prop-foo-type="string", but that led me to JSON.

@juliancheal
Copy link

@mgk @ankane Hey both, is there anything I can do to help get this PR merged?

@mgk
Copy link
Author

mgk commented Apr 9, 2021

@juliancheal As it stands now, my group has been using my PR version for some time and it is working well for us.

Still happy to update this PR once questions above are answered. If the project owner feels the typecasting he suggested is important maybe you could add that.

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.

3 participants