Skip to content

Commit

Permalink
added dynamic "already initialized" handling without the need to set …
Browse files Browse the repository at this point in the history
…a flag manually.

heavily inspired by #42. closes #41
  • Loading branch information
joernroeder committed Nov 15, 2017
1 parent 095cd25 commit e7b671c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
35 changes: 27 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ var PiwikTracker = function(opts) {
return process && process.env && process.env.NODE_ENV ? process.env.NODE_ENV.toLowerCase() : 'development';
};

var piwikIsAlreadyInitialized = function () {
var scripts = document.getElementsByTagName('script');

for (var i = 0, n = scripts.length; i < n; i++) {
if (scripts[i].getAttribute('data-' + opts.piwikScriptDataAttribute) !== null) {
return true;
}
}

return false;
};

opts = opts || {};
opts.trackErrors = ((opts.trackErrors !== undefined) ? opts.trackErrors : false);
opts.trackErrorHandler = ((opts.trackErrorHandler !== undefined) ? opts.trackErrorHandler : trackError);
Expand All @@ -30,8 +42,9 @@ 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.piwikScriptDataAttribute = ((opts.piwikScriptDataAttribute !== undefined) ? opts.piwikScriptDataAttribute : 'piwik-react-router');

if (!opts.url || !opts.siteId) {
if ((!opts.url || !opts.siteId) && !piwikIsAlreadyInitialized()) {
// Only return warning if this is not in the test environment as it can break the Tests/CI.
if (getEnvironment() !== 'test') {
warning(null, 'PiwikTracker cannot be initialized! You haven\'t passed a url and siteId to it.');
Expand Down Expand Up @@ -149,14 +162,19 @@ var PiwikTracker = function(opts) {

// piwik initializer
(function() {
if (opts.url.indexOf('http://') !== -1 || opts.url.indexOf('https://') !== -1) {
var u = opts.url + '/';
} else {
var u = (('https:' == document.location.protocol) ? 'https://' + opts.url + '/' : 'http://' + opts.url + '/');
var alreadyInitialized = piwikIsAlreadyInitialized();

if (!alreadyInitialized) {
if (opts.url.indexOf('http://') !== -1 || opts.url.indexOf('https://') !== -1) {
var u = opts.url + '/';
} else {
var u = (('https:' == document.location.protocol) ? 'https://' + opts.url + '/' : 'http://' + opts.url + '/');
}

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

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

if (opts.userId) {
push(['setUserId', opts.userId]);
Expand All @@ -166,7 +184,7 @@ var PiwikTracker = function(opts) {
push(['enableLinkTracking']);
}

if (opts.injectScript) {
if (opts.injectScript && !alreadyInitialized) {
var d = document;
var g = d.createElement('script');
var s = d.getElementsByTagName('script')[0];
Expand All @@ -175,6 +193,7 @@ var PiwikTracker = function(opts) {
g.defer = true;
g.async = true;
g.src = u + opts.clientTrackerName;
g.setAttribute('data-' + opts.piwikScriptDataAttribute, opts.siteId);

s.parentNode.insertBefore(g, s);
}
Expand Down
61 changes: 61 additions & 0 deletions test/client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,67 @@ describe('piwik-react-router client tests', function () {

assert.isTrue(piwikScripts.length === 0);
});

it ('should not inject piwik.js twice if multiple piwicReactRouter intances are created', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
siteId: 1
});

const piwikReactRouter2 = testUtils.requireNoCache('../')({
url: 'foo.bar',
siteId: 1
});

var allScripts = [].slice.call(window.document.scripts);
var piwikScripts = allScripts.filter((script) => {
return script.src.indexOf('piwik.js') !== -1;
});

assert.isTrue(piwikScripts.length === 1);
});

it ('it should correctly setup a second piwik-react-router instance without url and siteId', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
siteId: 1
});

const piwikReactRouter2 = testUtils.requireNoCache('../')();
});

it ('it should correctly store the string representation of siteId in the data-piwik-react-router attribute of the script', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
siteId: 100
});

const piwikReactRouter2 = testUtils.requireNoCache('../')();

var allScripts = [].slice.call(window.document.scripts);
var piwikScripts = allScripts.filter((script) => {
return script.src.indexOf('piwik.js') !== -1;
});

assert.isTrue(piwikScripts[0].getAttribute('data-piwik-react-router') === '100');
});

it ('should correctly use the given piwikScriptDataAttribute option.', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
siteId: 100,
piwikScriptDataAttribute: 'foobar'
});

const piwikReactRouter2 = testUtils.requireNoCache('../')();

var allScripts = [].slice.call(window.document.scripts);
var piwikScripts = allScripts.filter((script) => {
return script.src.indexOf('piwik.js') !== -1;
});

assert.isTrue(piwikScripts[0].getAttribute('data-foobar') === '100');
})
});

it ('should correctly handle basename', () => {
Expand Down

0 comments on commit e7b671c

Please sign in to comment.