ngx-matomo-client icon indicating copy to clipboard operation
ngx-matomo-client copied to clipboard

How to not trigger trackPageView if I change query params in the url.

Open p-aron opened this issue 1 year ago • 5 comments

Hello. I change a query param in the url and it triggers a NavigationEnd in the angular router, and the MatomoTracker makes a trackPageView. Is there any way to filter out these events?

p-aron avatar Nov 15 '23 18:11 p-aron

Hi @p-aron

You can configure your Matomo server, see the official help.

But if you want to configure it client-side, I think there is no easy way to do what you want currently. But good news is it could be easily implemented!

Long answer : This feature can be split up:

  1. If I understand correctly, you want to strip all (or only some?) url parameters, and track the page url without params, eg. /url?param1&param2 would be tracked as /url. This is already feasible by providing a custom implementation for PageUrlProvider.
  2. Subsequent navigation events to the same url should be ignored and not tracked as a new page view. This is already the case BUT based on RouterEvent.urlAfterRedirect. This may be a bug, we should use the url provided by PageUrlProvider instead.

Short answer : I will think about the following:

  1. add a simple configuration option to strip url parameters (without the need to provide a custom PageUrlProvider implementation)
  2. track page view only after distinct url changes using the

This can probably be implemented soon, but it would be great if you would like to open a PR for one or each of those two points!

EmmanuelRoux avatar Nov 15 '23 18:11 EmmanuelRoux

Hi @p-aron, is your issue solved?

EmmanuelRoux avatar Dec 13 '23 23:12 EmmanuelRoux

hello, my case is 2. I found the problematic code in/matomo-router.service.ts:

this.router.events
      .pipe(
        // Take only NavigationEnd events
        filter(isNavigationEnd),
        // Filter out excluded urls
        filter(isNotExcluded(this.config.exclude)),
        // Distinct urls
        distinctUntilKeyChanged('urlAfterRedirects'),
        // Optionally add some delay
        delayOp,
        // Set default page title & url
        switchMap(event =>
          this.presetPageTitleAndUrl(event).pipe(map(({ pageUrl }) => ({ pageUrl, event }))),
        ),
        // Run interceptors then track page view
        concatMap(({ event, pageUrl }) =>
          this.callInterceptors(event).pipe(tap(() => this.trackPageView(pageUrl))),
        ),
      )

My understanding is that distinctUntilKeyChanged('urlAfterRedirects') is not working as the code writer expected. I can maybe do a fix later.

p-aron avatar Dec 14 '23 18:12 p-aron

@p-aron you're right, that was my point 2.

I will take a look on how to fix it, but don't hesitate to create a PR if you have some time! Thanks

EmmanuelRoux avatar Dec 14 '23 18:12 EmmanuelRoux

Next week I might have some time to understand the code better. Thank you very much!

p-aron avatar Dec 14 '23 19:12 p-aron

:tada: This issue has been resolved in version 6.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

EmmanuelRoux avatar May 28 '24 16:05 EmmanuelRoux