From 36338dfbd4cf4020189bb46411acaecb3407f261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rn=20Ro=CC=88der?= Date: Mon, 20 Mar 2017 10:32:18 +0100 Subject: [PATCH] fixed #30 --- README.md | 5 +++++ index.js | 6 ++++++ test/client.tests.js | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/README.md b/README.md index dd0b3a4..450672c 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,11 @@ By enabling this option occurring javascript errors will be tracked as a `JavaSc see [http://davidwalsh.name/track-errors-google-analytics](http://davidwalsh.name/track-errors-google-analytics) for further details +### ignoreInitialVisit: `false` + +By enabling `ignoreInitialVisit` it connects to the history without tracking the initial visit. + + ## API ### track (Location location) diff --git a/index.js b/index.js index e0fc0a3..44a4826 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,7 @@ var PiwikTracker = function(opts) { opts.trackErrors = ((opts.trackErrors !== undefined) ? opts.trackErrors : false); opts.enableLinkTracking = ((opts.enableLinkTracking !== undefined) ? opts.enableLinkTracking : true); opts.updateDocumentTitle = ((opts.updateDocumentTitle !== undefined) ? opts.updateDocumentTitle : true); + opts.ignoreInitialVisit = ((opts.ignoreInitialVisit !== undefined) ? opts.ignoreInitialVisit : false); if (!opts.url || !opts.siteId) { // Only return warning if this is not in the test environment as it can break the Tests/CI. @@ -98,6 +99,11 @@ var PiwikTracker = function(opts) { track(loc); }); + if (!opts.ignoreInitialVisit && history.location) { + console.log('tracking initial visit', history.location); + track(history.location); + } + return history; }; diff --git a/test/client.tests.js b/test/client.tests.js index 985671c..1480d7b 100644 --- a/test/client.tests.js +++ b/test/client.tests.js @@ -314,6 +314,31 @@ describe('piwik-react-router client tests', function () { [ 'trackPageView' ] ]); }); + + it ('should correctly track the initial visit if opts.ignoreInitialVisit is disabled', () => { + const piwikReactRouter = testUtils.requireNoCache('../')({ + url: 'foo.bar', + siteId: 1, + }); + + const unlistenFn = sinon.spy(); + let listenStub = sinon.stub().returns(unlistenFn); + + const history = { + listen: listenStub, + location: { + pathname: '/foo/bar.html', + search: '?foo=bar' + } + }; + + piwikReactRouter.connectToHistory(history); + + assert.includeDeepMembers(window._paq, [ + [ 'setCustomUrl', '/foo/bar.html?foo=bar' ], + [ 'trackPageView' ] + ]); + }); }); it ('should correctly handle basename', () => {