Merge pull request #113 from MoOx/cleanup/analytics
Cleanup default analytics function
This commit was merged in pull request #113.
This commit is contained in:
@@ -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).
|
Internaly, this option is used when `popstate` is used (to not pushState again).
|
||||||
You should forget that option.
|
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.
|
a pageview with Google Analytics.
|
||||||
It's called every time a page is switched, even for history buttons.
|
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)
|
##### `scrollTo` (Integer, default to 0)
|
||||||
|
|
||||||
Value (in px) to scrollTo when a page is switched.
|
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)
|
## [CHANGELOG](CHANGELOG.md)
|
||||||
|
|
||||||
## [LICENSE](LICENSE)
|
## [LICENSE](LICENSE)
|
||||||
|
|
||||||
|
|||||||
2
index.js
2
index.js
@@ -279,7 +279,9 @@ Pjax.prototype = {
|
|||||||
// Fire Events
|
// Fire Events
|
||||||
trigger(document,"pjax:complete pjax:success", state.options)
|
trigger(document,"pjax:complete pjax:success", state.options)
|
||||||
|
|
||||||
|
if (typeof state.options.analytics === "function") {
|
||||||
state.options.analytics()
|
state.options.analytics()
|
||||||
|
}
|
||||||
|
|
||||||
if (state.options.history) {
|
if (state.options.history) {
|
||||||
// First parse url and check for hash to override scroll
|
// First parse url and check for hash to override scroll
|
||||||
|
|||||||
@@ -3,16 +3,14 @@
|
|||||||
var defaultSwitches = require("../switches")
|
var defaultSwitches = require("../switches")
|
||||||
|
|
||||||
module.exports = function(options) {
|
module.exports = function(options) {
|
||||||
this.options = options
|
options.elements = options.elements || "a[href], form[action]"
|
||||||
this.options.elements = this.options.elements || "a[href], form[action]"
|
options.selectors = options.selectors || ["title", ".js-Pjax"]
|
||||||
this.options.selectors = this.options.selectors || ["title", ".js-Pjax"]
|
options.switches = options.switches || {}
|
||||||
this.options.switches = this.options.switches || {}
|
options.switchesOptions = options.switchesOptions || {}
|
||||||
this.options.switchesOptions = this.options.switchesOptions || {}
|
options.history = options.history || true
|
||||||
this.options.history = this.options.history || true
|
options.analytics = (typeof options.analytics === "function" || options.analytics === false) ?
|
||||||
this.options.analytics = this.options.analytics || function() {
|
options.analytics :
|
||||||
// options.backward or options.foward can be true or undefined
|
function() {
|
||||||
// by default, we do track back/foward hit
|
|
||||||
// https://productforums.google.com/forum/#!topic/analytics/WVwMDjLhXYk
|
|
||||||
if (window._gaq) {
|
if (window._gaq) {
|
||||||
_gaq.push(["_trackPageview"])
|
_gaq.push(["_trackPageview"])
|
||||||
}
|
}
|
||||||
@@ -20,23 +18,22 @@ module.exports = function(options) {
|
|||||||
ga("send", "pageview", {page: location.pathname, title: document.title})
|
ga("send", "pageview", {page: location.pathname, title: document.title})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.options.scrollTo = (typeof this.options.scrollTo === "undefined") ? 0 : this.options.scrollTo;
|
options.scrollTo = (typeof options.scrollTo === "undefined") ? 0 : options.scrollTo;
|
||||||
this.options.cacheBust = (typeof this.options.cacheBust === "undefined") ? true : this.options.cacheBust
|
options.scrollRestoration = (typeof options.scrollRestoration !== "undefined") ? options.scrollRestoration : true
|
||||||
this.options.debug = this.options.debug || false
|
options.cacheBust = (typeof options.cacheBust === "undefined") ? true : options.cacheBust
|
||||||
this.options.timeout = this.options.timeout || 0
|
options.debug = options.debug || false
|
||||||
this.options.scrollRestoration = (typeof this.options.scrollRestoration !== "undefined") ? this.options.scrollRestoration : true
|
options.timeout = options.timeout || 0
|
||||||
|
|
||||||
// we can’t replace body.outerHTML or head.outerHTML
|
// we can’t replace body.outerHTML or head.outerHTML
|
||||||
// it create a bug where new body or new head are created in the dom
|
// it create a bug where new body or new head are created in the dom
|
||||||
// if you set head.outerHTML, a new body tag is appended, so the dom get 2 body
|
// if you set head.outerHTML, a new body tag is appended, so the dom get 2 body
|
||||||
// & it break the switchFallback which replace head & body
|
// & it break the switchFallback which replace head & body
|
||||||
if (!this.options.switches.head) {
|
if (!options.switches.head) {
|
||||||
this.options.switches.head = defaultSwitches.switchElementsAlt
|
options.switches.head = defaultSwitches.switchElementsAlt
|
||||||
}
|
}
|
||||||
if (!this.options.switches.body) {
|
if (!options.switches.body) {
|
||||||
this.options.switches.body = defaultSwitches.switchElementsAlt
|
options.switches.body = defaultSwitches.switchElementsAlt
|
||||||
}
|
|
||||||
if (typeof options.analytics !== "function") {
|
|
||||||
options.analytics = function() {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.options = options
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,26 +34,20 @@ tape("test parse initalization options function", function(t) {
|
|||||||
var options1 = {};
|
var options1 = {};
|
||||||
parseOptions.apply(body1, [options1]);
|
parseOptions.apply(body1, [options1]);
|
||||||
|
|
||||||
t.deepEqual(body1.options.elements, "a[href], form[action]");
|
t.equal(body1.options.elements, "a[href], form[action]");
|
||||||
t.deepEqual(body1.options.selectors.length, 2, "selectors length");
|
t.equal(body1.options.selectors.length, 2, "selectors length");
|
||||||
t.deepEqual(body1.options.selectors[0], "title");
|
t.equal(body1.options.selectors[0], "title");
|
||||||
t.deepEqual(body1.options.selectors[1], ".js-Pjax");
|
t.equal(body1.options.selectors[1], ".js-Pjax");
|
||||||
|
t.equal(isObjLiteral(body1.options.switches), true);
|
||||||
t.deepEqual(isObjLiteral(body1.options.switches), true);
|
t.equal(enumerableKeys(body1.options.switches), 2);// head and body
|
||||||
t.deepEqual(enumerableKeys(body1.options.switches), 2);// head and body
|
t.equal(isObjLiteral(body1.options.switchesOptions), true);
|
||||||
|
t.equal(enumerableKeys(body1.options.switchesOptions), 0);
|
||||||
t.deepEqual(isObjLiteral(body1.options.switchesOptions), true);
|
t.equal(body1.options.history, true);
|
||||||
t.deepEqual(enumerableKeys(body1.options.switchesOptions), 0);
|
t.equal(typeof body1.options.analytics, "function");
|
||||||
|
t.equal(body1.options.scrollTo, 0);
|
||||||
t.deepEqual(body1.options.history, true);
|
t.equal(body1.options.scrollRestoration, true);
|
||||||
|
t.equal(body1.options.cacheBust, true);
|
||||||
// TODO analytics is a little weird right now
|
t.equal(body1.options.debug, false);
|
||||||
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.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user