Quick question. Can i somehow access javascript variable inside script and then use it to make python graphs?
i got javascript script running on my website, i need to "export" variables inside script after leaving website, then use it in python script running somewhere else. My site is basic html page collecting data about its usage.
window.onload = function(){
var heatmapInstance = h337.create({
container: document.querySelector('.heatmap'),
radius: 90
});
document.querySelector('.heatmap').onmousemove = function(ev) {
heatmapInstance.addData({
x: ev.layerX,
y: ev.layerY,
value: 1
});
};
}
function getHeatMap() {
var currentData = heatmapInstance.getData();
return currentData;
}
1 Answer 1
You can use the beacon API to flush data when users close the tab, in supported browsers, to your back-end.
window.onload = window.onunload = function analytics(event) {
if (!navigator.sendBeacon) return;
var url = "https://example.com/analytics";
// Create the data to send
var data = "state=" + event.type + "&location=" + location.href;
// Send the beacon
var status = navigator.sendBeacon(url, data);
// Log the data and result
console.log("sendBeacon: URL = ", url, "; data = ", data, "; status = ", status);
};
Sign up to request clarification or add additional context in comments.
Comments
default