Skip to content

Instantly share code, notes, and snippets.

@nicobrx
Created August 9, 2016 14:54
Show Gist options
  • Save nicobrx/75d6bb89314edf094fbc3942923c1cb7 to your computer and use it in GitHub Desktop.
Save nicobrx/75d6bb89314edf094fbc3942923c1cb7 to your computer and use it in GitHub Desktop.
<script>
/*
* adapted from https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain#iframes
* I wrapped the whole thing in a setTimeout, so I could observe the delayed execution time
* of the GA function in the JavaScript console, using the GA debugger Chrome plugin.
* alternatively, you could just wrap the frameWindow.postMessage in a setTimeout
*/
setTimeout(function(){
ga(function(tracker) {
var clientId = tracker.get('clientId');
var frameWindow = document.getElementById('destination-frame').contentWindow;
// change https://xyz.shoppingcart.com to match your iFrame domain
frameWindow.postMessage(clientId, 'https://xyz.shoppingcart.com');
});
}, 2000);
// I decided on a 2 second timeout by trial and error. In my case, this seemed to be
// long enough to allow the iFrame to load > 95% of the time. Increasing the delay too
// much risks having the user move on before being tracked at all
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment