




(function loadIframe(){

	// Messages from Iframe to Parent window
	window.addEventListener('message', function(event) {
		if("ads.kwanzoo.com" === event.origin.substring(event.origin.indexOf("//") + 2)) {
			const eventId = event.data.eventId;
			const data = event.data.data;
			eventId && console.log(eventId, data);  // do not log ping message

			// google analytics integration code
			if("kwanzoo_message_ga" === eventId) {
				window.dataLayer = window.dataLayer || [];
				window.dataLayer.push(data);
			}
			// drift integration code
			else if("kwanzoo_message_drift" === eventId) {
				if(data.isDriftEnabled && typeof drift != "undefined") {
					drift.on('ready', function() {
						drift.api.setUserAttributes({
							kwanzooId: data.sessionUid
						});
					});
				}
			}
		}
	});

	var protocol_scheme = 'https';

	if( protocol_scheme != 'https')
		protocol_scheme = 'http';
	
	var finalUrl = protocol_scheme + '://ads.kwanzoo.com/wvt-iframe/load';
	
	function getCookieValue(cookieName){
		var cookieValArr = document.cookie.match(cookieName + "=([^;]*)");
		var value = "";
		if(cookieValArr) value = cookieValArr[1];
		return  value;
	}

	<!-- Marketing Automation Platform (MAP) cookie integration -->

	var marketoCookieName = "";
	var pardotCookieName = "";
	var hubspotCookieName = "";
	var activeCampaignCookieName = "";

	var params = 'widgetId=9615';

	if(marketoCookieName)
		params += "&marketo=" + getCookieValue(marketoCookieName);

	if(pardotCookieName)
		params += "&pardot=" + getCookieValue(pardotCookieName);

	if(hubspotCookieName) 
		params += "&hubspot=" + getCookieValue(hubspotCookieName);
		
	if(activeCampaignCookieName) 
		params += "&active_campaign=" + getCookieValue(activeCampaignCookieName);

	if(params) finalUrl += "?" + params
	
	finalUrl = unescape(finalUrl);

	var iframe = document.createElement("iframe");
	iframe.setAttribute("id", "kwcWidgetFrame");
	iframe.setAttribute("name", "kwcWidgetFrame");
	iframe.setAttribute("src", finalUrl);
    iframe.setAttribute("style", "display:none;");
    // From chrome 85 default referrer policy is updated. (KwanzooNextAPI/#502) 
    iframe.setAttribute("referrerpolicy", "no-referrer-when-downgrade");
    
    
	// The browser runs the code in each script tag immediately, before moving forward in the document. 
	// That is, if there are two script tags as in the sample code in the question, 
	// then when the code in the first one runs, the second script will not yet be part of the DOM
	
	var currScript = document.currentScript;
    if (!currScript) {
        var all_scripts = document.getElementsByTagName("script");
        currScript = all_scripts[all_scripts.length - 1];
    }
    currScript.parentNode.insertBefore(iframe, currScript);
	iframe.contentWindow.KZ_REF_URL = window.location.href;

})();


