From f642eec04714acbb739d6d0660e12d9bab85a725 Mon Sep 17 00:00:00 2001 From: Robin North Date: Wed, 24 Jan 2018 11:26:13 +0000 Subject: [PATCH] Preserve ability to disable analytics behavior, explicitly document this option --- README.md | 7 ++++--- lib/proto/parse-options.js | 16 ++++++++------- tests/lib/proto/parse-options.js | 34 +++++++++++++------------------- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 961305d..5bd22e3 100644 --- a/README.md +++ b/README.md @@ -371,12 +371,14 @@ Enable pushState. Only disable if you are crazy. Internaly, this option is used when `popstate` is used (to not pushState again). You should forget that option. -##### `analytics` (Function, default to a function that push `_gaq` `trackPageview` or send `ga` `pageview` +##### `analytics` (Function|Boolean, default to a function that pushes `_gaq` `_trackPageview` or sends `ga` `pageview` -Function that allow you to add behavior for analytics. By default it try to track +Function that allows you to add behavior for analytics. By default it tries to track a pageview with Google Analytics. It's called every time a page is switched, even for history buttons. +Set to `false` to disable this behavior. + ##### `scrollTo` (Integer, default to 0) Value (in px) to scrollTo when a page is switched. @@ -543,4 +545,3 @@ Clone this repository and run `npm run example`, which will open the example app ## [CHANGELOG](CHANGELOG.md) ## [LICENSE](LICENSE) - diff --git a/lib/proto/parse-options.js b/lib/proto/parse-options.js index 116476f..3679cb6 100644 --- a/lib/proto/parse-options.js +++ b/lib/proto/parse-options.js @@ -9,14 +9,16 @@ module.exports = function(options) { this.options.switches = this.options.switches || {} this.options.switchesOptions = this.options.switchesOptions || {} this.options.history = this.options.history || true - this.options.analytics = (typeof this.options.analytics === "function") ? this.options.analytics : function() { - if (window._gaq) { - _gaq.push(["_trackPageview"]) + this.options.analytics = (typeof this.options.analytics === "function" || this.options.analytics === false) ? + this.options.analytics : + function() { + if (window._gaq) { + _gaq.push(["_trackPageview"]) + } + if (window.ga) { + ga("send", "pageview", {page: location.pathname, title: document.title}) + } } - if (window.ga) { - ga("send", "pageview", {page: location.pathname, title: document.title}) - } - } this.options.scrollTo = (typeof this.options.scrollTo === "undefined") ? 0 : this.options.scrollTo; this.options.cacheBust = (typeof this.options.cacheBust === "undefined") ? true : this.options.cacheBust this.options.debug = this.options.debug || false diff --git a/tests/lib/proto/parse-options.js b/tests/lib/proto/parse-options.js index 8f57a65..3c4a4b3 100644 --- a/tests/lib/proto/parse-options.js +++ b/tests/lib/proto/parse-options.js @@ -34,26 +34,20 @@ tape("test parse initalization options function", function(t) { var options1 = {}; parseOptions.apply(body1, [options1]); - t.deepEqual(body1.options.elements, "a[href], form[action]"); - t.deepEqual(body1.options.selectors.length, 2, "selectors length"); - t.deepEqual(body1.options.selectors[0], "title"); - t.deepEqual(body1.options.selectors[1], ".js-Pjax"); - - t.deepEqual(isObjLiteral(body1.options.switches), true); - t.deepEqual(enumerableKeys(body1.options.switches), 2);// head and body - - t.deepEqual(isObjLiteral(body1.options.switchesOptions), true); - t.deepEqual(enumerableKeys(body1.options.switchesOptions), 0); - - t.deepEqual(body1.options.history, true); - - // TODO analytics is a little weird right now - t.deepEqual(typeof body1.options.analytics, "function"); - - t.deepEqual(body1.options.scrollTo, 0); - t.deepEqual(body1.options.cacheBust, true); - t.deepEqual(body1.options.debug, false); - t.deepEqual(body1.options.scrollRestoration, true) + t.equal(body1.options.elements, "a[href], form[action]"); + t.equal(body1.options.selectors.length, 2, "selectors length"); + t.equal(body1.options.selectors[0], "title"); + t.equal(body1.options.selectors[1], ".js-Pjax"); + t.equal(isObjLiteral(body1.options.switches), true); + t.equal(enumerableKeys(body1.options.switches), 2);// head and body + t.equal(isObjLiteral(body1.options.switchesOptions), true); + t.equal(enumerableKeys(body1.options.switchesOptions), 0); + t.equal(body1.options.history, true); + t.equal(typeof body1.options.analytics, "function"); + t.equal(body1.options.scrollTo, 0); + t.equal(body1.options.scrollRestoration, true); + t.equal(body1.options.cacheBust, true); + t.equal(body1.options.debug, false); t.end(); });