Skip to content

Commit 7597a6f

Browse files
author
Han Kim
committed
dont defer init for assumepageview
1 parent e8cc1b0 commit 7597a6f

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
lines changed

lib/protos.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
var Emitter = require('component-emitter');
8-
var after = require('@ndhoule/after');
98
var each = require('@ndhoule/each');
109
var events = require('analytics-events');
1110
var every = require('@ndhoule/every');
@@ -187,19 +186,14 @@ exports.invoke = function(method) {
187186
};
188187

189188
/**
190-
* Queue a `method` with `args`. If the integration assumes an initial
191-
* pageview, then let the first call to `page` pass through.
189+
* Queue a `method` with `args`.
192190
*
193191
* @api private
194192
* @param {string} method
195193
* @param {Array} args
196194
*/
197195

198196
exports.queue = function(method, args) {
199-
if (method === 'page' && this._assumesPageview && !this._initialized) {
200-
return this.page.apply(this, args);
201-
}
202-
203197
this._queue.push({ method: method, args: args });
204198
};
205199

@@ -336,12 +330,10 @@ exports._wrapInitialize = function() {
336330
this.emit('initialize');
337331
return ret;
338332
};
339-
340-
if (this._assumesPageview) this.initialize = after(2, this.initialize);
341333
};
342334

343335
/**
344-
* Wrap the page method to call `initialize` instead if the integration assumes
336+
* Wrap the page method to call to noop the first page call if the integration assumes
345337
* a pageview.
346338
*
347339
* @api private
@@ -350,8 +342,10 @@ exports._wrapInitialize = function() {
350342
exports._wrapPage = function() {
351343
var page = this.page;
352344
this.page = function() {
353-
if (this._assumesPageview && !this._initialized) {
354-
return this.initialize.apply(this, arguments);
345+
if (this._assumesPageview) {
346+
if (this._initialPageNooped) return page.apply(this, arguments);
347+
348+
return this._initialPageNooped = true;
355349
}
356350

357351
return page.apply(this, arguments);

lib/statics.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,16 @@ exports.global = function(key) {
7474
};
7575

7676
/**
77-
* Mark the integration as assuming an initial pageview, so to defer loading
78-
* the script until the first `page` call, noop the first `initialize`.
77+
* Mark the integration as assuming an initial pageview, so to defer the first page call, keep track of
78+
* whether we already nooped the first page call.
7979
*
8080
* @api public
8181
* @return {Integration}
8282
*/
8383

8484
exports.assumesPageview = function() {
8585
this.prototype._assumesPageview = true;
86+
this.prototype._initialPageNooped = false;
8687
return this;
8788
};
8889

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
},
2222
"homepage": "https://github.com/segmentio/analytics.js-integration#readme",
2323
"dependencies": {
24-
"@ndhoule/after": "^1.0.0",
2524
"@ndhoule/clone": "^1.0.0",
2625
"@ndhoule/defaults": "^2.0.1",
2726
"@ndhoule/each": "^2.0.1",

test/index.test.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,11 @@ describe('integration', function() {
209209
assert(integration._initialized === true);
210210
});
211211

212-
it('should be a noop the first time if the integration assumes a pageview', function() {
212+
it('should still initialize if the integration assumes a pageview', function() {
213213
var initialize = Integration.prototype.initialize = spy();
214214
Integration.assumesPageview();
215215
var integration = new Integration();
216216
integration.initialize();
217-
assert(!initialize.called);
218-
integration.initialize();
219217
assert(initialize.called);
220218
});
221219
});
@@ -322,9 +320,9 @@ describe('integration', function() {
322320
integration.initialize = spy();
323321
});
324322

325-
it('should transform #page to #initialize when a pageview is assumed', function() {
323+
it('should not transform #page to #initialize when a pageview is assumed', function() {
326324
integration.queue('page', [{ name: 'page' }]);
327-
assert(integration.initialize.calledWith({ name: 'page' }));
325+
assert(!integration.initialize.calledWith({ name: 'page' }));
328326
});
329327

330328
it('should push the method and args onto the queue', function() {
@@ -343,12 +341,12 @@ describe('integration', function() {
343341
});
344342

345343
describe('#page', function() {
346-
it('should call initialize the first time when a page view is assumed', function() {
344+
it('should not call initialize the first time when a page view is assumed', function() {
347345
Integration.assumesPageview();
348346
integration = new Integration();
349347
integration.initialize = spy();
350348
integration.page({ name: 'page name' });
351-
assert(integration.initialize.calledWith({ name: 'page name' }));
349+
assert(!integration.initialize.calledWith({ name: 'page name' }));
352350
});
353351

354352
it('should return the value', function() {

0 commit comments

Comments
 (0)