Created
June 6, 2019 20:40
-
-
Save jordangarcia/8f296aafb293a9c6270b162e0b4f510b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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