Created
August 9, 2016 14:54
-
-
Save nicobrx/75d6bb89314edf094fbc3942923c1cb7 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
<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