Skip to content
This repository was archived by the owner on Jun 20, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ The name of the `piwik.js` static resource on the Piwik server. Set this option

The name of the `piwik.php` script on the Piwik server. Set this option if the Piwik instance uses a different name.

### optionalTrackers: []

Set a comma delimited list of Matomo servers urls for addTracker.

- The list of `url`s must match the order of urls in `optionalTrackersWebsiteId`.
- e.g. [https://asdf.bar/piwik.php, https://qwerty.bar/piwik.php]

By using this together with `optionalTrackersWebsiteId` you can track your analytics data into more than just one Matomo server or into multiple websites on the same Matomo server. See [Multiple Matomo trackers](https://developer.matomo.org/guides/tracking-javascript-guide#multiple-piwik-trackers)

### optionalTrackersWebsiteId: []

Set a comma delimited list of Matomo `siteId` for addTracker. Used together with `optionalTrackers`.

- The list of `siteId`s must match the order of urls in `optionalTrackers`.
- e.g. [1, 2]

## API

Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ var PiwikTracker = function(opts) {
opts.injectScript = ((opts.injectScript !== undefined) ? opts.injectScript : true);
opts.clientTrackerName = ((opts.clientTrackerName !== undefined) ? opts.clientTrackerName : 'piwik.js');
opts.serverTrackerName = ((opts.serverTrackerName !== undefined) ? opts.serverTrackerName : 'piwik.php');
opts.optionalTrackers = ((opts.optionalTrackers !== undefined) ? opts.optionalTrackers : []);
opts.optionalTrackersWebsiteId = ((opts.optionalTrackersWebsiteId !== undefined) ? opts.optionalTrackersWebsiteId : []);

var alreadyInitialized = piwikIsAlreadyInitialized();

Expand Down Expand Up @@ -219,6 +221,12 @@ var PiwikTracker = function(opts) {

push(['setSiteId', opts.siteId]);
push(['setTrackerUrl', u + opts.serverTrackerName]);

if (opts.optionalTrackers.length && opts.optionalTrackersWebsiteId.length ) {
for (var i in opts.optionalTrackers) {
push(['addTracker', opts.optionalTrackers[i], opts.optionalTrackersWebsiteId[i]]);
}
}
}

if (opts.userId) {
Expand Down
51 changes: 51 additions & 0 deletions test/client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,57 @@ describe('piwik-react-router client tests', function () {
]);
});

it ('should correctly push the addTracker to the _paq array on instantiation if optionalTrackers and optionalTrackersWebsiteId are given', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
siteId: 1,
enableLinkTracking: true,
optionalTrackers: ['http://bar.bar/piwik.php'],
optionalTrackersWebsiteId: [2]
});

assert.sameDeepMembers(window._paq, [
[ 'setSiteId', 1 ],
[ 'setTrackerUrl', 'http://foo.bar/piwik.php' ],
[ 'enableLinkTracking' ],
[ 'addTracker', 'http://bar.bar/piwik.php', 2]
]);
});

it ('should correctly push multiple addTrackers to the _paq array on instantiation if optionalTrackers and optionalTrackersWebsiteId are given with multiple values', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
siteId: 1,
enableLinkTracking: true,
optionalTrackers: ['http://bar.bar/piwik.php', 'http://qwerty.bar/piwik.php'],
optionalTrackersWebsiteId: [1, 2]
});

assert.sameDeepMembers(window._paq, [
[ 'setSiteId', 1 ],
[ 'setTrackerUrl', 'http://foo.bar/piwik.php' ],
[ 'enableLinkTracking' ],
[ 'addTracker', 'http://bar.bar/piwik.php', 1],
[ 'addTracker', 'http://qwerty.bar/piwik.php', 2]
]);
});

it ('should not push addTracker to the _paq array on instantiation if optionalTrackers and optionalTrackersWebsiteId are not given', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
siteId: 1,
enableLinkTracking: true,
optionalTrackers: [],
optionalTrackersWebsiteId: []
});

assert.sameDeepMembers(window._paq, [
[ 'setSiteId', 1 ],
[ 'setTrackerUrl', 'http://foo.bar/piwik.php' ],
[ 'enableLinkTracking' ]
]);
});

it ('should correctly push the userId on instantiation', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
Expand Down