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).
|
||||
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)
|
||||
|
||||
|
||||
2
index.js
2
index.js
@@ -279,7 +279,9 @@ Pjax.prototype = {
|
||||
// Fire Events
|
||||
trigger(document,"pjax:complete pjax:success", state.options)
|
||||
|
||||
if (typeof state.options.analytics === "function") {
|
||||
state.options.analytics()
|
||||
}
|
||||
|
||||
if (state.options.history) {
|
||||
// First parse url and check for hash to override scroll
|
||||
|
||||
@@ -3,16 +3,14 @@
|
||||
var defaultSwitches = require("../switches")
|
||||
|
||||
module.exports = function(options) {
|
||||
this.options = options
|
||||
this.options.elements = this.options.elements || "a[href], form[action]"
|
||||
this.options.selectors = this.options.selectors || ["title", ".js-Pjax"]
|
||||
this.options.switches = this.options.switches || {}
|
||||
this.options.switchesOptions = this.options.switchesOptions || {}
|
||||
this.options.history = this.options.history || true
|
||||
this.options.analytics = this.options.analytics || function() {
|
||||
// options.backward or options.foward can be true or undefined
|
||||
// by default, we do track back/foward hit
|
||||
// https://productforums.google.com/forum/#!topic/analytics/WVwMDjLhXYk
|
||||
options.elements = options.elements || "a[href], form[action]"
|
||||
options.selectors = options.selectors || ["title", ".js-Pjax"]
|
||||
options.switches = options.switches || {}
|
||||
options.switchesOptions = options.switchesOptions || {}
|
||||
options.history = options.history || true
|
||||
options.analytics = (typeof options.analytics === "function" || options.analytics === false) ?
|
||||
options.analytics :
|
||||
function() {
|
||||
if (window._gaq) {
|
||||
_gaq.push(["_trackPageview"])
|
||||
}
|
||||
@@ -20,23 +18,22 @@ module.exports = function(options) {
|
||||
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
|
||||
this.options.timeout = this.options.timeout || 0
|
||||
this.options.scrollRestoration = (typeof this.options.scrollRestoration !== "undefined") ? this.options.scrollRestoration : true
|
||||
options.scrollTo = (typeof options.scrollTo === "undefined") ? 0 : options.scrollTo;
|
||||
options.scrollRestoration = (typeof options.scrollRestoration !== "undefined") ? options.scrollRestoration : true
|
||||
options.cacheBust = (typeof options.cacheBust === "undefined") ? true : options.cacheBust
|
||||
options.debug = options.debug || false
|
||||
options.timeout = options.timeout || 0
|
||||
|
||||
// we can’t replace body.outerHTML or head.outerHTML
|
||||
// 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
|
||||
// & it break the switchFallback which replace head & body
|
||||
if (!this.options.switches.head) {
|
||||
this.options.switches.head = defaultSwitches.switchElementsAlt
|
||||
if (!options.switches.head) {
|
||||
options.switches.head = defaultSwitches.switchElementsAlt
|
||||
}
|
||||
if (!this.options.switches.body) {
|
||||
this.options.switches.body = defaultSwitches.switchElementsAlt
|
||||
}
|
||||
if (typeof options.analytics !== "function") {
|
||||
options.analytics = function() {}
|
||||
if (!options.switches.body) {
|
||||
options.switches.body = defaultSwitches.switchElementsAlt
|
||||
}
|
||||
|
||||
this.options = options
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user