diff --git a/Universal-Federated-Analytics-Min.js b/Universal-Federated-Analytics-Min.js index 174b7e4..5dfc224 100644 --- a/Universal-Federated-Analytics-Min.js +++ b/Universal-Federated-Analytics-Min.js @@ -1,53 +1,2 @@ -(function(){function D(){dataLayer.push(arguments)}function v(a){switch(a.toString().toLowerCase()){case "true":case "on":case "yes":case "1":return!0;case "false":case "off":case "no":case "0":return!1;default:return a}}function L(a){a=a.toLowerCase();a=a.match(/^g\-([0-9a-z])+$/);return null!==a&&0=r&&r>c[f.target.id].latest_milestone&&(c[f.target.id].latest_milestone=r);c[f.target.id].latest_milestone&&!c[f.target.id]._progress_milestones[c[f.target.id].latest_milestone]&&(c[f.target.id]._progress_milestones[c[f.target.id].latest_milestone]=!0,"video"===n?(h.video_current_time=c[f.target.id].current,h.video_duration=Math.round(f.target.duration),h.video_percent=c[f.target.id].latest_milestone): -(h.audio_current_time=c[f.target.id].current,h.audio_duration=Math.round(f.target.duration),h.audio_percent=c[f.target.id].latest_milestone),q(n+"_progress",h));break;case "play":"video"===n?(h.video_current_time=c[f.target.id].current,h.video_duration=Math.round(f.target.duration),h.video_percent=c[f.target.id].latest_milestone):(h.audio_current_time=c[f.target.id].current,h.audio_duration=Math.round(f.target.duration),h.audio_percent=c[f.target.id].latest_milestone);q(0===c[f.target.id].current? -n+"_start":n+"_play",h);break;case "pause":c[f.target.id].current!==Math.round(f.target.duration)&&("video"===n?(h.video_current_time=c[f.target.id].current,h.video_duration=Math.round(f.target.duration),h.video_percent=c[f.target.id].latest_milestone):(h.audio_current_time=c[f.target.id].current,h.audio_duration=Math.round(f.target.duration),h.audio_percent=c[f.target.id].latest_milestone),q(n+"_pause",h));break;case "ended":for("video"===n?(h.video_current_time=c[f.target.id].current,h.video_duration= -Math.round(f.target.duration),h.video_percent="100"):(h.audio_current_time=c[f.target.id].current,h.audio_duration=Math.round(f.target.duration),h.audio_percent="100"),q(n+"_complete",h),c[f.target.id].current=0,c[f.target.id].latest_milestone=0,n=1;n<=100/b;n++)4===100/b&&n===100/b?c[f.target.id].progress_point=95:100!==b*n?c[f.target.id].progress_point=b*n:"",c[f.target.id]._progress_milestones[c[f.target.id].progress_point]=!1}}for(var b=d.YT_MILESTONE,c={},e=document.querySelectorAll("video,audio"), -g=0;g=u[cCi].milestone&&!u[cCi].triggered&&u[b].id===b&&(u[cCi].triggered=!0,n.video_percent=u[cCi].milestone,n.video_current_time=Math.round(n.video_duration/u.length*(cCi+1)),q("video_progress",n),cCi++)},(void 0!==p[b].playerInfo?Math.round(p[b].playerInfo.duration):Math.round(p[b].getDuration()))/u.length)}]),K[K.length-1][1](b))):a.data==YT.PlayerState.PLAYING&&q("video_play", -k);a.data==YT.PlayerState.ENDED&&q("video_complete",k);a.data==YT.PlayerState.PAUSED&&q("video_pause",k)}catch(f){}};youtube_parser=function(a){if((a=a.match(/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&\?]*).*/))&&11==a[2].length)return a[2]};IsYouTube=function(a){a=a.match(/(.*)(youtu\.be\/|youtube(\-nocookie)?\.([A-Za-z]{2,4}|[A-Za-z]{2,3}\.[A-Za-z]{2})\/)(watch|embed\/|vi?\/)?(\?vi?=)?([^#&\?\/]{11}).*/);return null!=a&&00&&oCONFIG.USE_PARALLEL_CUSTOM_DIMENSIONS){r={groups:oCONFIG.GA4_NAME+b,cookie_expires:parseInt(oCONFIG.COOKIE_TIMEOUT),page_location:ur,page_title:n,[oCONFIG.PARALLEL_AGENCY_DIMENSION]:oCONFIG.AGENCY.toUpperCase(),[oCONFIG.PARALLEL_SUBAGENCY_DIMENSION]:oCONFIG.SUB_AGENCY.toUpperCase(),[oCONFIG.PARALLEL_SITE_TOPIC_DIMENSION]:oCONFIG.SITE_TOPIC.toLowerCase(),[oCONFIG.PARALLEL_SITE_PLATFORM_DIMENSION]:oCONFIG.SITE_PLATFORM.toLowerCase(),[oCONFIG.PARALLEL_SCRIPT_SOURCE_URL_DIMENSION]:oCONFIG.SCRIPT_SOURCE.toLowerCase(),[oCONFIG.PARALLEL_CODEVERSION_DIMENSION]:oCONFIG.VERSION.toLowerCase(),[oCONFIG.PARALLEL_URL_PROTOCOL_DIMENSION]:oCONFIG.URL_PROTOCOL.toLowerCase(),[oCONFIG.PARALLEL_USING_PARALLEL_DIMENSION]:oCONFIG.USING_PARALLEL_TRACKER.toLowerCase()};document.referrer&&-1!==document.referrer.search(location.hostname)?r.page_referrer=_scrubbedURL(document.referrer):document.referrer;var rr=_piiRedactor(_objToQuery(r),"default");rr=_queryToJSON(rr);rr=_unflattenJSON(rr);gtag("config",oCONFIG.GWT_GA4ID[b],rr)}else{r={groups:oCONFIG.GA4_NAME+b,cookie_expires:parseInt(oCONFIG.COOKIE_TIMEOUT),page_location:ur,page_title:n};document.referrer&&-1!==document.referrer.search(location.hostname)?r.page_referrer=_scrubbedURL(document.referrer):document.referrer;var rr=_piiRedactor(_objToQuery(r),"default");rr=_queryToJSON(rr);rr=_unflattenJSON(rr);gtag("config",oCONFIG.GWT_GA4ID[b],rr)}}isSearch?_sendViewSearchResult({search_term:isSearch}):""}function _initAutoTracker(){var _isDownload=function(a){var ex=a.href.toLowerCase().replace(/[#?&].*/,"").split(a.hostname)[1].split(".");var ext=ex[ex.length-1];if(ext.match(new RegExp("^("+oCONFIG.EXTS+")$"))!=null){return ext}else{return false}};var _eventHandler=function(event){try{if("mousedown"===event.type||"keydown"===event.type&&13===event.keyCode){if(event.target.nodeName==="A"||event.target.closest("a")!==null){var b=oCONFIG.COOKIE_DOMAIN,c="";var d="",f="",e=/^mailto:[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/i,h=/^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i,i="",t="",l={},g=/^(tel:)(.*)$/i;var a=event.target.closest("a");if("mousedown"===event.type){t="Mouse Click"}else if("keydown"===event.type&&13===event.keyCode){t="Enter Key Keystroke"}if(e.test(a.href)||h.test(a.href)||g.test(a.href)){try{h.test(a.href)?(f=a.hostname.toLowerCase().replace(/^www\./i,""),i="l"):e.test(a.href)?(f=a.href.split("@")[1].toLowerCase(),i="m"):g.test(a.href)&&(f=a.href,f=f.toLowerCase(),i="t")}catch(k){}}if(oCONFIG.SUBDOMAIN_BASED?-1!==f.indexOf(b):f===b){if("m"===i){c=a.href.match(/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/);l={link_id:a.id,link_url:c[0],link_domain:c[0].split("@")[1],link_text:a.text.replace(/(?:[\r\n]+)+/g,"").trim(),link_classes:a.className,interaction_type:t};_sendEvent("email_click",l)}else{if("l"===i&&_isDownload(a)){c=a.pathname.split(/[#?&?]/)[0];d=_isDownload(a);l={file_name:c,file_extension:d,link_text:a.text.replace(/(?:[\r\n]+)+/g,"").trim(),link_id:a.id,link_url:a.href.replace(/[#?&].*/,""),link_domain:a.hostname.replace(/^www\./i,""),interaction_type:t};_sendEvent("file_download",l)}else if("l"===i&&!_isDownload(a)){}}}else{if("l"===i&&_isDownload(a)){c=a.pathname.split(/[#?&?]/)[0];d=_isDownload(a);l={file_name:c,file_extension:d,link_text:a.text.replace(/(?:[\r\n]+)+/g,"").trim(),link_id:a.id,link_url:a.href.replace(/[#?&].*/,""),link_domain:a.hostname.replace(/^www\./i,""),outbound:true,interaction_type:t};_sendEvent("file_download",l)}else if("l"===i&&!_isDownload(a)){l={link_id:a.id,link_url:a.href.replace(/[#?&].*/,""),link_domain:a.hostname.replace(/^www\./i,""),link_text:a.text.replace(/(?:[\r\n]+)+/g,"").trim(),link_classes:a.className,outbound:true,interaction_type:t};_sendEvent("click",l)}else if("m"===i){c=a.href.match(/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/);l={link_id:a.id,link_url:c[0],link_domain:c[0].split("@")[1],link_text:a.text.replace(/(?:[\r\n]+)+/g,"").trim(),link_classes:a.className,outbound:true,interaction_type:t};_sendEvent("email_click",l)}else if("t"===i){l={link_id:a.id,link_url:a.href.split("tel:")[1],link_text:a.text.replace(/(?:[\r\n]+)+/g,"").trim(),link_classes:a.className,interaction_type:t};_sendEvent("telephone_click",l)}}}}}catch(error){}};document.addEventListener?document.addEventListener("mousedown",_eventHandler,false):document.attachEvent&&document.attachEvent("onmousedown",_eventHandler);document.addEventListener?document.addEventListener("keydown",_eventHandler,false):document.attachEvent&&document.attachEvent("onkeydown",_eventHandler)}if(oCONFIG.YOUTUBE){var tag=document.createElement("script");tag.src="https://www.youtube.com/iframe_api";var firstScriptTag=document.getElementsByTagName("script")[0];firstScriptTag.parentNode.insertBefore(tag,firstScriptTag);var videoArray=[];var playerArray=[];var _buckets=[];var _milestoneController=oCONFIG.YT_MILESTONE;var ytUtils=[];onYouTubeIframeAPIReady=function(){for(var i=0;i=_buckets[cCi].milestone&&!_buckets[cCi].triggered&&_buckets[videoIndex].id===videoIndex){_buckets[cCi].triggered=true;y.video_percent=_buckets[cCi].milestone;y.video_current_time=Math.round(y.video_duration/_buckets.length*(cCi+1));_sendEvent("video_progress",y);cCi++}}},(playerArray[videoIndex].playerInfo!==undefined?Math.round(playerArray[videoIndex].playerInfo.duration):Math.round(playerArray[videoIndex].getDuration()))/_buckets.length)}]);ytUtils[ytUtils.length-1][1](videoIndex)}}else if(event.data==YT.PlayerState.PLAYING){_sendEvent("video_play",p)}if(event.data==YT.PlayerState.ENDED){_sendEvent("video_complete",p)}if(event.data==YT.PlayerState.PAUSED){_sendEvent("video_pause",p)}}catch(error){}};youtube_parser=function(e){var t=e.match(/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/);if(t&&11==t[2].length)return t[2]};IsYouTube=function(u){var e=u.match(/(.*)(youtu\.be\/|youtube(\-nocookie)?\.([A-Za-z]{2,4}|[A-Za-z]{2,3}\.[A-Za-z]{2})\/)(watch|embed\/|vi?\/)?(\?vi?=)?([^#&\?\/]{11}).*/);return null!=e&&e.length>0};YTUrlHandler=function(t){return t=t.replace(/origin\=(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})\&?/gi,"origin="+document.location.protocol+"//"+document.location.host),stAdd="",adFlag=!1,-1==t.indexOf("https")&&(t=t.replace("http","https")),-1==t.indexOf("?")&&(stAdd="?flag=1"),-1==t.indexOf("enablejsapi")&&(stAdd+="&enablejsapi=1",adFlag=!0),-1==t.indexOf("origin")&&(stAdd+="&origin="+document.location.protocol+"//"+document.location.host,adFlag=!0),1==adFlag?t+stAdd:t};_initYouTubeTracker=function(){var i=0;var allIframes=document.getElementsByTagName("iframe");for(var iframe=0;iframe=j&&j>media_status[e.target.id].latest_milestone){media_status[e.target.id].latest_milestone=j}}if(media_status[e.target.id].latest_milestone&&!media_status[e.target.id]._progress_milestones[media_status[e.target.id].latest_milestone]){media_status[e.target.id]._progress_milestones[media_status[e.target.id].latest_milestone]=true;media_type==="video"?(mObj.video_current_time=media_status[e.target.id].current,mObj.video_duration=Math.round(e.target.duration),mObj.video_percent=media_status[e.target.id].latest_milestone):(mObj.audio_current_time=media_status[e.target.id].current,mObj.audio_duration=Math.round(e.target.duration),mObj.audio_percent=media_status[e.target.id].latest_milestone);_sendEvent(media_type+"_progress",mObj)}break;case"play":media_type==="video"?(mObj.video_current_time=media_status[e.target.id].current,mObj.video_duration=Math.round(e.target.duration),mObj.video_percent=media_status[e.target.id].latest_milestone):(mObj.audio_current_time=media_status[e.target.id].current,mObj.audio_duration=Math.round(e.target.duration),mObj.audio_percent=media_status[e.target.id].latest_milestone);var e_n=media_status[e.target.id].current===0?media_type+"_start":media_type+"_play";_sendEvent(e_n,mObj);break;case"pause":if(media_status[e.target.id].current!==Math.round(e.target.duration)){media_type==="video"?(mObj.video_current_time=media_status[e.target.id].current,mObj.video_duration=Math.round(e.target.duration),mObj.video_percent=media_status[e.target.id].latest_milestone):(mObj.audio_current_time=media_status[e.target.id].current,mObj.audio_duration=Math.round(e.target.duration),mObj.audio_percent=media_status[e.target.id].latest_milestone);_sendEvent(media_type+"_pause",mObj)}break;case"ended":media_type==="video"?(mObj.video_current_time=media_status[e.target.id].current,mObj.video_duration=Math.round(e.target.duration),mObj.video_percent="100"):(mObj.audio_current_time=media_status[e.target.id].current,mObj.audio_duration=Math.round(e.target.duration),mObj.audio_percent="100");_sendEvent(media_type+"_complete",mObj);media_status[e.target.id].current=0;media_status[e.target.id].latest_milestone=0;for(var b=1;b<=100/_milestone;b++){100/_milestone===4&&b===100/_milestone?media_status[e.target.id].progress_point=95:_milestone*b!==100?media_status[e.target.id].progress_point=_milestone*b:"";media_status[e.target.id]._progress_milestones[media_status[e.target.id].progress_point]=false}break;default:break}}var htmlMedia=document.querySelectorAll("video,audio");for(var i=0;i0){beacon.events.join("\r\n");arguments[1]=beacon.events.join("\r\n")}}return pl.apply(this,arguments)}}catch(e){return pl.apply(this,arguments)}}}function _unflattenJSON(data){try{if(Object(data)!==data||Array.isArray(data))return data;var result={},cur,prop,idx,last,temp;for(var p in data){cur=result,prop="",last=0;do{idx=p.indexOf(".",last);temp=p.substring(last,idx!==-1?idx:undefined);cur=cur[prop]||(cur[prop]=!isNaN(parseInt(temp))?[]:{});prop=temp;last=idx+1}while(idx>=0);cur[prop]=data[p]}return result[""]}catch(error){}}function _flattenJSON(data){try{var result={};function recurse(cur,prop){if(Object(cur)!==cur){result[prop]=cur}else if(Array.isArray(cur)){for(var i=0,l=cur.length;i2?_param.slice(1).join("="):_param[1];_param.splice(2);_param[1]=_para;var _val;try{_val=decodeURIComponent(decodeURIComponent(_param[1]))}catch(e){_val=decodeURIComponent(_param[1])}if((_param[0].match(new RegExp(checkParams))!=null||/query|json/gi.test(type))&&_val.indexOf("?")>-1){var paramArray=_val.split("?").splice(1).join("&").split("&");var paramSubArray=[];for(pa=0;pa-1){paramSubArray.push(paramArray[pa].split("?")[1])}}paramArray=paramArray.concat(paramSubArray);for(var ix=0;ix1?(o.split("?")[1].split("&").forEach(function(o,i){_allowedQuerystrings.toString().toLowerCase().indexOf(o.split("=")[0])>-1&&(t=t+"&"+o)}),t.length>0?a+"?"+_piiRedactor(t.substring(1),"query"):a):a;return r}function _setAllowedQS(){var queries={default:["utm_id","utm_source","utm_medium","utm_campaign","utm_term","utm_content","utm_source_platform","utm_creative_format","utm_marketing_tactic","gbraid","wbraid","_gl","gclid","dclid","gclsrc","affiliate","dap-dev-env","v"],gsa:["challenge","state"],dhs:["appreceiptnum"],doc:["station","meas","start","atlc","epac","cpac","basin","fdays","cone","tswind120","gm_track","50wind120","hwind120","mltoa34","swath","radii","wsurge","key_messages","inundation","rainqpf","ero","gage","wfo","spanish_key_messages","key_messages","sid","lan","office","pil","product","site","lat","lon"],hhs:["s_cid","selectedfacets"],hud:["postid"],nasa:["feature","productid","selectedfacets"],nps:["gid","mapid","site","webcam","id"],nsf:["meas","start","atlc","epac","cpac","basin","fdays","cone","tswind120","gm_track","50wind120","hwind120","mltoa34","swath","radii","wsurge","key_messages","inundation","rainqpf","ero","gage","wfo","spanish_key_messages","key_messages","sid"],va:["id"],dod:["p"],opm:["l","soc","jt","j","rmi","smin","hp","g","d","a"]};_allowedQuerystrings=queries.default.concat(queries[oCONFIG.AGENCY.toLowerCase()]).concat(oCONFIG.SEARCH_PARAMS.toLowerCase().split("|"))}function _setUpTrackers(){oCONFIG.AUTOTRACKER?_initAutoTracker():"";oCONFIG.YOUTUBE?_initYouTubeTracker():"";oCONFIG.HTMLVIDEO?_initHTMLVideoTracker():"";_initBannerTracker()}function _setUpTrackersIfReady(){return"interactive"===document.readyState||"complete"===document.readyState?(_setUpTrackers(),!0):!1}_setUpTrackersIfReady()||(document.addEventListener?document.addEventListener("DOMContentLoaded",_setUpTrackers):document.attachEvent&&document.attachEvent("onreadystatechange",_setUpTrackersIfReady));if(typeof window!=="undefined"){window.dispatchEvent(new CustomEvent("dap-universal-federated-analytics-load"))}})(); +//# sourceMappingURL=Universal-Federated-Analytics-Min.js.map \ No newline at end of file diff --git a/Universal-Federated-Analytics.js b/Universal-Federated-Analytics.js index 26f1968..b737c52 100644 --- a/Universal-Federated-Analytics.js +++ b/Universal-Federated-Analytics.js @@ -1354,4 +1354,8 @@ Digital Analytics Program Government Wide Site Usage Measurement and Tracking. return (("interactive" === document.readyState || "complete" === document.readyState) ? (_setUpTrackers(), !0) : !1); } _setUpTrackersIfReady() || (document.addEventListener ? document.addEventListener("DOMContentLoaded", _setUpTrackers) : document.attachEvent && document.attachEvent("onreadystatechange", _setUpTrackersIfReady)); + + if(typeof window !== 'undefined') { + window.dispatchEvent(new CustomEvent('dap-universal-federated-analytics-load')); + } })(); diff --git a/features/basic_page_load.feature b/features/basic_page_load.feature index f28f98a..4033167 100644 --- a/features/basic_page_load.feature +++ b/features/basic_page_load.feature @@ -17,3 +17,8 @@ Feature: Test the outgoing requests sent by a basic page with DAP code loaded And I wait 5 seconds Then there is a GA4 request But there are no unexpected requests + + Scenario: Fire an event when DAP is loaded + When I load the test site + And I wait 5 seconds + Then the custom event "dap-universal-federated-analytics-load" is fired on window diff --git a/features/support/step_definitions/browser_steps.js b/features/support/step_definitions/browser_steps.js index 06a25e4..04556be 100644 --- a/features/support/step_definitions/browser_steps.js +++ b/features/support/step_definitions/browser_steps.js @@ -50,4 +50,9 @@ Then("there are no unexpected requests", function () { return (new URL(request.url)).host; }); expect(["localhost:8080", "www.googletagmanager.com", "www.google-analytics.com"]).to.include.members(requestUrls); -}) \ No newline at end of file +}); + +Then("the custom event {string} is fired on window", async function(eventName) { + const eventFired = await this.page.evaluate(() => window.dapLoadedEventName); + expect(eventFired).to.equal(eventName); +}); \ No newline at end of file diff --git a/test_site/index.html b/test_site/index.html index c129e56..5af02ed 100644 --- a/test_site/index.html +++ b/test_site/index.html @@ -6,6 +6,20 @@ DAP test site