matomo-org / matomo-nodejs-tracker

A Node.js wrapper for the Matomo (Piwik) tracking HTTP API
MIT License
117 stars 41 forks source link

allow custom ending, not only piwik.php or matomo.php #52

Closed blubbll closed 4 years ago

Toub commented 4 years ago

If I use a proxy to rename the URL, I should be able to use a different URL ending.

However, the client throws an error:

A tracker URL must end with "matomo.php" or "piwik.php"

I understand this can save a lot of time for users who provide a wrong URL.

So, what about providing an extra parameter to allow to provide any URL?

new MatomoTracker(1, 'https://tracker.my-site.com', {allowCustomUrl: true});
Findus23 commented 4 years ago

@Toub That sounds like a reasonable solution that would both still protect inexperienced users and allow custom URLs.

If you create a PR, I'll release a new version.

Toub commented 4 years ago

Looking at the code, this is already supported:

Constructor:

/**
 * @constructor
 * @param {Number} siteId     Id of the site you want to track
 * @param {String} trackerUrl URL of your Matomo instance
 * @param {Boolean} [true] noURLValidation Set to true if the `piwik.php` has been renamed
 */
function MatomoTracker (siteId, trackerUrl, noURLValidation) {
  // [...]
  if (!noURLValidation) {
    assert.ok(trackerUrl.endsWith('matomo.php') || trackerUrl.endsWith('piwik.php'), 'A tracker URL must end with "matomo.php" or "piwik.php"');
  }

Test:

  it('should allow invalid URL if noURLValidation is set', () => {
    (() => new MatomoTracker(1, 'http://example.com/index.php', true)).should.not.throw(/tracker/);
  });

l will send a PR with a simple documentation update.