Skip to content

feat (event processor/pending events dispatcher): In browser entry point, add event listener for pagehide or unload that calls optimizely.close #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions packages/optimizely-sdk/lib/index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var logger = logging.getLogger();
logging.setLogHandler(loggerPlugin.createLogger());
logging.setLogLevel(logging.LogLevel.INFO);

var MODULE_NAME = 'INDEX_BROWSER';

var DEFAULT_EVENT_BATCH_SIZE = 10;
var DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s

Expand Down Expand Up @@ -105,16 +107,20 @@ module.exports = {
eventDispatcher = config.eventDispatcher;
}

config = fns.assignIn({
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
}, config, {
eventDispatcher: eventDispatcher,
// always get the OptimizelyLogger facade from logging
logger: logger,
errorHandler: logging.getErrorHandler(),
});
config = fns.assignIn(
{
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
},
config,
{
eventDispatcher: eventDispatcher,
// always get the OptimizelyLogger facade from logging
logger: logger,
errorHandler: logging.getErrorHandler(),
}
);

if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
Expand All @@ -125,7 +131,24 @@ module.exports = {
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
}

return new Optimizely(config);
var optimizely = new Optimizely(config);
Copy link
Contributor

@mikeproeng37 mikeproeng37 Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should bother making this turnoffable in case the user is really adamant about doing it themselves. I don't see it as a hard blocker for this PR though. Just food for thought.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair question. I think this is the right default given that batching will be enabled by default. I'm willing to wait and see if there's any interest in turning it off from users.


try {
if (typeof window.addEventListener === 'function') {
var unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
window.addEventListener(
unloadEvent,
function() {
optimizely.close();
},
false
);
}
} catch (e) {
logger.error(enums.LOG_MESSAGES.UNABLE_TO_ATTACH_UNLOAD, MODULE_NAME, e.message);
}

return optimizely;
} catch (e) {
logger.error(e);
return null;
Expand Down
18 changes: 18 additions & 0 deletions packages/optimizely-sdk/lib/index.browser.umdtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
var configValidator = require('./utils/config_validator');
var enums = require('./utils/enums');
var logger = require('./plugins/logger');
var Optimizely = require('./optimizely');

var packageJSON = require('../package.json');
var eventDispatcher = require('./plugins/event_dispatcher/index.browser');
Expand All @@ -40,6 +41,7 @@ describe('javascript-sdk', function() {
logToConsole: false,
});
sinon.stub(configValidator, 'validate');
sinon.stub(Optimizely.prototype, 'close');

xhr = sinon.useFakeXMLHttpRequest();
global.XMLHttpRequest = xhr;
Expand All @@ -52,14 +54,18 @@ describe('javascript-sdk', function() {
sinon.spy(console, 'info');
sinon.spy(console, 'warn');
sinon.spy(console, 'error');

sinon.spy(window, 'addEventListener');
});

afterEach(function() {
console.log.restore();
console.info.restore();
console.warn.restore();
console.error.restore();
window.addEventListener.restore();
configValidator.validate.restore();
Optimizely.prototype.close.restore();
xhr.restore();
});

Expand Down Expand Up @@ -292,6 +298,18 @@ describe('javascript-sdk', function() {
var variation = optlyInstance.getVariation('testExperimentNotRunning', 'testUser');
assert.strictEqual(variation, null);
});

it('should hook into window `pagehide` event', function() {
var optlyInstance = window.optimizelySdk.createInstance({
datafile: testData.getTestProjectConfig(),
errorHandler: fakeErrorHandler,
eventDispatcher: eventDispatcher,
logger: silentLogger,
});

sinon.assert.calledOnce(window.addEventListener);
sinon.assert.calledWith(window.addEventListener, sinon.match('pagehide').or(sinon.match('unload')));
});
});
});
});
1 change: 1 addition & 0 deletions packages/optimizely-sdk/lib/utils/enums/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ exports.LOG_MESSAGES = {
UNKNOWN_MATCH_TYPE: '%s: Audience condition %s uses an unknown match type. You may need to upgrade to a newer release of the Optimizely SDK.',
UPDATED_OPTIMIZELY_CONFIG: '%s: Updated Optimizely config to revision %s (project id %s)',
OUT_OF_BOUNDS: '%s: Audience condition %s evaluated to UNKNOWN because the number value for user attribute "%s" is not in the range [-2^53, +2^53].',
UNABLE_TO_ATTACH_UNLOAD: '%s: unable to bind optimizely.close() to page unload event: "%s"',
};

exports.RESERVED_EVENT_KEYWORDS = {
Expand Down