AmazingDreams / vue-matomo

Vue plugin for Matomo Analytics
MIT License
278 stars 61 forks source link

<a :href="foo" … doesn't fire tracking call #82

Closed alxndrstffn closed 3 years ago

alxndrstffn commented 3 years ago

Hi,

I have a vue-2 component which receives its content from a REST-Call in a standard reactive pattern.

Part of that content is an external link to a user's profile:

<a :href="user.website" target="_blank">{{user.website}}</a>

I want to track clicks on that link, but for some reason, nothing gets tracked while url-changes get tracked.

And I want to refrain from adding something like this:

<a :href="user.website" target="_blank" @click="<manualTrackingCallThingy()">{{user.website}}</a>

Is there something I need to add in the initial configuration (I've used the one from the README and just changed the host property)

AmazingDreams commented 3 years ago

Check if enableLinkTracking is set to true, which it should be by default. I found that link tracking does not work properly for dynamic urls. I think matomo scans the page for a tags and adds click events. This was also an issue with navigating using the Vue router itself. I had to call enableLinkTracking on matomo after each navigation (https://github.com/AmazingDreams/vue-matomo/blob/master/src/index.js#L84)

I think that something similar is happening in your case. ie the router navigates, matomo scans for links, REST call completes and a new link tag is added which is then missed by matomo. You would have to do the same - add a call to this.$matomo.enableLinkTracking() after the links are rendered. See https://developer.matomo.org/guides/spa-tracking#link-tracking

alxndrstffn commented 3 years ago

Thank you very much @AmazingDreams.

Calling enableLinkTracking() after the component's state has been updated solved my issue.

AmazingDreams commented 3 years ago

I have added an additional note to the README. If you don't mind please let me know if this is clear https://github.com/AmazingDreams/vue-matomo/blob/master/README.md#note-on-external-link-tracking

alxndrstffn commented 3 years ago

I don't!

Maybe it would be helpful to add an examplary and prominent <a :href="externalUrl" …>-snippet, for clarity and less need for readers to mentally map the new note to their specific problem (e.g. in my case I didn't attribute the problem to the link being external but rather had a hunch that it might have todo with v-bind:href and some matomo-internal event-listener not being applied)