From 19cac6cd376b0f26667e4a6a74d04208bf35006d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rn=20Ro=CC=88der?= Date: Mon, 11 Dec 2017 13:48:03 +0100 Subject: [PATCH 1/4] Release 0.12.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cbaa9ce..3627fc9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "piwik-react-router", - "version": "0.12.0", + "version": "0.12.1", "description": "Piwik analytics component for react-router", "keywords": [ "react", From 63537cfabc8598f822e76abfe1fb675f0cda7cf4 Mon Sep 17 00:00:00 2001 From: Florian Zeidler Date: Thu, 14 Jun 2018 10:37:31 +0200 Subject: [PATCH 2/4] handle initialization on documents without preexisting script tag (for example when testing) --- index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1c9f76f..60b7f6f 100644 --- a/index.js +++ b/index.js @@ -233,11 +233,18 @@ var PiwikTracker = function(opts) { var d=document; var g=d.createElement('script'); var 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 (s) { + s.parentNode.insertBefore(g,s) + } else { + var body=d.getElementsByTagName('body')[0]; + body.appendChild(g); + } } })(); From 73ea7f60a1852eee8ea99e49ff4c47bf9d3c345f Mon Sep 17 00:00:00 2001 From: Florian Zeidler Date: Fri, 15 Jun 2018 12:25:45 +0200 Subject: [PATCH 3/4] only add script tag to jsdom when needed --- test/client.tests.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/client.tests.js b/test/client.tests.js index 2721bb0..d628356 100644 --- a/test/client.tests.js +++ b/test/client.tests.js @@ -7,11 +7,7 @@ describe('piwik-react-router client tests', function () { let jsdomBody; beforeEach(() => { - // piwiks tracking client doesn't properly adds the script tag to jsdom or the other way around. - // As i won't modify the piwik loading script the easiest was to provide this hacky script tag. - // Dirty – i know ;) - jsdomBody = ''; - + jsdomBody = ''; this.jsdom = require('jsdom-global')(jsdomBody, { url: 'http://foo.bar' }); @@ -580,6 +576,9 @@ describe('piwik-react-router client tests', function () { it ('should warn about a missing siteId if opts.injectScript is disabled and the external piwik script is not properly initialized', () => { let warningSpy = sinon.spy(); + // add script tag for piwik initialization to find + let emptyScriptTag = document.createElement('script'); + document.getElementsByTagName('body')[0].appendChild(emptyScriptTag); // instantiating piwik (function() { @@ -603,6 +602,9 @@ describe('piwik-react-router client tests', function () { it ('should not warn about a missing siteId if opts.injectScript is disabled and the external piwik script is initialized', () => { let warningSpy = sinon.spy(); + // add script tag for piwik initialization to find + let emptyScriptTag = document.createElement('script'); + document.getElementsByTagName('body')[0].appendChild(emptyScriptTag); // instantiating piwik (function() { @@ -627,6 +629,9 @@ describe('piwik-react-router client tests', function () { it ('should not warn about a missing siteId if opts.injectScript is disabled and the external piwik script has replaced _paq with the TrackerProxy', () => { let warningSpy = sinon.spy(); let trackerProxySpy = sinon.spy(); + // add script tag for piwik initialization to find + let emptyScriptTag = document.createElement('script'); + document.getElementsByTagName('body')[0].appendChild(emptyScriptTag); // instantiating piwik (function() { From 4eaca033f3c984105c1ccd47dcfd75a9b05dbb74 Mon Sep 17 00:00:00 2001 From: Florian Zeidler Date: Fri, 15 Jun 2018 12:33:22 +0200 Subject: [PATCH 4/4] add initialization test with existing script tag --- test/client.tests.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/client.tests.js b/test/client.tests.js index d628356..dde3a53 100644 --- a/test/client.tests.js +++ b/test/client.tests.js @@ -562,6 +562,24 @@ describe('piwik-react-router client tests', function () { assert.isTrue(piwikScripts.length === 0); }); + it ('should inject piwik.js if another script object is already present', () => { + let emptyScriptTag = document.createElement('script'); + document.getElementsByTagName('head')[0].appendChild(emptyScriptTag); + + 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 warn about a missing siteId if opts.injectScript is disabled and the external piwik script is not initialized', () => { let warningSpy = sinon.spy(); const piwikReactRouter = testUtils.requireNoCache('../', {