@optimizely/sdk-plugin-pending-events v0.5.0
@optimizely/sdk-plugin-pending-events
An unsupported, experimental⚠️ EventDispatcher for Optimizely Full Stack (javascript-sdk, Web browser environment) that keeps a queue of pending (not completed) events persisted to localStorage, and retries pending events on initialization.
Motivation
Tracking click in browsers is difficult because browsers often redirect users before tracking requests complete. This example suggests one of many possible solutions to the above problem. Feel free to try it out, modify according the the LICENSE, and suggest changes here. And if you have another method for tackling this problem, we'd love to hear from you! Drop us a line at developers@optimizely.com.
Install
npm install @optimizely/sdk-plugin-pending-events --saveUsage
See example for an example of how this is used:
$ cd example
$ npm install
$ npm startLoad the URL provided by webpack-serve in a browser of your choice.
API
index
Construct an EventDispatcher compatible with @optimizely/optimizely-sdk
Parameters
localStorageKeyString Key under which to persist/load pending events inwindow.localStoragesendJSONSendJSON Function to call to send payloadloggerFunction?
Returns EventDispatcher An object with a dispatchEvent method, suitable for use as an EventDispatcher
SendJSON
Function to call to send JSON
Type: Function
Parameters
urlString URL to send tooptionsObjectcallbackSendJSONCallback Function to call, with no arguments, if successful, and with Error object, if error
Examples
// Example sendJSON built using fetch
const sendJSON = (url, options, callback) => {
const {method, body} = options;
return fetch(url, {
method,
body,
headers: {
'content-type': 'application/json',
}
})
.then((resp) => {
if (resp.status < 400) {
callback();
} else {
callback(new Error(`Bad response code: ${resp.status}`));
}
}, callback);
}SendJSONCallback
Type: Function
Parameters
errorError? Error, if any