Skip to content
This repository was archived by the owner on Jun 20, 2025. It is now read-only.

Commit 8fcb9ac

Browse files
committed
handling the replacement of _paq with piwiks TrackerProxy
1 parent d60f209 commit 8fcb9ac

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ var PiwikTracker = function(opts) {
5555

5656
var hasSiteId = false;
5757
var hasTrackerUrl = false;
58+
59+
if (window._paq.length === undefined) {
60+
// piwik seems to have replaced the array by it's TrackerProxy
61+
// @see https://github.com/piwik/piwik/blob/3.x-dev/js/piwik.js#L7050
62+
// @see https://github.com/piwik/piwik/blob/3.x-dev/js/piwik.js#L7115
63+
// we should be good to go.
64+
return true;
65+
}
66+
5867
for (var j = 0, l = window._paq.length; j < l; j++) {
5968
if (~window._paq[j].indexOf('setSiteId')) {
6069
hasSiteId = true

test/client.tests.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,36 @@ describe('piwik-react-router client tests', function () {
622622
});
623623

624624
assert.isFalse(warningSpy.called);
625+
});
626+
627+
it ('should not warn about a missing siteId if opts.injectScript is disabled and the external piwik script has replaced _paq with the TrackerProxy', () => {
628+
let warningSpy = sinon.spy();
629+
let trackerProxySpy = sinon.spy();
625630

631+
// instantiating piwik
632+
(function() {
633+
var u='http://foo.bar/';
634+
635+
// simulate piwiks TrackerProxy replacement
636+
// @see https://github.com/piwik/piwik/blob/3.x-dev/js/piwik.js#L7115
637+
window._paq = {
638+
push: trackerProxySpy
639+
};
640+
window._paq.push(['setTrackerUrl', u+'piwik.php']);
641+
window._paq.push(['setSiteId', 1]);
642+
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
643+
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
644+
})();
645+
646+
const piwikReactRouter = testUtils.requireNoCache('../', {
647+
'warning': warningSpy
648+
})({
649+
url: 'foo.bar',
650+
injectScript: false
651+
});
652+
653+
assert.isFalse(warningSpy.called);
654+
assert.isTrue(trackerProxySpy.called);
626655
});
627656

628657
it ('should not inject piwik.js twice if multiple piwicReactRouter intances are created', () => {

0 commit comments

Comments
 (0)