Skip to content
This repository was archived by the owner on Jun 20, 2025. It is now read-only.
Merged
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ see [http://davidwalsh.name/track-errors-google-analytics](http://davidwalsh.nam
By enabling `ignoreInitialVisit` it connects to the history without tracking the initial visit.


### injectScript: `true`

By disabling `injectScript` the piwik.js script will not be injected automatically to allow a separate loading.


### clientTrackerName: `'piwik.js'`

The name of the `piwik.js` static resource on the Piwik server. Set this option if the Piwik instance uses a different name.
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var PiwikTracker = function(opts) {
opts.enableLinkTracking = ((opts.enableLinkTracking !== undefined) ? opts.enableLinkTracking : true);
opts.updateDocumentTitle = ((opts.updateDocumentTitle !== undefined) ? opts.updateDocumentTitle : true);
opts.ignoreInitialVisit = ((opts.ignoreInitialVisit !== undefined) ? opts.ignoreInitialVisit : false);
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');

Expand Down Expand Up @@ -151,8 +152,10 @@ var PiwikTracker = function(opts) {
push(['enableLinkTracking']);
}

var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.defer=true; g.async=true; g.src=u+opts.clientTrackerName;
s.parentNode.insertBefore(g,s);
if (opts.injectScript) {
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.defer=true; g.async=true; g.src=u+'piwik.js';
s.parentNode.insertBefore(g,s);
}
})();

// return api
Expand Down
32 changes: 32 additions & 0 deletions test/client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,38 @@ describe('piwik-react-router client tests', function () {
});
});

describe('should correctly inject piwik script', () => {
it ('should inject piwik.js if opts.injectScript is enabled', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
siteId: 1,
injectScript: true
});

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 ('should not inject piwik.js if opts.injectScript is disabled', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
siteId: 1,
injectScript: false
});

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

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

it ('should correctly handle basename', () => {
const piwikReactRouter = testUtils.requireNoCache('../')({
url: 'foo.bar',
Expand Down