EmmanuelRoux / ngx-matomo-client

Matomo analytics client for Angular applications
https://matomo.org/
MIT License
74 stars 16 forks source link

Add possibility to provide a custom tracker class #78

Open christophercr opened 11 months ago

christophercr commented 11 months ago

First of all thanks a lot for such a useful lib! Works like a charm! 👏

I would like to use the Matomo Tag Manager and I also need to "push" other data into the _mtm variable. Perfhaps this is out of the scope of ngx-matomo and maybe what I'm going to explain below is not feasible, but who knows 😛

Currently there is no way to perform custom logic nor pushing custom data into the _mtm variable because there are only 2 trackers to use in the library: NoopMatomoTracker and StandardMatomoTracker. The latter as its name suggests, works out of the box with a standard Motomo traking workflow and uses the _paq variable.

image

I think this could be enhanced by being able to provide a custom Matomo tracker class, via the config for example, so that such class can be instantiated instead of the Noop or Standard ones.

export function createMatomoTracker(
  config: InternalMatomoConfiguration,
  platformId: Object,
  ngZone: NgZone,
): MatomoTracker {
  if (config.disabled || !isPlatformBrowser(platformId)) {
     return new NoopMatomoTracker();
  }

  return config.customTrackerClass;
    ? new config.customTrackerClass(ngZone, config)
    : new StandardMatomoTracker(ngZone, config);
}

From then on, everything should be fairly simple for the developer since the tracker class should extend the MatomoTracker abstract class as the other 2 trackers. This way any custom logic could be placed in the push() for example or in any trackXXXX() method if needed.

Not really sure if this would fit in ngx-matomo in its current state cause what I actually need is to customize data to be sent to the Tag Manager and trigger some other logic in my own app whenever such events are sent.

What do yo think?

EmmanuelRoux commented 11 months ago

Hi @christophercr Thank you for your message and the suggestion!

Actually this library was not originally designed to work specifically with Tag Manager, but I think it would be a great idea to provide better integration indeed!

Your suggestion seems good. I would just suggest to use dependency injection instead of instantiate a class directly.

Feel free to open a PR so we can work on it!