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

Commit 4d030c3

Browse files
committed
updated external piwik detection to allow instanitation without siteId or url if injectScript is disabled #46
1 parent 9a87e15 commit 4d030c3

File tree

2 files changed

+86
-10
lines changed

2 files changed

+86
-10
lines changed

index.js

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,50 @@ var PiwikTracker = function(opts) {
2121
return process && process.env && process.env.NODE_ENV ? process.env.NODE_ENV.toLowerCase() : 'development';
2222
};
2323

24+
var getBaseUrl = function () {
25+
var u = ''
26+
var url = opts.url || window.location.hostname;
27+
28+
if (url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1) {
29+
u = url + '/';
30+
}
31+
else {
32+
u = (('https:' == document.location.protocol) ? 'https://' + url + '/' : 'http://' + url + '/');
33+
}
34+
35+
return u;
36+
}
37+
2438
var piwikIsAlreadyInitialized = function () {
2539
var scripts = document.getElementsByTagName('script');
40+
var scriptUrl = getBaseUrl() + opts.clientTrackerName;
41+
42+
var found = false;
2643

2744
for (var i = 0, n = scripts.length; i < n; i++) {
28-
if (scripts[i].getAttribute('data-' + opts.piwikScriptDataAttribute) !== null) {
29-
return true;
45+
if (scripts[i].getAttribute('src') === scriptUrl) {
46+
found = true;
47+
break;
48+
}
49+
}
50+
51+
if (!found) {
52+
return false;
53+
}
54+
55+
var hasSiteId = false;
56+
var hasTrackerUrl = false;
57+
for (var j = 0, l = window._paq.length; j < l; j++) {
58+
if (~window._paq[j].indexOf('setSiteId')) {
59+
hasSiteId = true
60+
}
61+
62+
if (~window._paq[j].indexOf('setTrackerUrl')) {
63+
hasTrackerUrl = true
64+
}
65+
66+
if (hasTrackerUrl && hasSiteId) {
67+
return true
3068
}
3169
}
3270

@@ -44,7 +82,15 @@ var PiwikTracker = function(opts) {
4482
opts.serverTrackerName = ((opts.serverTrackerName !== undefined) ? opts.serverTrackerName : 'piwik.php');
4583
opts.piwikScriptDataAttribute = ((opts.piwikScriptDataAttribute !== undefined) ? opts.piwikScriptDataAttribute : 'piwik-react-router');
4684

47-
if ((!opts.url || !opts.siteId) && !piwikIsAlreadyInitialized()) {
85+
window._paq = window['_paq'] || [];
86+
87+
88+
var alreadyInitialized = piwikIsAlreadyInitialized();
89+
var piwikWithoutUrlOrSiteId = (!opts.url || !opts.siteId) && !alreadyInitialized;
90+
var piwikWithoutInjectScript = !opts.injectScript && !alreadyInitialized;
91+
92+
93+
if (piwikWithoutUrlOrSiteId || piwikWithoutInjectScript) {
4894
// Only return warning if this is not in the test environment as it can break the Tests/CI.
4995
if (getEnvironment() !== 'test') {
5096
warning(null, 'PiwikTracker cannot be initialized! You haven\'t passed a url and siteId to it.');
@@ -53,8 +99,6 @@ var PiwikTracker = function(opts) {
5399
return apiShim;
54100
}
55101

56-
window._paq = window['_paq'] || [];
57-
58102
/**
59103
* Adds a page view for the given location
60104
*/
@@ -165,11 +209,7 @@ var PiwikTracker = function(opts) {
165209
var alreadyInitialized = piwikIsAlreadyInitialized();
166210

167211
if (!alreadyInitialized) {
168-
if (opts.url.indexOf('http://') !== -1 || opts.url.indexOf('https://') !== -1) {
169-
var u = opts.url + '/';
170-
} else {
171-
var u = (('https:' == document.location.protocol) ? 'https://' + opts.url + '/' : 'http://' + opts.url + '/');
172-
}
212+
var u = getBaseUrl()
173213

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

test/client.tests.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,42 @@ describe('piwik-react-router client tests', function () {
547547
assert.isTrue(piwikScripts.length === 0);
548548
});
549549

550+
it ('should warn about a missing siteId if opts.injectScript is disabled and the external piwik script is not initialized', () => {
551+
let warningSpy = sinon.spy();
552+
const piwikReactRouter = testUtils.requireNoCache('../', {
553+
'warning': warningSpy
554+
})({
555+
url: 'foo.bar',
556+
injectScript: false
557+
});
558+
559+
assert.isTrue(warningSpy.called);
560+
});
561+
562+
it ('should not warn about a missing siteId if opts.injectScript is disabled and the external piwik script is initialized', () => {
563+
let warningSpy = sinon.spy();
564+
565+
// instantiating piwik
566+
(function() {
567+
var u='http://foo.bar/';
568+
var _paq = _paq || [];
569+
_paq.push(['setTrackerUrl', u+'piwik.php']);
570+
_paq.push(['setSiteId', 1]);
571+
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
572+
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
573+
})();
574+
575+
const piwikReactRouter = testUtils.requireNoCache('../', {
576+
'warning': warningSpy
577+
})({
578+
url: 'foo.bar',
579+
injectScript: false
580+
});
581+
582+
assert.isFalse(warningSpy.called);
583+
584+
});
585+
550586
it ('should not inject piwik.js twice if multiple piwicReactRouter intances are created', () => {
551587
const piwikReactRouter = testUtils.requireNoCache('../')({
552588
url: 'foo.bar',

0 commit comments

Comments
 (0)