Created
December 12, 2019 11:49
-
-
Save pualien/f5402416a51cfb66f8a965fbab53dedc to your computer and use it in GitHub Desktop.
Iframe Post Message cmp IAB
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
/* iframe code */ | |
var callId = Math.random(); | |
function __receiveTealiumMessage(event) { | |
var tealiumMasterTagCallIdentifier = '__rtiShareDl'; | |
var event_data = {}; | |
try { | |
event_data = JSON.parse(event.data); | |
} catch (e) { | |
if (utag !== undefined && utag.DB) { | |
utag.DB("error in " + tealiumMasterTagCallIdentifier); | |
utag.DB(e); | |
} | |
} | |
if (event_data[tealiumMasterTagCallIdentifier] | |
&& event_data[tealiumMasterTagCallIdentifier].command | |
&& event_data[tealiumMasterTagCallIdentifier].callId | |
&& event_data[tealiumMasterTagCallIdentifier].callId === callId | |
&& window !== event.source) { | |
if (utag !== undefined && utag.DB) { | |
utag.DB("received " + tealiumMasterTagCallIdentifier); | |
} | |
window.___ext_utag_data = event_data; | |
window.removeEventListener("message", __receiveTealiumMessage, false); | |
} | |
} | |
function __sendTealiumIframeRequest() { | |
var tealiumRequestIframeTagCallIdentifier = '__rtiRequestShareDl'; | |
___iframe_request_obj = {}; | |
___iframe_request_obj[tealiumRequestIframeTagCallIdentifier] = {}; | |
___iframe_request_obj[tealiumRequestIframeTagCallIdentifier]["command"] = "request share DL"; | |
___iframe_request_obj[tealiumRequestIframeTagCallIdentifier]["callId"] = callId; | |
if (utag !== undefined && utag.DB) { | |
utag.DB("sending " + tealiumRequestIframeTagCallIdentifier); | |
} | |
window.parent.postMessage(JSON.stringify(___iframe_request_obj), "*"); | |
} | |
window.addEventListener('message', __receiveTealiumMessage); | |
__sendTealiumIframeRequest(); |
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
/* page code */ | |
function __receiveTealiumIframeRequest(event) { | |
var tealiumMasterTagCallIdentifier = '__rtiShareDl'; | |
var tealiumRequestIframeTagCallIdentifier = '__rtiRequestShareDl'; | |
var iframeAllowedOriginDomains = [ | |
"http://localhost:3001", | |
"http://localhost:3001/" | |
]; | |
var event_data = {}; | |
try { | |
event_data = JSON.parse(event.data); | |
} catch (e) { | |
if (utag !== undefined && utag.DB) { | |
utag.DB("error in " + tealiumRequestIframeTagCallIdentifier); | |
utag.DB(e); | |
} | |
} | |
if ( | |
event_data[tealiumRequestIframeTagCallIdentifier] | |
&& event_data[tealiumRequestIframeTagCallIdentifier].command | |
&& event_data[tealiumRequestIframeTagCallIdentifier].callId | |
&& iframeAllowedOriginDomains.includes(event.origin)) { | |
if (utag !== undefined && utag.DB) { | |
utag.DB("received " + tealiumRequestIframeTagCallIdentifier); | |
} | |
function __sendTealiumDataToIframe(event, callId) { | |
var dpclnDL = JSON.parse(JSON.stringify(window.utag_data)); | |
var keepOnlyProp = ["ut.visitor_id", "ut.session_id", "da_campaign", "da_medium", "da_source"]; | |
for (var k in dpclnDL) { | |
if (keepOnlyProp.indexOf(k) < 0) { | |
delete dpclnDL[k]; | |
} | |
} | |
dpclnDL[tealiumMasterTagCallIdentifier] = {}; | |
dpclnDL[tealiumMasterTagCallIdentifier]["command"] = "share DL"; | |
dpclnDL[tealiumMasterTagCallIdentifier]["callId"] = callId; | |
var frames = document.getElementsByTagName('iframe'); | |
var iframe = undefined; | |
for (var i = 0; i < frames.length; i++) { | |
tmpOrig = frames[i].src.match(/^.+\:\/\/[^\/]+\//); | |
if (tmpOrig !== null) { | |
tmpOrig = tmpOrig[0]; | |
} | |
if (tmpOrig !== null && iframeAllowedOriginDomains.includes(tmpOrig)) { | |
iframe = frames[i]; | |
break; | |
} | |
} | |
event.source.window.postMessage(JSON.stringify(dpclnDL),event.origin); | |
} | |
__sendTealiumDataToIframe(event, event_data[tealiumRequestIframeTagCallIdentifier].callId); | |
} | |
} | |
window.addEventListener('message', __receiveTealiumIframeRequest); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment