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

How to Cleanup trackForm/trackLink #1018

Open
william-will-angi opened this issue Dec 20, 2023 · 2 comments
Open

How to Cleanup trackForm/trackLink #1018

william-will-angi opened this issue Dec 20, 2023 · 2 comments

Comments

@william-will-angi
Copy link

william-will-angi commented Dec 20, 2023

I am trying to use the trackForm/trackLink utilities but am not sure how to integrate it them in a React app.

const LinkComponent = (props) => {
  const ref = useRef()
  useEffect(()=>{
    window.analytics.trackLink(ref.current, {
      linkText: props.text
    })
    return () => {
      // What should I put here?
    }
  }, [props.text])
  return <a ref={ref}>{props.text}</a>
}

My concern is making sure only one event listener is ever assigned to the link, so if one of the props was to change, it doesn't receive an additional event listener.

Edit: I verified that calling trackLink on the same element multiple times will cause multiple track events to be fired.

@silesky
Copy link
Contributor

silesky commented Jan 8, 2024

@william-will-angi thank you for taking out an issue.

Without diving in too deeply, trackLink is targeted at vanilla JS environments (and not used very much) -- is there a reason you can't use the regular "analytics.track"?

@william-will-angi
Copy link
Author

@silesky Thanks for the response.

We are currently using analytics.track, however in doing so we have to do logic similar to here to mimic the timeout functionality. It isn't terrible, but I was thinking it would be nice to re-use what already existed if possible. If trackLink is considered legacy now, we can continue using analytics.track

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

No branches or pull requests

2 participants