Skip to content

Instantly share code, notes, and snippets.

@ideacco
Created August 6, 2019 12:04
Show Gist options
  • Save ideacco/9d6740efb0949ba1cebc6bd4cf56a81a to your computer and use it in GitHub Desktop.
Save ideacco/9d6740efb0949ba1cebc6bd4cf56a81a to your computer and use it in GitHub Desktop.
跨域 postMessage
//原始网站区域
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-104793185-3', 'auto');
ga('send', 'pageview');
// ga('require', 'linker');
// ga('linker:autoLink', ['https://go2sleep-official.myshopify.com'], false, true);
ga(function(tracker) {
// Gets the client ID of the default tracker.
var clientId = tracker.get('clientId');
// Gets a reference to the window object of the destionation iframe.
var frameWindow = document.getElementByTagName('iframe').contentWindow;
// Sends the client ID to the window inside the destination frame.
frameWindow.postMessage(clientId, 'https://go2sleep-official.myshopify.com');
});
// shopify 的代码区域
window.addEventListener('message', function(event) {
if (event.origin !== 'https://sleepon.us') return;
ga('create', 'UA-104793185-3', 'auto', {
clientId: event.data
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment