The client’s homepage has a “location finder” iframe near the bottom. This iframe contains the phone number of each location. CallTrackerFX is installed in this iframe. The desired result is for CallTrackerFX to work inside the iframe exactly like it does outside the iframe, but in practice this is not so simple.
CallTrackerFX uses the user’s session information to determine what numbers to replace and how. By default, if the user is accessing CallTrackerFX inside an iframe, their session is considered a referral from the parent site, instead of (for instance) organic search. CallTrackerFX gets its information from a cookie, so in order to change CallTrackerFX’s session variables, the cookie must be changed.
CallTrackerFX creates an object called CallTrack that can be accessed in the console. That object contains another object called CallTrack.Cookie, which contains the methods necessary to manipulate the cookie CallTrackerFX relies on. Getting the cookie information is easy (CallTrack.Cookie.get()), and from there it can be passed to the iframe using the same method we use to pass MarketingCloudFX data around.
Manipulating the cookie in the iframe is a different matter. The CallTrack.Cookie object has a set() method to match its get() method, and under normal circumstances it’s trivial to add a cookie like so:
CallTrack.Cookie.set(cookieName, value)
However, inside of an iframe, this isn’t sufficient. Cookies inside iframes need the “samesite=none” and “secure” attributes in order to save. The CallTrack.Cookie.set() does allow for additional attributes like so:
CallTrack.Cookie.set(cookiename, value, {attribute1: value, attribute2: value})
Since the data gathered by CallTrack.Cookie.get() includes more than one cookie, we can loop through each one and use CallTrack.Cookie.set() to add the cookies to the document. After that, the CallTrack object will need to be re-initialized using CallTrack.init() so it can apply the appropriate rules.
Wrote a script to pass CallTrackerFX session data from a parent window to an iframe.
Parent window:
<script>
(function () {
// Wait for the MCFX pixel to be loaded.
var checkForMCFX = setInterval(
function () {
if (tracker) {
clearInterval(checkForMCFX);
mcfxPixelFound();
}
}, 500
)
function mcfxPixelFound() {
var sessionDetails = tracker.visitor.get();
//Add a piece of data we can check later to see if this is MCFX data or CTFX data.
sessionDetails.source = "MCFX";
var locatorIframe = document.querySelectorAll("iframe#locator_iframe14896");
setTimeout(function () {
locatorIframe.forEach(function (locator) {
locator.contentWindow.postMessage(JSON.stringify(sessionDetails), "https://code.metalocator.com");
console.log("MCFX session data sent to iframe.");
})
}, 500);
}
//Wait for the CTFX object to be loaded.
var checkForCTFX = setInterval(
function () {
if (CallTrack) {
clearInterval(checkForCTFX);
ctfxObjectFound();
}
}, 500
)
function ctfxObjectFound() {
var sessionDetails = CallTrack.Cookie.get();
//Add a piece of data we can check later to see if this is MCFX data or CTFX data.
sessionDetails.source = "CTFX";
var locatorIframe = document.querySelectorAll("iframe#locator_iframe14896");
setTimeout(function () {
locatorIframe.forEach(function (locator) {
locator.contentWindow.postMessage(JSON.stringify(sessionDetails), "https://code.metalocator.com");
console.log("CTFX session data sent to iframe.");
})
}, 500);
}
})();
</script>
Child window:
<script>
(function () {
// Wait for the MCFX pixel to be loaded.
window.addEventListener("message", function (event) {
if (event.origin !== "https://bwisetrailers.com") return;
if (JSON.parse(event.data).source === "MCFX") {
//Remove the source data we added in the parent window.
delete event.data.source;
var checkForMCFX = setInterval(
function () {
if (tracker) {
clearInterval(checkForMCFX);
mcfxPixelFound(event.data);
}
}, 500
)
} else if (JSON.parse(event.data).source === "CTFX") {
//Remove the source data we added in the parent window.
delete event.data.source;
var checkForCTFX = setInterval(
function () {
if (CallTrack) {
clearInterval(checkForCTFX);
ctfxObjectFound(event.data);
}
}, 500
)
}
})
function mcfxPixelFound(sessionData) {
tracker.visitor.set(JSON.parse(sessionData));
console.log("MCFX session data successfully set.");
}
function ctfxObjectFound(sessionData) {
sessionData = JSON.parse(sessionData);
Object.keys(sessionData).forEach(function(key){
CallTrack.Cookie.set(key, sessionData[key], {
samesite: "none",
secure: true
})
})
console.log("CTFX session data successfully set.");
//Reinitialize CallTrack object to pick up the new session data.
CallTrack.init();
console.log("CallTrack object reinitialized.");
}
})();
</script>