Created
April 7, 2012 10:50
-
-
Save ttrefren/2327440 to your computer and use it in GitHub Desktop.
failed attempt at writing blocking tracking
This file contains hidden or 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></head> | |
<body> | |
<h1>hi</h1> | |
</body> | |
<!-- start Mixpanel --><script type="text/javascript">(function(d,c){var a,b,g,e;a=d.createElement("script");a.type="text/javascript";a.async=!0;a.src=("https:"===d.location.protocol?"https:":"http:")+'//api.mixpanel.com/site_media/js/api/mixpanel.2.js';b=d.getElementsByTagName("script")[0];b.parentNode.insertBefore(a,b);c._i=[];c.init=function(a,d,f){var b=c;"undefined"!==typeof f?b=c[f]=[]:f="mixpanel";g="disable track track_pageview track_links track_forms register register_once unregister identify name_tag set_config".split(" "); | |
for(e=0;e<g.length;e++)(function(a){b[a]=function(){b.push([a].concat(Array.prototype.slice.call(arguments,0)))}})(g[e]);c._i.push([a,d,f])};window.mixpanel=c})(document,[]); | |
mixpanel.init("tim", { debug: true });</script><!-- end Mixpanel --> | |
<script type="text/javascript"> | |
//(function() { | |
var blocking_queue = []; | |
window.blocking_track = function(event, properties, max_time) { | |
max_time = max_time || 1000; | |
var i = blocking_queue.length; | |
blocking_queue.push(false); | |
var unblock = function() { | |
blocking_queue[i] = true; | |
}; | |
// pass unblock as our callback to mp.track | |
mixpanel.track(event, properties, unblock); | |
// set a maximum block time - after this we unblock no matter what | |
setTimeout(unblock, max_time); | |
console.log('In blocking track', event, blocking_queue); | |
}; | |
window.wait_for_tracking = function() { | |
// Check if any of the items in our block queue | |
// are still blocking. | |
var kill = false; | |
setTimeout(function() { | |
kill = true; | |
}, 1000); | |
var any_blocked = function() { | |
if (kill) { | |
console.log('killed') | |
return false; | |
} | |
console.log('queue state', blocking_queue, blocking_queue[0]); | |
console.log(blocking_queue.length) | |
var i = 0; | |
for (i; i < blocking_queue.length; i++) { | |
// if even a single element is false, we still block | |
console.log('checking blocking queue', i, blocking_queue[i]) | |
if (!blocking_queue[i]) { | |
console.log('still blocked') | |
return true; | |
} | |
} | |
return false; | |
}; | |
while(any_blocked()) {} | |
}; | |
//}); | |
blocking_track("t1", {}, 500); | |
blocking_track("t2", {}, 500); | |
blocking_track("t3", {}, 500); | |
wait_for_tracking(); | |
console.log('tracking completed'); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment