matomo-org / matomo

Empowering People Ethically with the leading open source alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites & apps and visualise this data and extract insights. Privacy is built-in. Liberating Web Analytics. Star us on Github? +1. And we love Pull Requests!
https://matomo.org/
GNU General Public License v3.0
19.65k stars 2.62k forks source link

Better support for dynamic link tracking #7477

Open kevinoid opened 9 years ago

kevinoid commented 9 years ago

Hello Piwik Developers,

I am using Piwik on a website with a frontend written in AngularJS and finding it very difficult to do link tracking with Piwik. The source of the difficulty is that enableLinkTracking only tracks links which exist and have an href property at the moment Piwik is initialized, which can be before or after Angular initialization, depending on script load timing. Although this can be combined or replaced with addListener on individual link elements, the lack of ability to query which elements are currently tracked and the dynamic nature of frameworks like Angular which often create and destroy elements can make this quite difficult and cumbersome.

I am curious if you have considered replacing the per-link event listener with a single event listener on the html element which can listen for click events as they bubble. This way all links, dynamic and static, could be tracked without additional work by the user. There would be a performance cost, since each click event would cause DOM traversal to check if the click was on an a or area element, but the cost seems reasonable for most uses. Alternatively, this could be avoided by creating a enableDynamicLinkTracking method to require users to opt-in to this behavior.

What do you think? If this approach seems reasonable, I'd be happy to send a PR. If not, perhaps alternative approaches for improved support of dynamic link tracking could be considered?

Thanks for your time and consideration, Kevin

mattab commented 9 years ago

Hi @kevinoid

This sounds interesting, especially if you propose the working pull request :+1:

Regarding speed: I guess listening to all clicks is costly, in case user clicks a lot like in some games, or when the DOM tree is huge and it's costly to know where we've clicked. we could put "safe nets" to skip some clicks if clickrate is high...

Maybe others will have some feedback as well!

kevinoid commented 9 years ago

Hi @mattab

Thanks for the encouragement, I'll give it a shot.

Would you prefer the initial PR to apply the new behavior to the existing enableLinkTracking method (with or without the "safe nets" you mentioned), or should I create a new method such as enableDynamicLinkTracking? Obviously it can be changed later, just want to start as close as possible to the goal.

mattab commented 9 years ago

good to hear.

you can create enableDynamicLinkTracking and then we can easily call it from enableLinkTracking or not

mattab commented 9 years ago

we'll be very happy to review your pull request if/when you create one :+1:

kevinoid commented 9 years ago

Hey @mattab, thanks for the ping. Sorry about the delay, I had some unexpected priority changes on my current project which set me back a bit. I'm planning to have the first-draft PR ready in the next day or two, but if I don't, feel free to ping me again. It's still a priority for me!

tomalex0 commented 7 years ago

Curios to know if this feature is available now? @mattab @kevinoid

kevinoid commented 7 years ago

Good question @tomalex0. I left the project which needed this feature shortly after my last comment and I never got around to sending the PR. I'm not aware of any other work on the issue. I'll look around to see if I still have the prototype code I was working on and if it might still be useful.

mattab commented 7 years ago

@kevinoid as the issue is still opened we haven't implemented it yet. It would be great if you can create a pull request for this improvement :+1:

tomalex0 commented 7 years ago

@mattab , i'm using piwik in angular2 application, and i'm going to write a directive to address this issue for the mean time.

would like to know how you differentiate outlinks and download and how application urls are excluded from the outlinks and download