Skip to content

Instantly share code, notes, and snippets.

@jordangarcia
Created June 6, 2019 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordangarcia/8f296aafb293a9c6270b162e0b4f510b to your computer and use it in GitHub Desktop.
Save jordangarcia/8f296aafb293a9c6270b162e0b4f510b to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script src="./node_modules/@optimizely/optimizely-sdk/dist/optimizely.browser.umd.js"></script>
<script>
async function main() {
optimizelySdk.setLogLevel("debug");
const optimizely = optimizelySdk.createInstance({
sdkKey: "BsSyVRsUbE3ExgGCJ9w1to",
eventFlushInterval: 105000,
eventBatchSize: 1,
eventDispatcher: {
dispatchEvent: async (data, callback) => {
console.log("dispatching", data);
const res = await fetch("http://localhost:3000/log", {
method: "POST", // or 'PUT'
body: JSON.stringify(data.params),
headers: {
"Content-Type": "application/json"
}
});
callback({
statusCode: res.status
})
}
}
});
await optimizely.onReady();
$(() => {
optimizely.isFeatureEnabled("feature1", "jordan");
// optimizely.track('win', 'jordan')
// for (let i = 0 ; i < 1000; i++) {
// optimizely.track('win', 'jordan')
// }
$("a").click(() => {
console.log("button clicked");
optimizely.track("event1", "jordan");
});
if ("onpagehide" in window) {
console.log("adding onpagehide");
window.addEventListener(
"pagehide",
function(e) {
console.log("page hide", e);
optimizely.close();
},
false
);
} else {
console.log("adding onunload");
window.addEventListener(
"unload",
function(e) {
console.log("unload");
optimizely.close();
},
false
);
}
});
}
main();
</script>
</head>
<body>
<h1>welcome!</h1>
<a href="page1.html">clickme</a>
</body>
</html>
{
"name": "matjaz",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@optimizely/optimizely-sdk": "^3.2.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1"
}
}
<html>
<head>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script src="./node_modules/@optimizely/optimizely-sdk/dist/optimizely.browser.umd.js"></script>
<script>
optimizelySdk.setLogLevel("debug");
const optimizely = optimizelySdk.createInstance({
sdkKey: "BsSyVRsUbE3ExgGCJ9w1to",
eventFlushInterval: 105000,
eventBatchSize: 1000,
eventDispatcher: {
dispatchEvent: async (data, callback) => {
console.log("dispatching", data);
const res = await fetch("http://localhost:3000/log", {
method: "POST", // or 'PUT'
body: JSON.stringify(data.params),
headers: {
"Content-Type": "application/json"
}
});
callback({
statusCode: res.status
});
}
}
});
</script>
</head>
<body>
<a href="index.html">back</a>
</body>
</html>
const express = require("express");
const bodyParser = require("body-parser");
const cors = require('cors')
const app = express();
const port = 3000;
app.use(cors()); // for parsing application/json
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
const sleep = (time = 0) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, time)
})
}
app.post('/log', async (req, res) => {
console.log(`
${new Date()}
${JSON.stringify(req.body)}`)
await sleep(300);
res.send('hello')
})
app.get('/', (req, res) => {
res.send('hello world')
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment