Arnaud73 / ngx-matomo

Matomo (aka. Piwik) web analytics for Angular applications
MIT License
102 stars 47 forks source link

Option to disable tracking when Angular is in dev mode #41

Closed matcho closed 2 years ago

matcho commented 4 years ago

Hi,

Could it be possible to add an option (or make it a default behaviour) that stops tracking when Angular app is in dev mode ? Of course all trackPageView() calls can be surrounded by a test if (! isDevMode()) but this impacts readability, and it seems quite natural not to track page views generated by the developer's work.

What makes me ask for this feature is the following issue I ran into: running e2e tests with Protractor triggers hundreds of Matomo events. In such a case, IP / hostname filtering in Matomo settings is not always a good solution because it requires extra attention and frequent adjustments (since IP may change, especially when working at home), and may also be unavailable to the developer depending on who runs Matomo.

Thanks for your consideration, Mathias

martinraie commented 3 years ago

This would be really useful feature. Previously (before 1.0.0-rc.0) I used following construct and it worked well:

    if (!isDevMode()) {
      this.matomoInjector.init(url, id);
    }

In 1.0.0-rc.0 init() is not called by my own code anymore and some other way to achieve same behavior would be nice.

Arnaud73 commented 3 years ago

Thanks for having given a try to the RC :-)

I understand that in certain situation you would like to disable tracking in dev mode, however, in many other situations, you might still want to have tracking, especially if you have a dedicated site id, or even a dedicated instance.

In your situation, I think I'd :

  1. add the the Matomo configuration to the environment object in the environment files, one for dev, one for production, e.g.:
    export const environment = {
    production: false,
    matomoConfig: {
      scriptUrl: '//some.server/matomo.js',
      trackers: [],
    }
    };
  2. pass this configuration at the initialization level. This would be something like MatomoModule.forRoot(environment.matomoConfig).

That sould cleanly do the trick.

I was thinking of raising an exception if no tracker was provided, but I guess I'm going to change my mind.

Please tell me if it works for you.

martinraie commented 3 years ago

With this setup matomo script is still initialized and tries to track pageviews and events etc. As it doesn't have correct tracking url, it just fails. Currently I solved the problem by creating an extra site in Matomo server just for development environments. It doesn't have any analytics value but at least everything works in local machines and doesn't throw errors.

Arnaud73 commented 3 years ago

OK, I'll check how to address nicely your issue very soon.

Arnaud73 commented 3 years ago

You can have a look at https://www.npmjs.com/package/ngx-matomo/v/1.0.0-rc.1

This new release is now built with Angular 12 (RC), but shall be a drop-in replacement.

You can have a look at the demo app (https://github.com/Arnaud73/ngx-matomo/tree/master/projects/demo), I've used several configurations, one of them disabling the tracking features.

Let me know if it works for you.

Arnaud73 commented 2 years ago

Release 1.0 finaly implemented what has been suggested in RC1 : if the Matomo script URL is not provided, then the tracker is in "dummy" mode.